@@ -140,6 +140,91 @@ function getSketchContent(projectsInAllCategories) {
140
140
} ) ) ) ) ;
141
141
}
142
142
143
+ async function addAssetsToProject ( assets , response , project ) {
144
+ /* eslint-disable no-await-in-loop */
145
+ for ( let i = 0 ; i < assets . length ; i += 1 ) { // iterate through each asset in the project in series (async/await functionality would not work with forEach() )
146
+ const assetNamePath = assets [ i ] ;
147
+ let assetName = assetNamePath . split ( 'assets/' ) [ 1 ] ;
148
+ let assetUrl = '' ;
149
+ let assetContent = '' ;
150
+
151
+ response . forEach ( ( asset ) => {
152
+ if ( asset . name === assetName || asset . name . split ( '.' ) [ 0 ] === assetName ) {
153
+ assetName = asset . name ;
154
+ assetUrl = asset . download_url ;
155
+ }
156
+ } ) ;
157
+
158
+ if ( assetName !== '' ) {
159
+ if ( i === 0 ) {
160
+ const id = objectID ( ) . toHexString ( ) ;
161
+ project . files . push ( {
162
+ name : 'assets' ,
163
+ id,
164
+ _id : id ,
165
+ children : [ ] ,
166
+ fileType : 'folder'
167
+ } ) ;
168
+ // add assets folder inside root
169
+ project . files [ 0 ] . children . push ( id ) ;
170
+ }
171
+
172
+ const fileID = objectID ( ) . toHexString ( ) ;
173
+
174
+ if ( assetName . slice ( - 5 ) === '.vert' || assetName . slice ( - 5 ) === '.frag' ) { // check if the file has .vert or .frag extension
175
+ const assetOptions = {
176
+ url : assetUrl ,
177
+ method : 'GET' ,
178
+ headers : {
179
+ ...headers ,
180
+ Authorization : `Basic ${ Buffer . from ( `${ clientId } :${ clientSecret } ` ) . toString ( 'base64' ) } `
181
+ } ,
182
+ json : true
183
+ } ;
184
+
185
+ // a function to await for the response that contains the content of asset file
186
+ const doRequest = function ( optionsAsset ) {
187
+ return new Promise ( ( ( resolve , reject ) => {
188
+ rp ( optionsAsset ) . then ( ( res ) => {
189
+ resolve ( res ) ;
190
+ } ) . catch ( ( err ) => {
191
+ reject ( err ) ;
192
+ } ) ;
193
+ } ) ) ;
194
+ } ;
195
+
196
+ assetContent = await doRequest ( assetOptions ) ;
197
+ // push to the files array of the project only when response is received
198
+ project . files . push ( {
199
+ name : assetName ,
200
+ content : assetContent ,
201
+ id : fileID ,
202
+ _id : fileID ,
203
+ children : [ ] ,
204
+ fileType : 'file'
205
+ } ) ;
206
+ console . log ( `create assets: ${ assetName } ` ) ;
207
+ // add asset file inside the newly created assets folder at index 4
208
+ project . files [ 4 ] . children . push ( fileID ) ;
209
+ } else { // for assets files that are not .vert or .frag extension
210
+ project . files . push ( {
211
+ name : assetName ,
212
+ url : `https://cdn.jsdelivr.net/gh/processing/p5.js-website@main/src/data/examples/assets/${ assetName } ` ,
213
+ id : fileID ,
214
+ _id : fileID ,
215
+ children : [ ] ,
216
+ fileType : 'file'
217
+ } ) ;
218
+ console . log ( `create assets: ${ assetName } ` ) ;
219
+ // add asset file inside the newly created assets folder at index 4
220
+ project . files [ 4 ] . children . push ( fileID ) ;
221
+ }
222
+ }
223
+ }
224
+ /* eslint-disable no-await-in-loop */
225
+ }
226
+
227
+
143
228
function createProjectsInP5user ( projectsInAllCategories ) {
144
229
const options = {
145
230
url : 'https://api.github.com/repos/processing/p5.js-website/contents/src/data/examples/assets' ,
@@ -248,87 +333,7 @@ function createProjectsInP5user(projectsInAllCategories) {
248
333
const assetsInProject = project . sketchContent . match ( / a s s e t s \/ [ \w - ] + \. [ \w ] * / g)
249
334
|| project . sketchContent . match ( / a s s e t \/ [ \w - ] * / g) || [ ] ;
250
335
251
- /* eslint-disable no-await-in-loop */
252
- 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() )
253
- const assetNamePath = assetsInProject [ i ] ;
254
- let assetName = assetNamePath . split ( 'assets/' ) [ 1 ] ;
255
- let assetUrl = '' ;
256
- let assetContent = '' ;
257
-
258
- res . forEach ( ( asset ) => {
259
- if ( asset . name === assetName || asset . name . split ( '.' ) [ 0 ] === assetName ) {
260
- assetName = asset . name ;
261
- assetUrl = asset . download_url ;
262
- }
263
- } ) ;
264
-
265
- if ( assetName !== '' ) {
266
- if ( i === 0 ) {
267
- const id = objectID ( ) . toHexString ( ) ;
268
- newProject . files . push ( {
269
- name : 'assets' ,
270
- id,
271
- _id : id ,
272
- children : [ ] ,
273
- fileType : 'folder'
274
- } ) ;
275
- // add assets folder inside root
276
- newProject . files [ 0 ] . children . push ( id ) ;
277
- }
278
-
279
- const fileID = objectID ( ) . toHexString ( ) ;
280
-
281
- if ( assetName . slice ( - 5 ) === '.vert' || assetName . slice ( - 5 ) === '.frag' ) { // check if the file has .vert or .frag extension
282
- const assetOptions = {
283
- url : assetUrl ,
284
- method : 'GET' ,
285
- headers : {
286
- ...headers ,
287
- Authorization : `Basic ${ Buffer . from ( `${ clientId } :${ clientSecret } ` ) . toString ( 'base64' ) } `
288
- } ,
289
- json : true
290
- } ;
291
-
292
- // a function to await for the response that contains the content of asset file
293
- const doRequest = function ( optionsAsset ) {
294
- return new Promise ( ( ( resolve , reject ) => {
295
- rp ( optionsAsset ) . then ( ( response ) => {
296
- resolve ( response ) ;
297
- } ) . catch ( ( error ) => {
298
- reject ( error ) ;
299
- } ) ;
300
- } ) ) ;
301
- } ;
302
-
303
- assetContent = await doRequest ( assetOptions ) ;
304
- // push to the files array of the project only when response is received
305
- newProject . files . push ( {
306
- name : assetName ,
307
- content : assetContent ,
308
- id : fileID ,
309
- _id : fileID ,
310
- children : [ ] ,
311
- fileType : 'file'
312
- } ) ;
313
- console . log ( `create assets: ${ assetName } ` ) ;
314
- // add asset file inside the newly created assets folder at index 4
315
- newProject . files [ 4 ] . children . push ( fileID ) ;
316
- } else { // for assets files that are not .vert or .frag extension
317
- newProject . files . push ( {
318
- name : assetName ,
319
- url : `https://cdn.jsdelivr.net/gh/processing/p5.js-website@main/src/data/examples/assets/${ assetName } ` ,
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
- }
329
- }
330
- }
331
- /* eslint-disable no-await-in-loop */
336
+ await addAssetsToProject ( assetsInProject , res , newProject ) ;
332
337
333
338
newProject . save ( ( saveErr , savedProject ) => {
334
339
if ( saveErr ) throw saveErr ;
0 commit comments