Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): load local deno.json file as import map #2824

Merged
merged 6 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion internal/functions/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
jgoux marked this conversation as resolved.
Show resolved Hide resolved
} else if len(function.ImportMap) == 0 && fallbackExists {
function.ImportMap = utils.FallbackImportMapPath
}
Expand Down
58 changes: 56 additions & 2 deletions internal/functions/deploy/deploy_test.go
sweatybridge marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down