@@ -194,43 +194,64 @@ export function projectForUserExists(username, projectId, callback) {
194
194
} ) ;
195
195
}
196
196
197
- // function getExternalLibs(project) {
198
- // const indexHtml = project.files.find((file) => file.name === "index.html");
199
- // const { window } = new JSDOM(indexHtml.content);
200
- // const scriptTags = window.document.getElementsByTagName('script');
201
- // const libs = [];
197
+ function bundleExternalLibs ( project ) {
198
+ const indexHtml = project . files . find ( ( file ) => file . name === 'index.html' ) ;
199
+ const { window } = new JSDOM ( indexHtml . content ) ;
200
+ const scriptTags = window . document . getElementsByTagName ( 'script' ) ;
202
201
203
- // Object.values(scriptTags).forEach((tag ) => {
204
- // const { src } = tag ;
202
+ Object . values ( scriptTags ) . forEach ( async ( { src } , i ) => {
203
+ if ( ! isUrl ( src ) ) return ;
205
204
206
- // if (!isUrl(src)) return;
205
+ const path = src . split ( '/' ) ;
206
+ const filename = path [ path . length - 1 ] ;
207
207
208
- // const path = src.split('/');
209
- // const filename = path[path.length - 1];
208
+ project . files . push ( {
209
+ name : filename ,
210
+ url : src
211
+ } ) ;
210
212
211
- // libs.push({
212
- // name: filename,
213
- // url: src
214
- // });
215
- // });
213
+ const libId = project . files . find ( ( file ) => file . name === filename ) . id ;
214
+ project . files . find ( ( file ) => file . name === 'root' ) . children . push ( libId ) ;
215
+ } ) ;
216
+ }
216
217
217
- // return libs;
218
- // }
218
+ function addFileToZip ( file , files , zip , path = '' ) {
219
+ return new Promise ( ( resolve , reject ) => {
220
+ if ( file . fileType === 'folder' ) {
221
+ const newPath = file . name === 'root' ? path : `${ path } ${ file . name } /` ;
222
+ const numChildFiles = file . children . filter ( ( f ) => f . fileType !== 'folder' )
223
+ . length ;
224
+ let childrenAdded = 0 ;
219
225
220
- async function addFileToZip ( file , files , zip , path = '' ) {
221
- if ( file . fileType === 'folder' ) {
222
- const newPath = file . name === 'root' ? path : `${ path } ${ file . name } /` ;
223
- file . children . forEach ( ( fileId ) => {
224
- const childFile = files . find ( ( f ) => f . id === fileId ) ;
225
- addFileToZip ( childFile , files , zip , newPath ) ;
226
- } ) ;
227
- } else if ( file . url ) {
228
- console . log ( `file: ${ JSON . stringify ( file ) } ` ) ;
229
- const { data } = await axios . get ( file . url ) ;
230
- zip . file ( `${ path } ${ file . name } ` , data ) ;
231
- } else {
232
- zip . file ( `${ path } ${ file . name } ` , file . content ) ;
233
- }
226
+ file . children . forEach ( async ( fileId ) => {
227
+ const childFile = files . find ( ( f ) => f . id === fileId ) ;
228
+
229
+ try {
230
+ await addFileToZip ( childFile , files , zip , newPath ) ;
231
+ childrenAdded += 1 ;
232
+
233
+ if ( childrenAdded === numChildFiles ) {
234
+ resolve ( ) ;
235
+ }
236
+ } catch ( err ) {
237
+ reject ( err ) ;
238
+ }
239
+ } ) ;
240
+ } else if ( file . url ) {
241
+ axios
242
+ . get ( file . url )
243
+ . then ( ( { data } ) => {
244
+ zip . file ( `${ path } ${ file . name } ` , data ) ;
245
+ resolve ( ) ;
246
+ } )
247
+ . catch ( ( err ) => {
248
+ reject ( err ) ;
249
+ } ) ;
250
+ } else {
251
+ zip . file ( `${ path } ${ file . name } ` , file . content ) ;
252
+ resolve ( ) ;
253
+ }
254
+ } ) ;
234
255
}
235
256
236
257
async function buildZip ( project , req , res ) {
@@ -243,10 +264,9 @@ async function buildZip(project, req, res) {
243
264
) } _${ currentTime } .zip`;
244
265
const { files } = project ;
245
266
const root = files . find ( ( file ) => file . name === 'root' ) ;
246
- // const libs = getExternalLibs(project);
247
267
248
- // libs.forEach((lib) => addFileToZip(lib, files, zip) );
249
- addFileToZip ( root , files , zip ) ;
268
+ bundleExternalLibs ( project ) ;
269
+ await addFileToZip ( root , files , zip ) ;
250
270
251
271
const base64 = await zip . generateAsync ( { type : 'base64' } ) ;
252
272
const buff = Buffer . from ( base64 , 'base64' ) ;
0 commit comments