Skip to content

Commit fd96e19

Browse files
committed
chore: refactor import map constructor
1 parent f8d12cc commit fd96e19

File tree

3 files changed

+42
-50
lines changed

3 files changed

+42
-50
lines changed

internal/utils/deno.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -209,29 +209,12 @@ func CopyDenoScripts(ctx context.Context, fsys afero.Fs) (*DenoScriptDir, error)
209209
return &sd, nil
210210
}
211211

212-
func newImportMap(relJsonPath string, fsys afero.Fs) (function.ImportMap, error) {
213-
var result function.ImportMap
214-
if len(relJsonPath) == 0 {
215-
return result, nil
216-
}
217-
data, err := afero.ReadFile(fsys, relJsonPath)
218-
if err != nil {
219-
return result, errors.Errorf("failed to load import map: %w", err)
220-
}
221-
if err := result.Parse(data); err != nil {
222-
return result, err
223-
}
224-
unixPath := filepath.ToSlash(relJsonPath)
225-
if err := result.Resolve(unixPath, afero.NewIOFS(fsys)); err != nil {
226-
return result, err
227-
}
228-
return result, nil
229-
}
230-
231212
func BindHostModules(cwd, relEntrypointPath, relImportMapPath string, fsys afero.Fs) ([]string, error) {
232-
importMap, err := newImportMap(relImportMapPath, fsys)
233-
if err != nil {
234-
return nil, err
213+
importMap := function.ImportMap{}
214+
if imPath := filepath.ToSlash(relImportMapPath); len(imPath) > 0 {
215+
if err := importMap.Load(imPath, afero.NewIOFS(fsys)); err != nil {
216+
return nil, err
217+
}
235218
}
236219
var modules []string
237220
// Resolving all Import Graph

internal/utils/deno_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/spf13/afero"
99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
11+
"github.com/supabase/cli/pkg/function"
1112
)
1213

1314
func TestResolveImports(t *testing.T) {
@@ -31,7 +32,8 @@ func TestResolveImports(t *testing.T) {
3132
require.NoError(t, fsys.Mkdir(filepath.Join(cwd, DbTestsDir), 0755))
3233
require.NoError(t, fsys.Mkdir(filepath.Join(cwd, FunctionsDir, "child"), 0755))
3334
// Run test
34-
resolved, err := newImportMap(jsonPath, fsys)
35+
resolved := function.ImportMap{}
36+
err = resolved.Load(jsonPath, afero.NewIOFS(fsys))
3537
// Check error
3638
assert.NoError(t, err)
3739
assert.Equal(t, "/tmp/", resolved.Imports["abs/"])
@@ -53,7 +55,8 @@ func TestResolveImports(t *testing.T) {
5355
fsys := afero.NewMemMapFs()
5456
require.NoError(t, afero.WriteFile(fsys, FallbackImportMapPath, importMap, 0644))
5557
// Run test
56-
resolved, err := newImportMap(FallbackImportMapPath, fsys)
58+
resolved := function.ImportMap{}
59+
err := resolved.Load(FallbackImportMapPath, afero.NewIOFS(fsys))
5760
// Check error
5861
assert.NoError(t, err)
5962
assert.Equal(t, "https://deno.land", resolved.Scopes["my-scope"]["my-mod"])

pkg/function/deploy.go

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ func writeForm(form *multipart.Writer, meta api.FunctionDeployMetadata, fsys fs.
143143
if err := enc.Encode(meta); err != nil {
144144
return errors.Errorf("failed to encode metadata: %w", err)
145145
}
146+
uploadAsset := func(srcPath string, r io.Reader) error {
147+
fmt.Fprintf(os.Stderr, "Uploading asset (%s): %s\n", *meta.Name, srcPath)
148+
f, err := form.CreateFormFile("file", srcPath)
149+
if err != nil {
150+
return errors.Errorf("failed to create map: %w", err)
151+
}
152+
if _, err := io.Copy(f, r); err != nil {
153+
return errors.Errorf("failed to write form: %w", err)
154+
}
155+
return nil
156+
}
146157
addFile := func(srcPath string, w io.Writer) error {
147158
f, err := fsys.Open(filepath.FromSlash(srcPath))
148159
if err != nil {
@@ -154,39 +165,15 @@ func writeForm(form *multipart.Writer, meta api.FunctionDeployMetadata, fsys fs.
154165
} else if fi.IsDir() {
155166
return errors.New("file path is a directory: " + srcPath)
156167
}
157-
fmt.Fprintf(os.Stderr, "Uploading asset (%s): %s\n", *meta.Name, srcPath)
158168
r := io.TeeReader(f, w)
159-
dst, err := form.CreateFormFile("file", srcPath)
160-
if err != nil {
161-
return errors.Errorf("failed to create form: %w", err)
162-
}
163-
if _, err := io.Copy(dst, r); err != nil {
164-
return errors.Errorf("failed to write form: %w", err)
165-
}
166-
return nil
169+
return uploadAsset(srcPath, r)
167170
}
168171
// Add import map
169172
importMap := ImportMap{}
170173
if imPath := cast.Val(meta.ImportMapPath, ""); len(imPath) > 0 {
171-
data, err := fs.ReadFile(fsys, filepath.FromSlash(imPath))
172-
if err != nil {
173-
return errors.Errorf("failed to load import map: %w", err)
174-
}
175-
if err := importMap.Parse(data); err != nil {
176-
return err
177-
}
178-
if err := importMap.Resolve(imPath, fsys); err != nil {
174+
if err := importMap.Load(imPath, fsys, uploadAsset); err != nil {
179175
return err
180176
}
181-
// TODO: replace with addFile once edge runtime supports jsonc
182-
fmt.Fprintf(os.Stderr, "Uploading asset (%s): %s\n", *meta.Name, imPath)
183-
f, err := form.CreateFormFile("file", imPath)
184-
if err != nil {
185-
return errors.Errorf("failed to create import map: %w", err)
186-
}
187-
if _, err := f.Write(data); err != nil {
188-
return errors.Errorf("failed to write import map: %w", err)
189-
}
190177
}
191178
// Add static files
192179
patterns := config.Glob(cast.Val(meta.StaticPatterns, []string{}))
@@ -207,6 +194,25 @@ type ImportMap struct {
207194
Scopes map[string]map[string]string `json:"scopes"`
208195
}
209196

197+
func (m *ImportMap) Load(imPath string, fsys fs.FS, opts ...func(string, io.Reader) error) error {
198+
data, err := fs.ReadFile(fsys, filepath.FromSlash(imPath))
199+
if err != nil {
200+
return errors.Errorf("failed to load import map: %w", err)
201+
}
202+
if err := m.Parse(data); err != nil {
203+
return err
204+
}
205+
if err := m.Resolve(imPath, fsys); err != nil {
206+
return err
207+
}
208+
for _, apply := range opts {
209+
if err := apply(imPath, bytes.NewReader(data)); err != nil {
210+
return err
211+
}
212+
}
213+
return nil
214+
}
215+
210216
func (m *ImportMap) Parse(data []byte) error {
211217
data = jsonc.ToJSONInPlace(data)
212218
decoder := json.NewDecoder(bytes.NewReader(data))

0 commit comments

Comments
 (0)