@@ -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
}
@@ -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' ,
@@ -156,7 +241,7 @@ function createProjectsInP5user(projectsInAllCategories) {
156
241
if ( err ) throw err ;
157
242
158
243
eachSeries ( projectsInAllCategories , ( projectsInOneCategory , categoryCallback ) => {
159
- eachSeries ( projectsInOneCategory , ( project , projectCallback ) => {
244
+ eachSeries ( projectsInOneCategory , async ( project , projectCallback ) => {
160
245
let newProject ;
161
246
const a = objectID ( ) . toHexString ( ) ;
162
247
const b = objectID ( ) . toHexString ( ) ;
@@ -248,43 +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
- assetsInProject . forEach ( ( assetNamePath , i ) => {
252
- let assetName = assetNamePath . split ( 'assets/' ) [ 1 ] ;
253
-
254
- res . forEach ( ( asset ) => {
255
- if ( asset . name === assetName || asset . name . split ( '.' ) [ 0 ] === assetName ) {
256
- assetName = asset . name ;
257
- }
258
- } ) ;
259
-
260
- if ( assetName !== '' ) {
261
- if ( i === 0 ) {
262
- const id = objectID ( ) . toHexString ( ) ;
263
- newProject . files . push ( {
264
- name : 'assets' ,
265
- id,
266
- _id : id ,
267
- children : [ ] ,
268
- fileType : 'folder'
269
- } ) ;
270
- // add assets folder inside root
271
- newProject . files [ 0 ] . children . push ( id ) ;
272
- }
273
-
274
- 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 ) ;
286
- }
287
- } ) ;
336
+ await addAssetsToProject ( assetsInProject , res , newProject ) ;
288
337
289
338
newProject . save ( ( saveErr , savedProject ) => {
290
339
if ( saveErr ) throw saveErr ;
@@ -304,6 +353,7 @@ function createProjectsInP5user(projectsInAllCategories) {
304
353
}
305
354
306
355
function getp5User ( ) {
356
+ console . log ( 'Getting p5 user' ) ;
307
357
User . findOne ( { username : 'p5' } , ( err , user ) => {
308
358
if ( err ) throw err ;
309
359
0 commit comments