Skip to content

Fix/cant see shaders code #1690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 90 additions & 40 deletions server/scripts/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import User from '../models/user';
import Project from '../models/project';

const defaultHTML =
`<!DOCTYPE html>
`<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script>
Expand All @@ -23,7 +23,7 @@ const defaultHTML =
`;

const defaultCSS =
`html, body {
`html, body {
margin: 0;
padding: 0;
}
Expand Down Expand Up @@ -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',
Expand All @@ -156,7 +241,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();
Expand Down Expand Up @@ -248,43 +333,7 @@ function createProjectsInP5user(projectsInAllCategories) {
const assetsInProject = project.sketchContent.match(/assets\/[\w-]+\.[\w]*/g)
|| project.sketchContent.match(/asset\/[\w-]*/g) || [];

assetsInProject.forEach((assetNamePath, i) => {
let assetName = assetNamePath.split('assets/')[1];

res.forEach((asset) => {
if (asset.name === assetName || asset.name.split('.')[0] === assetName) {
assetName = asset.name;
}
});

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();
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);
}
});
await addAssetsToProject(assetsInProject, res, newProject);

newProject.save((saveErr, savedProject) => {
if (saveErr) throw saveErr;
Expand All @@ -304,6 +353,7 @@ function createProjectsInP5user(projectsInAllCategories) {
}

function getp5User() {
console.log('Getting p5 user');
User.findOne({ username: 'p5' }, (err, user) => {
if (err) throw err;

Expand Down