@@ -8,7 +8,7 @@ import User from '../models/user';
8
8
import Project from '../models/project' ;
9
9
10
10
const defaultHTML =
11
- `<!DOCTYPE html>
11
+ `<!DOCTYPE html>
12
12
<html lang="en">
13
13
<head>
14
14
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script>
@@ -23,7 +23,7 @@ const defaultHTML =
23
23
` ;
24
24
25
25
const defaultCSS =
26
- `html, body {
26
+ `html, body {
27
27
margin: 0;
28
28
padding: 0;
29
29
}
@@ -132,6 +132,7 @@ function getSketchContent(projectsInAllCategories) {
132
132
}
133
133
} else {
134
134
project . sketchContent = res ;
135
+ // console.log("options.url: ", options.url);
135
136
}
136
137
return project ;
137
138
} ) . catch ( ( err ) => {
@@ -140,6 +141,17 @@ function getSketchContent(projectsInAllCategories) {
140
141
} ) ) ) ) ;
141
142
}
142
143
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
+
143
155
function createProjectsInP5user ( projectsInAllCategories ) {
144
156
const options = {
145
157
url : 'https://api.github.com/repos/processing/p5.js-website/contents/src/data/examples/assets' ,
@@ -156,7 +168,7 @@ function createProjectsInP5user(projectsInAllCategories) {
156
168
if ( err ) throw err ;
157
169
158
170
eachSeries ( projectsInAllCategories , ( projectsInOneCategory , categoryCallback ) => {
159
- eachSeries ( projectsInOneCategory , ( project , projectCallback ) => {
171
+ eachSeries ( projectsInOneCategory , async ( project , projectCallback ) => {
160
172
let newProject ;
161
173
const a = objectID ( ) . toHexString ( ) ;
162
174
const b = objectID ( ) . toHexString ( ) ;
@@ -248,12 +260,17 @@ function createProjectsInP5user(projectsInAllCategories) {
248
260
const assetsInProject = project . sketchContent . match ( / a s s e t s \/ [ \w - ] + \. [ \w ] * / g)
249
261
|| project . sketchContent . match ( / a s s e t \/ [ \w - ] * / g) || [ ] ;
250
262
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 ] ;
252
266
let assetName = assetNamePath . split ( 'assets/' ) [ 1 ] ;
267
+ let assetUrl = '' ;
268
+ let assetContent = '' ;
253
269
254
270
res . forEach ( ( asset ) => {
255
271
if ( asset . name === assetName || asset . name . split ( '.' ) [ 0 ] === assetName ) {
256
272
assetName = asset . name ;
273
+ assetUrl = asset . download_url ;
257
274
}
258
275
} ) ;
259
276
@@ -272,19 +289,58 @@ function createProjectsInP5user(projectsInAllCategories) {
272
289
}
273
290
274
291
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
+ }
286
341
}
287
- } ) ;
342
+ }
343
+ /* eslint-disable no-await-in-loop */
288
344
289
345
newProject . save ( ( saveErr , savedProject ) => {
290
346
if ( saveErr ) throw saveErr ;
@@ -304,6 +360,7 @@ function createProjectsInP5user(projectsInAllCategories) {
304
360
}
305
361
306
362
function getp5User ( ) {
363
+ console . log ( 'Getting p5 user' ) ;
307
364
User . findOne ( { username : 'p5' } , ( err , user ) => {
308
365
if ( err ) throw err ;
309
366
0 commit comments