@@ -143,6 +143,17 @@ func writeForm(form *multipart.Writer, meta api.FunctionDeployMetadata, fsys fs.
143
143
if err := enc .Encode (meta ); err != nil {
144
144
return errors .Errorf ("failed to encode metadata: %w" , err )
145
145
}
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
+ }
146
157
addFile := func (srcPath string , w io.Writer ) error {
147
158
f , err := fsys .Open (filepath .FromSlash (srcPath ))
148
159
if err != nil {
@@ -154,39 +165,15 @@ func writeForm(form *multipart.Writer, meta api.FunctionDeployMetadata, fsys fs.
154
165
} else if fi .IsDir () {
155
166
return errors .New ("file path is a directory: " + srcPath )
156
167
}
157
- fmt .Fprintf (os .Stderr , "Uploading asset (%s): %s\n " , * meta .Name , srcPath )
158
168
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 )
167
170
}
168
171
// Add import map
169
172
importMap := ImportMap {}
170
173
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 {
179
175
return err
180
176
}
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
- }
190
177
}
191
178
// Add static files
192
179
patterns := config .Glob (cast .Val (meta .StaticPatterns , []string {}))
@@ -207,6 +194,25 @@ type ImportMap struct {
207
194
Scopes map [string ]map [string ]string `json:"scopes"`
208
195
}
209
196
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
+
210
216
func (m * ImportMap ) Parse (data []byte ) error {
211
217
data = jsonc .ToJSONInPlace (data )
212
218
decoder := json .NewDecoder (bytes .NewReader (data ))
0 commit comments