Skip to content

Commit

Permalink
chore: factorize importpath checking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
avallete committed Dec 20, 2024
1 parent 1c93b5e commit 0ec463f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 47 deletions.
44 changes: 20 additions & 24 deletions internal/functions/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,6 @@ func GetFunctionSlugs(fsys afero.Fs) (slugs []string, err error) {
}

func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool, fsys afero.Fs) (config.FunctionConfig, error) {
// Although some functions do not require import map, it's more convenient to setup
// vscode deno extension with a single import map for all functions.
fallbackExists := true
if _, err := fsys.Stat(utils.FallbackImportMapPath); errors.Is(err, os.ErrNotExist) {
if _, err := fsys.Stat(utils.FallbackDenoJsonPath); errors.Is(err, os.ErrNotExist) {
fallbackExists = false
} else if err != nil {
return nil, errors.Errorf("failed to check fallback deno.json: %w", err)
}
} else if err != nil {
return nil, errors.Errorf("failed to check fallback import map: %w", err)
}
// Flag import map is specified relative to current directory instead of workdir
if len(importMapPath) > 0 && !filepath.IsAbs(importMapPath) {
importMapPath = filepath.Join(utils.CurrentDirAbs, importMapPath)
Expand All @@ -96,24 +84,32 @@ func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool,
if len(importMapPath) > 0 {
function.ImportMap = importMapPath
} else if len(function.ImportMap) == 0 {
denoJsonPath := filepath.Join(functionDir, "deno.json")
denoJsoncPath := filepath.Join(functionDir, "deno.jsonc")
if _, err := fsys.Stat(denoJsonPath); err == nil {
function.ImportMap = denoJsonPath
} else if _, err := fsys.Stat(denoJsoncPath); err == nil {
function.ImportMap = denoJsoncPath
} else if fallbackExists {
if _, err := fsys.Stat(utils.FallbackImportMapPath); err == nil {
function.ImportMap = utils.FallbackImportMapPath
} else {
function.ImportMap = utils.FallbackDenoJsonPath
}
if dedicatedFunctionPath, err := utils.GetImportsFilePath(functionDir, fsys); err == nil {
function.ImportMap = dedicatedFunctionPath
} else if fallbackFunctionPath, err := utils.GetImportsFilePath(utils.FunctionsDir, fsys); err == nil {
function.ImportMap = fallbackFunctionPath
}
}
if noVerifyJWT != nil {
function.VerifyJWT = cast.Ptr(!*noVerifyJWT)
}
functionConfig[name] = function
}
// Check validity of ImportMap paths
functionsWithFallback := []string{}
if fallbacksPath, err := utils.GetImportsFilePath(utils.FunctionsDir, fsys); err == nil {
for name, function := range functionConfig {
if function.ImportMap == fallbacksPath {
functionsWithFallback = append(functionsWithFallback, name)
}
}
if len(functionsWithFallback) > 0 {
fmt.Fprintf(os.Stderr, "Warning: The following functions are using the fallback import map at %s: %s\n",
fallbacksPath,
strings.Join(functionsWithFallback, ", "))
fmt.Fprintln(os.Stderr, "This is not recommended and will be deprecated. Please move import maps into each function folder.")
}
}

return functionConfig, nil
}
34 changes: 13 additions & 21 deletions internal/functions/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,27 +287,19 @@ verify_jwt = false
}

func TestImportMapPath(t *testing.T) {
t.Run("loads import map from default location", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, utils.FallbackImportMapPath, []byte("{}"), 0644))
// Run test
fc, err := GetFunctionConfig([]string{"test"}, "", nil, fsys)
// Check error
assert.NoError(t, err)
assert.Equal(t, utils.FallbackImportMapPath, fc["test"].ImportMap)
})

t.Run("loads deno.json from default location", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, utils.FallbackDenoJsonPath, []byte("{}"), 0644))
// Run test
fc, err := GetFunctionConfig([]string{"test"}, "", nil, fsys)
// Check error
assert.NoError(t, err)
assert.Equal(t, utils.FallbackDenoJsonPath, fc["test"].ImportMap)
})
for _, importFile := range utils.ImportFiles {
t.Run("loads "+importFile+" from default location", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
importPath := filepath.Join(utils.FunctionsDir, importFile)
require.NoError(t, afero.WriteFile(fsys, importPath, []byte("{}"), 0644))
// Run test
fc, err := GetFunctionConfig([]string{"test"}, "", nil, fsys)
// Check error
assert.NoError(t, err)
assert.Equal(t, importPath, fc["test"].ImportMap)
})
}

t.Run("per function config takes precedence", func(t *testing.T) {
t.Cleanup(func() { clear(utils.Config.Functions) })
Expand Down
15 changes: 13 additions & 2 deletions internal/utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/docker/docker/client"
Expand Down Expand Up @@ -116,6 +117,7 @@ var (
}

SupabaseDirPath = "supabase"
ImportFiles = []string{"deno.jsonc", "deno.json", "import_map.json"}
ConfigPath = filepath.Join(SupabaseDirPath, "config.toml")
GitIgnorePath = filepath.Join(SupabaseDirPath, ".gitignore")
TempDir = filepath.Join(SupabaseDirPath, ".temp")
Expand All @@ -135,8 +137,7 @@ var (
SchemasDir = filepath.Join(SupabaseDirPath, "schemas")
MigrationsDir = filepath.Join(SupabaseDirPath, "migrations")
FunctionsDir = filepath.Join(SupabaseDirPath, "functions")
FallbackImportMapPath = filepath.Join(FunctionsDir, "import_map.json")
FallbackDenoJsonPath = filepath.Join(FunctionsDir, "deno.json")
FallbackImportMapPath = filepath.Join(FunctionsDir, ImportFiles[2])
FallbackEnvFilePath = filepath.Join(FunctionsDir, ".env")
DbTestsDir = filepath.Join(SupabaseDirPath, "tests")
CustomRolesPath = filepath.Join(SupabaseDirPath, "roles.sql")
Expand All @@ -147,6 +148,16 @@ var (
ErrNotRunning = errors.Errorf("%s is not running.", Aqua("supabase start"))
)

func GetImportsFilePath(basedir string, fsys afero.Fs) (string, error) {
for _, filename := range ImportFiles {
path := filepath.Join(basedir, filename)
if _, err := fsys.Stat(path); err == nil {
return path, nil
}
}
return "", errors.Errorf("no import map found, searched for: %s", strings.Join(ImportFiles, ", "))
}

func GetCurrentTimestamp() string {
// Magic number: https://stackoverflow.com/q/45160822.
return time.Now().UTC().Format("20060102150405")
Expand Down

0 comments on commit 0ec463f

Please sign in to comment.