@@ -8,7 +8,7 @@ import User from '../models/user';
88import Project from '../models/project' ;
99
1010const defaultHTML =
11- `<!DOCTYPE html>
11+ `<!DOCTYPE html>
1212<html lang="en">
1313 <head>
1414 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script>
@@ -23,7 +23,7 @@ const defaultHTML =
2323` ;
2424
2525const defaultCSS =
26- `html, body {
26+ `html, body {
2727 margin: 0;
2828 padding: 0;
2929}
@@ -132,6 +132,7 @@ function getSketchContent(projectsInAllCategories) {
132132 }
133133 } else {
134134 project . sketchContent = res ;
135+ // console.log("options.url: ", options.url);
135136 }
136137 return project ;
137138 } ) . catch ( ( err ) => {
@@ -140,6 +141,17 @@ function getSketchContent(projectsInAllCategories) {
140141 } ) ) ) ) ;
141142}
142143
144+ // a function to await for the response that contains the content of asset file
145+ function doRequest ( options ) {
146+ return new Promise ( ( ( resolve , reject ) => {
147+ rp ( options ) . then ( ( response ) => {
148+ resolve ( response ) ;
149+ } ) . catch ( ( error ) => {
150+ reject ( error ) ;
151+ } ) ;
152+ } ) ) ;
153+ }
154+
143155function createProjectsInP5user ( projectsInAllCategories ) {
144156 const options = {
145157 url : 'https://api.github.com/repos/processing/p5.js-website/contents/src/data/examples/assets' ,
@@ -156,7 +168,7 @@ function createProjectsInP5user(projectsInAllCategories) {
156168 if ( err ) throw err ;
157169
158170 eachSeries ( projectsInAllCategories , ( projectsInOneCategory , categoryCallback ) => {
159- eachSeries ( projectsInOneCategory , ( project , projectCallback ) => {
171+ eachSeries ( projectsInOneCategory , async ( project , projectCallback ) => {
160172 let newProject ;
161173 const a = objectID ( ) . toHexString ( ) ;
162174 const b = objectID ( ) . toHexString ( ) ;
@@ -248,12 +260,17 @@ function createProjectsInP5user(projectsInAllCategories) {
248260 const assetsInProject = project . sketchContent . match ( / a s s e t s \/ [ \w - ] + \. [ \w ] * / g)
249261 || project . sketchContent . match ( / a s s e t \/ [ \w - ] * / g) || [ ] ;
250262
251- assetsInProject . forEach ( ( assetNamePath , i ) => {
263+ /* eslint-disable no-await-in-loop */
264+ for ( let i = 0 ; i < assetsInProject . length ; i += 1 ) { // iterate through each asset in the project in series (async/await functionality would not work with forEach() )
265+ const assetNamePath = assetsInProject [ i ] ;
252266 let assetName = assetNamePath . split ( 'assets/' ) [ 1 ] ;
267+ let assetUrl = '' ;
268+ let assetContent = '' ;
253269
254270 res . forEach ( ( asset ) => {
255271 if ( asset . name === assetName || asset . name . split ( '.' ) [ 0 ] === assetName ) {
256272 assetName = asset . name ;
273+ assetUrl = asset . download_url ;
257274 }
258275 } ) ;
259276
@@ -272,19 +289,58 @@ function createProjectsInP5user(projectsInAllCategories) {
272289 }
273290
274291 const fileID = objectID ( ) . toHexString ( ) ;
275- newProject . files . push ( {
276- name : assetName ,
277- url : `https://cdn.jsdelivr.net/gh/processing/p5.js-website@main/src/data/examples/assets/${ assetName } ` ,
278- id : fileID ,
279- _id : fileID ,
280- children : [ ] ,
281- fileType : 'file'
282- } ) ;
283- console . log ( `create assets: ${ assetName } ` ) ;
284- // add asset file inside the newly created assets folder at index 4
285- newProject . files [ 4 ] . children . push ( fileID ) ;
292+
293+ if ( assetName . slice ( - 5 ) === '.vert' || assetName . slice ( - 5 ) === '.frag' ) { // check if the file has .vert or .frag extension
294+ const assetOptions = {
295+ url : assetUrl ,
296+ method : 'GET' ,
297+ headers : {
298+ ...headers ,
299+ Authorization : `Basic ${ Buffer . from ( `${ clientId } :${ clientSecret } ` ) . toString ( 'base64' ) } `
300+ } ,
301+ json : true
302+ } ;
303+
304+ // //a function to await for the response that contains the content of asset file
305+ // function doRequest(options) {
306+ // return new Promise(function (resolve, reject) {
307+ // rp(options).then((response) => {
308+ // resolve(response);
309+ // }).catch((error) => {
310+ // reject(error);
311+ // })
312+ // })
313+ // }
314+
315+ assetContent = await doRequest ( assetOptions ) ;
316+ // push to the files array of the project only when response is received
317+ newProject . files . push ( {
318+ name : assetName ,
319+ content : assetContent ,
320+ id : fileID ,
321+ _id : fileID ,
322+ children : [ ] ,
323+ fileType : 'file'
324+ } ) ;
325+ console . log ( `create assets: ${ assetName } ` ) ;
326+ // add asset file inside the newly created assets folder at index 4
327+ newProject . files [ 4 ] . children . push ( fileID ) ;
328+ } else { // for assets files that are not .vert or .frag extension
329+ newProject . files . push ( {
330+ name : assetName ,
331+ url : `https://cdn.jsdelivr.net/gh/processing/p5.js-website@main/src/data/examples/assets/${ assetName } ` ,
332+ id : fileID ,
333+ _id : fileID ,
334+ children : [ ] ,
335+ fileType : 'file'
336+ } ) ;
337+ console . log ( `create assets: ${ assetName } ` ) ;
338+ // add asset file inside the newly created assets folder at index 4
339+ newProject . files [ 4 ] . children . push ( fileID ) ;
340+ }
286341 }
287- } ) ;
342+ }
343+ /* eslint-disable no-await-in-loop */
288344
289345 newProject . save ( ( saveErr , savedProject ) => {
290346 if ( saveErr ) throw saveErr ;
@@ -304,6 +360,7 @@ function createProjectsInP5user(projectsInAllCategories) {
304360}
305361
306362function getp5User ( ) {
363+ console . log ( 'Getting p5 user' ) ;
307364 User . findOne ( { username : 'p5' } , ( err , user ) => {
308365 if ( err ) throw err ;
309366
0 commit comments