Skip to content

Commit

Permalink
feat: load local deno.json file
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoux committed Nov 1, 2024
1 parent 68f9c56 commit c09681d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
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
} 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
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

0 comments on commit c09681d

Please sign in to comment.