From c09681da79ce7be7a3ee8311e01af55eb3782a77 Mon Sep 17 00:00:00 2001 From: Julien Goux Date: Fri, 1 Nov 2024 17:40:51 +0100 Subject: [PATCH] feat: load local deno.json file --- internal/functions/deploy/deploy.go | 10 +++- internal/functions/deploy/deploy_test.go | 58 +++++++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/internal/functions/deploy/deploy.go b/internal/functions/deploy/deploy.go index 855d44537..a998e3a36 100644 --- a/internal/functions/deploy/deploy.go +++ b/internal/functions/deploy/deploy.go @@ -81,12 +81,20 @@ func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool, functionConfig := make(config.FunctionConfig, len(slugs)) for _, name := range slugs { function := utils.Config.Functions[name] + functionDir := filepath.Join(utils.FunctionsDir, name) // Precedence order: flag > config > fallback if len(function.Entrypoint) == 0 { - function.Entrypoint = filepath.Join(utils.FunctionsDir, name, "index.ts") + function.Entrypoint = filepath.Join(functionDir, "index.ts") } + // Check for deno.json or deno.jsonc in functionDir + denoJsonPath := filepath.Join(functionDir, "deno.json") + denoJsoncPath := filepath.Join(functionDir, "deno.jsonc") if len(importMapPath) > 0 { function.ImportMap = importMapPath + } else if _, err := fsys.Stat(denoJsonPath); err == nil { + function.ImportMap = denoJsonPath + } else if _, err := fsys.Stat(denoJsoncPath); err == nil { + function.ImportMap = denoJsoncPath } else if len(function.ImportMap) == 0 && fallbackExists { function.ImportMap = utils.FallbackImportMapPath } diff --git a/internal/functions/deploy/deploy_test.go b/internal/functions/deploy/deploy_test.go index c92f210cc..7f3d17bed 100644 --- a/internal/functions/deploy/deploy_test.go +++ b/internal/functions/deploy/deploy_test.go @@ -314,6 +314,53 @@ func TestImportMapPath(t *testing.T) { assert.Equal(t, "import_map.json", fc[slug].ImportMap) }) + t.Run("loads local deno.json", func(t *testing.T) { + t.Cleanup(func() { clear(utils.Config.Functions) }) + slug := "hello" + // Setup in-memory fs + fsys := afero.NewMemMapFs() + // Create deno.json in function folder + denoConfigPath := filepath.Join(utils.FunctionsDir, slug, "deno.json") + require.NoError(t, afero.WriteFile(fsys, denoConfigPath, []byte("{}"), 0644)) + // Run test + fc, err := GetFunctionConfig([]string{slug}, "", nil, fsys) + // Check error + assert.NoError(t, err) + assert.Equal(t, denoConfigPath, fc[slug].ImportMap) + }) + + t.Run("loads local deno.jsonc", func(t *testing.T) { + t.Cleanup(func() { clear(utils.Config.Functions) }) + slug := "hello" + // Setup in-memory fs + fsys := afero.NewMemMapFs() + // Create deno.jsonc in function folder + denoConfigPath := filepath.Join(utils.FunctionsDir, slug, "deno.jsonc") + require.NoError(t, afero.WriteFile(fsys, denoConfigPath, []byte("{}"), 0644)) + // Run test + fc, err := GetFunctionConfig([]string{slug}, "", nil, fsys) + // Check error + assert.NoError(t, err) + assert.Equal(t, denoConfigPath, fc[slug].ImportMap) + }) + + t.Run("local deno.json takes precedence over fallback", func(t *testing.T) { + t.Cleanup(func() { clear(utils.Config.Functions) }) + slug := "hello" + // Setup in-memory fs + fsys := afero.NewMemMapFs() + // Create fallback import map to test precedence order + require.NoError(t, afero.WriteFile(fsys, utils.FallbackImportMapPath, []byte("{}"), 0644)) + // Create deno.json in function folder to test precedence order + denoConfigPath := filepath.Join(utils.FunctionsDir, slug, "deno.json") + require.NoError(t, afero.WriteFile(fsys, denoConfigPath, []byte("{}"), 0644)) + // Run test + fc, err := GetFunctionConfig([]string{slug}, "", cast.Ptr(false), fsys) + // Check error + assert.NoError(t, err) + assert.Equal(t, denoConfigPath, fc[slug].ImportMap) + }) + t.Run("overrides with cli flag", func(t *testing.T) { t.Cleanup(func() { clear(utils.Config.Functions) }) slug := "hello" @@ -322,12 +369,19 @@ func TestImportMapPath(t *testing.T) { } // Setup in-memory fs fsys := afero.NewMemMapFs() + // Custom global import map loaded via cli flag + customImportMapPath := filepath.Join(utils.FunctionsDir, "custom_import_map.json") + require.NoError(t, afero.WriteFile(fsys, customImportMapPath, []byte("{}"), 0644)) + // Create fallback import map to test precedence order require.NoError(t, afero.WriteFile(fsys, utils.FallbackImportMapPath, []byte("{}"), 0644)) + // Create deno.json in function folder to test precedence order + denoConfigPath := filepath.Join(utils.FunctionsDir, slug, "deno.json") + require.NoError(t, afero.WriteFile(fsys, denoConfigPath, []byte("{}"), 0644)) // Run test - fc, err := GetFunctionConfig([]string{slug}, utils.FallbackImportMapPath, cast.Ptr(false), fsys) + fc, err := GetFunctionConfig([]string{slug}, customImportMapPath, cast.Ptr(false), fsys) // Check error assert.NoError(t, err) - assert.Equal(t, utils.FallbackImportMapPath, fc[slug].ImportMap) + assert.Equal(t, customImportMapPath, fc[slug].ImportMap) }) t.Run("returns empty string if no fallback", func(t *testing.T) {