@@ -236,153 +236,144 @@ async function createProjectsInP5user(projectsInAllCategories) {
236236
237237 try {
238238 const res = await rp ( options ) ;
239- User . findOne ( { username : 'p5' } , ( err , user ) => {
240- if ( err ) throw err ;
241-
242- Q . all ( projectsInAllCategories . map ( projectsInOneCategory => Q . all ( projectsInOneCategory . map ( async ( project ) => {
243- let newProject ;
244- const a = objectID ( ) . toHexString ( ) ;
245- const b = objectID ( ) . toHexString ( ) ;
246- const c = objectID ( ) . toHexString ( ) ;
247- const r = objectID ( ) . toHexString ( ) ;
248- const noNumberprojectName = project . projectName . replace ( / ( \d + ) / g, '' ) ;
249- if ( noNumberprojectName === 'Instance Mode: Instance Container ' ) {
250- newProject = new Project ( {
251- name : project . projectName ,
252- user : user . _id ,
253- files : [
254- {
255- name : 'root' ,
256- id : r ,
257- _id : r ,
258- children : [ a , b , c ] ,
259- fileType : 'folder'
260- } ,
261- {
262- name : 'sketch.js' ,
263- content : '// Instance Mode: Instance Container, please check its index.html file' ,
264- id : a ,
265- _id : a ,
266- fileType : 'file' ,
267- children : [ ]
268- } ,
269- {
270- name : 'index.html' ,
271- content : project . sketchContent ,
272- isSelectedFile : true ,
273- id : b ,
274- _id : b ,
275- fileType : 'file' ,
276- children : [ ]
277- } ,
278- {
279- name : 'style.css' ,
280- content : defaultCSS ,
281- id : c ,
282- _id : c ,
283- fileType : 'file' ,
284- children : [ ]
285- }
286- ] ,
287- _id : shortid . generate ( )
288- } ) ;
289- } else {
290- newProject = new Project ( {
291- name : project . projectName ,
292- user : user . _id ,
293- files : [
294- {
295- name : 'root' ,
296- id : r ,
297- _id : r ,
298- children : [ a , b , c ] ,
299- fileType : 'folder'
300- } ,
301- {
302- name : 'sketch.js' ,
303- content : project . sketchContent ,
304- id : a ,
305- _id : a ,
306- isSelectedFile : true ,
307- fileType : 'file' ,
308- children : [ ]
309- } ,
310- {
311- name : 'index.html' ,
312- content : defaultHTML ,
313- id : b ,
314- _id : b ,
315- fileType : 'file' ,
316- children : [ ]
317- } ,
318- {
319- name : 'style.css' ,
320- content : defaultCSS ,
321- id : c ,
322- _id : c ,
323- fileType : 'file' ,
324- children : [ ]
325- }
326- ] ,
327- _id : shortid . generate ( )
328- } ) ;
329- }
330-
331- const assetsInProject = project . sketchContent . match ( / a s s e t s \/ [ \w - ] + \. [ \w ] * / g)
332- || project . sketchContent . match ( / a s s e t \/ [ \w - ] * / g) || [ ] ;
239+ const user = await User . findOne ( { username : 'p5' } ) . exec ( ) ;
240+ Q . all ( projectsInAllCategories . map ( projectsInOneCategory => Q . all ( projectsInOneCategory . map ( async ( project ) => {
241+ let newProject ;
242+ const a = objectID ( ) . toHexString ( ) ;
243+ const b = objectID ( ) . toHexString ( ) ;
244+ const c = objectID ( ) . toHexString ( ) ;
245+ const r = objectID ( ) . toHexString ( ) ;
246+ const noNumberprojectName = project . projectName . replace ( / ( \d + ) / g, '' ) ;
247+ if ( noNumberprojectName === 'Instance Mode: Instance Container ' ) {
248+ newProject = new Project ( {
249+ name : project . projectName ,
250+ user : user . _id ,
251+ files : [
252+ {
253+ name : 'root' ,
254+ id : r ,
255+ _id : r ,
256+ children : [ a , b , c ] ,
257+ fileType : 'folder'
258+ } ,
259+ {
260+ name : 'sketch.js' ,
261+ content : '// Instance Mode: Instance Container, please check its index.html file' ,
262+ id : a ,
263+ _id : a ,
264+ fileType : 'file' ,
265+ children : [ ]
266+ } ,
267+ {
268+ name : 'index.html' ,
269+ content : project . sketchContent ,
270+ isSelectedFile : true ,
271+ id : b ,
272+ _id : b ,
273+ fileType : 'file' ,
274+ children : [ ]
275+ } ,
276+ {
277+ name : 'style.css' ,
278+ content : defaultCSS ,
279+ id : c ,
280+ _id : c ,
281+ fileType : 'file' ,
282+ children : [ ]
283+ }
284+ ] ,
285+ _id : shortid . generate ( )
286+ } ) ;
287+ } else {
288+ newProject = new Project ( {
289+ name : project . projectName ,
290+ user : user . _id ,
291+ files : [
292+ {
293+ name : 'root' ,
294+ id : r ,
295+ _id : r ,
296+ children : [ a , b , c ] ,
297+ fileType : 'folder'
298+ } ,
299+ {
300+ name : 'sketch.js' ,
301+ content : project . sketchContent ,
302+ id : a ,
303+ _id : a ,
304+ isSelectedFile : true ,
305+ fileType : 'file' ,
306+ children : [ ]
307+ } ,
308+ {
309+ name : 'index.html' ,
310+ content : defaultHTML ,
311+ id : b ,
312+ _id : b ,
313+ fileType : 'file' ,
314+ children : [ ]
315+ } ,
316+ {
317+ name : 'style.css' ,
318+ content : defaultCSS ,
319+ id : c ,
320+ _id : c ,
321+ fileType : 'file' ,
322+ children : [ ]
323+ }
324+ ] ,
325+ _id : shortid . generate ( )
326+ } ) ;
327+ }
333328
334- try {
335- await addAssetsToProject ( assetsInProject , res , newProject ) ;
336- } catch ( error ) {
337- throw error ;
338- }
329+ const assetsInProject = project . sketchContent . match ( / a s s e t s \/ [ \w - ] + \. [ \w ] * / g)
330+ || project . sketchContent . match ( / a s s e t \/ [ \w - ] * / g) || [ ] ;
339331
340- newProject . save ( ( saveErr , savedProject ) => {
341- if ( saveErr ) throw saveErr ;
342- console . log ( `Created a new project in p5 user: ${ savedProject . name } ` ) ;
343- } ) ;
344- } ) ) ) )
345- . then ( ( ) => process . exit ( ) ) ;
346- } ) ;
332+ try {
333+ await addAssetsToProject ( assetsInProject , res , newProject ) ;
334+ const savedProject = await newProject . save ( ) ;
335+ console . log ( `Created a new project in p5 user: ${ savedProject . name } ` ) ;
336+ } catch ( error ) {
337+ throw error ;
338+ }
339+ } ) ) ) )
340+ . then ( ( ) => process . exit ( ) ) ;
347341 } catch ( error ) {
348342 throw error ;
349343 }
350344}
351345
352- function getp5User ( ) {
346+ async function getp5User ( ) {
353347 console . log ( 'Getting p5 user' ) ;
354- User . findOne ( { username : 'p5' } , async ( err , user ) => {
355- if ( err ) throw err ;
356-
348+ try {
349+ const user = await User . findOne ( { username : 'p5' } ) . exec ( ) ;
357350 let p5User = user ;
358351 if ( ! p5User ) {
359352 p5User = new User ( {
360353 username : 'p5' ,
361354 email : process . env . EXAMPLE_USER_EMAIL ,
362355 password : process . env . EXAMPLE_USER_PASSWORD
363356 } ) ;
364- p5User . save ( ( saveErr ) => {
365- if ( saveErr ) throw saveErr ;
366- console . log ( `Created a user p5 ${ p5User } ` ) ;
367- } ) ;
357+ await p5User . save ( ) ;
358+ console . log ( `Created a user p5 ${ p5User } ` ) ;
368359 }
369-
370- Project . find ( { user : p5User . _id } , ( projectsErr , projects ) => {
371- // if there are already some sketches, delete them
372- console . log ( 'Deleting old projects...' ) ;
373- projects . forEach ( ( project ) => {
374- Project . remove ( { _id : project . _id } , ( removeErr ) => {
375- if ( removeErr ) throw removeErr ;
376- } ) ;
377- } ) ;
360+ const projects = await Project . find ( { user : p5User . _id } ) . exec ( ) ;
361+ console . log ( 'Deleting old projects...' ) ;
362+ projects . forEach ( async ( project ) => {
363+ try {
364+ await Project . deleteOne ( { _id : project . _id } ) ;
365+ } catch ( error ) {
366+ throw error ;
367+ }
378368 } ) ;
379-
380369 const categories = await getCategories ( ) ;
381370 const sketchesInCategories = await getSketchesInCategories ( categories ) ;
382371 const sketchContent = await getSketchContent ( sketchesInCategories ) ;
383- const projects = createProjectsInP5user ( sketchContent ) ;
384- return projects ;
385- } ) ;
372+ const projectsInUser = createProjectsInP5user ( sketchContent ) ;
373+ return projectsInUser ;
374+ } catch ( error ) {
375+ throw error ;
376+ }
386377}
387378
388379getp5User ( ) ;
0 commit comments