From 0ec463f2d84a6681477d942a314d20aafdaeac9e Mon Sep 17 00:00:00 2001 From: avallete Date: Fri, 20 Dec 2024 14:31:13 +0900 Subject: [PATCH] chore: factorize importpath checking logic --- internal/functions/deploy/deploy.go | 44 +++++++++++------------- internal/functions/deploy/deploy_test.go | 34 +++++++----------- internal/utils/misc.go | 15 ++++++-- 3 files changed, 46 insertions(+), 47 deletions(-) diff --git a/internal/functions/deploy/deploy.go b/internal/functions/deploy/deploy.go index 7816ed12b..c56a35358 100644 --- a/internal/functions/deploy/deploy.go +++ b/internal/functions/deploy/deploy.go @@ -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) @@ -96,18 +84,10 @@ 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 { @@ -115,5 +95,21 @@ func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool, } 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 } diff --git a/internal/functions/deploy/deploy_test.go b/internal/functions/deploy/deploy_test.go index 682a4dba0..ee06c7641 100644 --- a/internal/functions/deploy/deploy_test.go +++ b/internal/functions/deploy/deploy_test.go @@ -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) }) diff --git a/internal/utils/misc.go b/internal/utils/misc.go index f2a5005fd..ad095ba10 100644 --- a/internal/utils/misc.go +++ b/internal/utils/misc.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "regexp" + "strings" "time" "github.com/docker/docker/client" @@ -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") @@ -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") @@ -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")