From 204fa829d208a2a7a451ce4e59e932ab754ec863 Mon Sep 17 00:00:00 2001 From: Long Phan Date: Sun, 22 Nov 2020 13:37:11 +0400 Subject: [PATCH 1/5] add text content to .vert and .frag files --- server/scripts/examples.js | 89 +++++++++++++++++++++++++++++++------- 1 file changed, 73 insertions(+), 16 deletions(-) diff --git a/server/scripts/examples.js b/server/scripts/examples.js index 72552fd48e..984cdd8c14 100644 --- a/server/scripts/examples.js +++ b/server/scripts/examples.js @@ -8,7 +8,7 @@ import User from '../models/user'; import Project from '../models/project'; const defaultHTML = -` + ` @@ -23,7 +23,7 @@ const defaultHTML = `; const defaultCSS = -`html, body { + `html, body { margin: 0; padding: 0; } @@ -132,6 +132,7 @@ function getSketchContent(projectsInAllCategories) { } } else { project.sketchContent = res; + // console.log("options.url: ", options.url); } return project; }).catch((err) => { @@ -140,6 +141,17 @@ function getSketchContent(projectsInAllCategories) { })))); } +// a function to await for the response that contains the content of asset file +function doRequest(options) { + return new Promise(((resolve, reject) => { + rp(options).then((response) => { + resolve(response); + }).catch((error) => { + reject(error); + }); + })); +} + function createProjectsInP5user(projectsInAllCategories) { const options = { url: 'https://api.github.com/repos/processing/p5.js-website/contents/src/data/examples/assets', @@ -156,7 +168,7 @@ function createProjectsInP5user(projectsInAllCategories) { if (err) throw err; eachSeries(projectsInAllCategories, (projectsInOneCategory, categoryCallback) => { - eachSeries(projectsInOneCategory, (project, projectCallback) => { + eachSeries(projectsInOneCategory, async (project, projectCallback) => { let newProject; const a = objectID().toHexString(); const b = objectID().toHexString(); @@ -248,12 +260,17 @@ function createProjectsInP5user(projectsInAllCategories) { const assetsInProject = project.sketchContent.match(/assets\/[\w-]+\.[\w]*/g) || project.sketchContent.match(/asset\/[\w-]*/g) || []; - assetsInProject.forEach((assetNamePath, i) => { + /* eslint-disable no-await-in-loop */ + 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() ) + const assetNamePath = assetsInProject[i]; let assetName = assetNamePath.split('assets/')[1]; + let assetUrl = ''; + let assetContent = ''; res.forEach((asset) => { if (asset.name === assetName || asset.name.split('.')[0] === assetName) { assetName = asset.name; + assetUrl = asset.download_url; } }); @@ -272,19 +289,58 @@ function createProjectsInP5user(projectsInAllCategories) { } const fileID = objectID().toHexString(); - newProject.files.push({ - name: assetName, - url: `https://cdn.jsdelivr.net/gh/processing/p5.js-website@main/src/data/examples/assets/${assetName}`, - id: fileID, - _id: fileID, - children: [], - fileType: 'file' - }); - console.log(`create assets: ${assetName}`); - // add asset file inside the newly created assets folder at index 4 - newProject.files[4].children.push(fileID); + + if (assetName.slice(-5) === '.vert' || assetName.slice(-5) === '.frag') { // check if the file has .vert or .frag extension + const assetOptions = { + url: assetUrl, + method: 'GET', + headers: { + ...headers, + Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}` + }, + json: true + }; + + // //a function to await for the response that contains the content of asset file + // function doRequest(options) { + // return new Promise(function (resolve, reject) { + // rp(options).then((response) => { + // resolve(response); + // }).catch((error) => { + // reject(error); + // }) + // }) + // } + + assetContent = await doRequest(assetOptions); + // push to the files array of the project only when response is received + newProject.files.push({ + name: assetName, + content: assetContent, + id: fileID, + _id: fileID, + children: [], + fileType: 'file' + }); + console.log(`create assets: ${assetName}`); + // add asset file inside the newly created assets folder at index 4 + newProject.files[4].children.push(fileID); + } else { // for assets files that are not .vert or .frag extension + newProject.files.push({ + name: assetName, + url: `https://cdn.jsdelivr.net/gh/processing/p5.js-website@main/src/data/examples/assets/${assetName}`, + id: fileID, + _id: fileID, + children: [], + fileType: 'file' + }); + console.log(`create assets: ${assetName}`); + // add asset file inside the newly created assets folder at index 4 + newProject.files[4].children.push(fileID); + } } - }); + } + /* eslint-disable no-await-in-loop */ newProject.save((saveErr, savedProject) => { if (saveErr) throw saveErr; @@ -304,6 +360,7 @@ function createProjectsInP5user(projectsInAllCategories) { } function getp5User() { + console.log('Getting p5 user'); User.findOne({ username: 'p5' }, (err, user) => { if (err) throw err; From 0dc63222ccccac1a1da2894b0c4082a71fa024b9 Mon Sep 17 00:00:00 2001 From: Long Phan Date: Sun, 22 Nov 2020 13:46:45 +0400 Subject: [PATCH 2/5] add text content to .vert and .frag files --- server/scripts/examples.js | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/server/scripts/examples.js b/server/scripts/examples.js index 984cdd8c14..8844b0d148 100644 --- a/server/scripts/examples.js +++ b/server/scripts/examples.js @@ -141,17 +141,6 @@ function getSketchContent(projectsInAllCategories) { })))); } -// a function to await for the response that contains the content of asset file -function doRequest(options) { - return new Promise(((resolve, reject) => { - rp(options).then((response) => { - resolve(response); - }).catch((error) => { - reject(error); - }); - })); -} - function createProjectsInP5user(projectsInAllCategories) { const options = { url: 'https://api.github.com/repos/processing/p5.js-website/contents/src/data/examples/assets', @@ -301,16 +290,16 @@ function createProjectsInP5user(projectsInAllCategories) { json: true }; - // //a function to await for the response that contains the content of asset file - // function doRequest(options) { - // return new Promise(function (resolve, reject) { - // rp(options).then((response) => { - // resolve(response); - // }).catch((error) => { - // reject(error); - // }) - // }) - // } + // a function to await for the response that contains the content of asset file + const doRequest = function (_options) { + return new Promise(((resolve, reject) => { + rp(_options).then((response) => { + resolve(response); + }).catch((error) => { + reject(error); + }); + })); + }; assetContent = await doRequest(assetOptions); // push to the files array of the project only when response is received From a102211c0965fa5f733511923ebd424c2376be37 Mon Sep 17 00:00:00 2001 From: Long Phan Date: Sun, 22 Nov 2020 13:51:32 +0400 Subject: [PATCH 3/5] add text content to .vert and .frag files --- server/scripts/examples.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/scripts/examples.js b/server/scripts/examples.js index 8844b0d148..8186cb12ba 100644 --- a/server/scripts/examples.js +++ b/server/scripts/examples.js @@ -291,9 +291,9 @@ function createProjectsInP5user(projectsInAllCategories) { }; // a function to await for the response that contains the content of asset file - const doRequest = function (_options) { + const doRequest = function (optionsAsset) { return new Promise(((resolve, reject) => { - rp(_options).then((response) => { + rp(optionsAsset).then((response) => { resolve(response); }).catch((error) => { reject(error); From 60dc9df38ca2f71ac58f9376e8417674d16f5e51 Mon Sep 17 00:00:00 2001 From: Long Phan Date: Sun, 22 Nov 2020 13:58:11 +0400 Subject: [PATCH 4/5] add text content to .vert and .frag files --- server/scripts/examples.js | 1 - 1 file changed, 1 deletion(-) diff --git a/server/scripts/examples.js b/server/scripts/examples.js index 8186cb12ba..7170c8bfd8 100644 --- a/server/scripts/examples.js +++ b/server/scripts/examples.js @@ -132,7 +132,6 @@ function getSketchContent(projectsInAllCategories) { } } else { project.sketchContent = res; - // console.log("options.url: ", options.url); } return project; }).catch((err) => { From a13e6ec23b4a10ed7ea5cfe796233904af67433b Mon Sep 17 00:00:00 2001 From: Long Phan Date: Tue, 24 Nov 2020 16:37:00 +0400 Subject: [PATCH 5/5] add addAssetsToProject() to examples.js --- server/scripts/examples.js | 167 +++++++++++++++++++------------------ 1 file changed, 86 insertions(+), 81 deletions(-) diff --git a/server/scripts/examples.js b/server/scripts/examples.js index 7170c8bfd8..32c9f59530 100644 --- a/server/scripts/examples.js +++ b/server/scripts/examples.js @@ -140,6 +140,91 @@ function getSketchContent(projectsInAllCategories) { })))); } +async function addAssetsToProject(assets, response, project) { + /* eslint-disable no-await-in-loop */ + 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() ) + const assetNamePath = assets[i]; + let assetName = assetNamePath.split('assets/')[1]; + let assetUrl = ''; + let assetContent = ''; + + response.forEach((asset) => { + if (asset.name === assetName || asset.name.split('.')[0] === assetName) { + assetName = asset.name; + assetUrl = asset.download_url; + } + }); + + if (assetName !== '') { + if (i === 0) { + const id = objectID().toHexString(); + project.files.push({ + name: 'assets', + id, + _id: id, + children: [], + fileType: 'folder' + }); + // add assets folder inside root + project.files[0].children.push(id); + } + + const fileID = objectID().toHexString(); + + if (assetName.slice(-5) === '.vert' || assetName.slice(-5) === '.frag') { // check if the file has .vert or .frag extension + const assetOptions = { + url: assetUrl, + method: 'GET', + headers: { + ...headers, + Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}` + }, + json: true + }; + + // a function to await for the response that contains the content of asset file + const doRequest = function (optionsAsset) { + return new Promise(((resolve, reject) => { + rp(optionsAsset).then((res) => { + resolve(res); + }).catch((err) => { + reject(err); + }); + })); + }; + + assetContent = await doRequest(assetOptions); + // push to the files array of the project only when response is received + project.files.push({ + name: assetName, + content: assetContent, + id: fileID, + _id: fileID, + children: [], + fileType: 'file' + }); + console.log(`create assets: ${assetName}`); + // add asset file inside the newly created assets folder at index 4 + project.files[4].children.push(fileID); + } else { // for assets files that are not .vert or .frag extension + project.files.push({ + name: assetName, + url: `https://cdn.jsdelivr.net/gh/processing/p5.js-website@main/src/data/examples/assets/${assetName}`, + id: fileID, + _id: fileID, + children: [], + fileType: 'file' + }); + console.log(`create assets: ${assetName}`); + // add asset file inside the newly created assets folder at index 4 + project.files[4].children.push(fileID); + } + } + } + /* eslint-disable no-await-in-loop */ +} + + function createProjectsInP5user(projectsInAllCategories) { const options = { url: 'https://api.github.com/repos/processing/p5.js-website/contents/src/data/examples/assets', @@ -248,87 +333,7 @@ function createProjectsInP5user(projectsInAllCategories) { const assetsInProject = project.sketchContent.match(/assets\/[\w-]+\.[\w]*/g) || project.sketchContent.match(/asset\/[\w-]*/g) || []; - /* eslint-disable no-await-in-loop */ - 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() ) - const assetNamePath = assetsInProject[i]; - let assetName = assetNamePath.split('assets/')[1]; - let assetUrl = ''; - let assetContent = ''; - - res.forEach((asset) => { - if (asset.name === assetName || asset.name.split('.')[0] === assetName) { - assetName = asset.name; - assetUrl = asset.download_url; - } - }); - - if (assetName !== '') { - if (i === 0) { - const id = objectID().toHexString(); - newProject.files.push({ - name: 'assets', - id, - _id: id, - children: [], - fileType: 'folder' - }); - // add assets folder inside root - newProject.files[0].children.push(id); - } - - const fileID = objectID().toHexString(); - - if (assetName.slice(-5) === '.vert' || assetName.slice(-5) === '.frag') { // check if the file has .vert or .frag extension - const assetOptions = { - url: assetUrl, - method: 'GET', - headers: { - ...headers, - Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}` - }, - json: true - }; - - // a function to await for the response that contains the content of asset file - const doRequest = function (optionsAsset) { - return new Promise(((resolve, reject) => { - rp(optionsAsset).then((response) => { - resolve(response); - }).catch((error) => { - reject(error); - }); - })); - }; - - assetContent = await doRequest(assetOptions); - // push to the files array of the project only when response is received - newProject.files.push({ - name: assetName, - content: assetContent, - id: fileID, - _id: fileID, - children: [], - fileType: 'file' - }); - console.log(`create assets: ${assetName}`); - // add asset file inside the newly created assets folder at index 4 - newProject.files[4].children.push(fileID); - } else { // for assets files that are not .vert or .frag extension - newProject.files.push({ - name: assetName, - url: `https://cdn.jsdelivr.net/gh/processing/p5.js-website@main/src/data/examples/assets/${assetName}`, - id: fileID, - _id: fileID, - children: [], - fileType: 'file' - }); - console.log(`create assets: ${assetName}`); - // add asset file inside the newly created assets folder at index 4 - newProject.files[4].children.push(fileID); - } - } - } - /* eslint-disable no-await-in-loop */ + await addAssetsToProject(assetsInProject, res, newProject); newProject.save((saveErr, savedProject) => { if (saveErr) throw saveErr;