Skip to content

Refactor examples.js to use async/await #1694

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 7 commits into from
Dec 4, 2020
Merged
Changes from 4 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
273 changes: 135 additions & 138 deletions server/scripts/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Q from 'q';
import mongoose from 'mongoose';
import objectID from 'bson-objectid';
import shortid from 'shortid';
import eachSeries from 'async/eachSeries';
import User from '../models/user';
import Project from '../models/project';

Expand Down Expand Up @@ -46,7 +45,7 @@ mongoose.connection.on('error', () => {
process.exit(1);
});

function getCategories() {
async function getCategories() {
const categories = [];
const options = {
url: 'https://api.github.com/repos/processing/p5.js-website/contents/src/data/examples/en',
Expand All @@ -57,23 +56,23 @@ function getCategories() {
},
json: true
};
return rp(options).then((res) => {
try {
const res = await rp(options);
res.forEach((metadata) => {
let category = '';
for (let j = 1; j < metadata.name.split('_').length; j += 1) {
category += `${metadata.name.split('_')[j]} `;
}
categories.push({ url: metadata.url, name: category.trim() });
});

return categories;
}).catch((err) => {
throw err;
});
} catch (error) {
throw error;
}
}

function getSketchesInCategories(categories) {
return Q.all(categories.map((category) => {
return Q.all(categories.map(async (category) => {
const options = {
url: `${category.url.replace('?ref=main', '')}`,
method: 'GET',
Expand All @@ -83,8 +82,8 @@ function getSketchesInCategories(categories) {
},
json: true
};

return rp(options).then((res) => {
try {
const res = await rp(options);
const projectsInOneCategory = [];
res.forEach((example) => {
let projectName;
Expand All @@ -103,14 +102,14 @@ function getSketchesInCategories(categories) {
}
});
return projectsInOneCategory;
}).catch((err) => {
throw err;
});
} catch (error) {
throw error;
}
}));
}

function getSketchContent(projectsInAllCategories) {
return Q.all(projectsInAllCategories.map(projectsInOneCategory => Q.all(projectsInOneCategory.map((project) => {
return Q.all(projectsInAllCategories.map(projectsInOneCategory => Q.all(projectsInOneCategory.map(async (project) => {
const options = {
url: `${project.sketchUrl.replace('?ref=main', '')}`,
method: 'GET',
Expand All @@ -119,8 +118,8 @@ function getSketchContent(projectsInAllCategories) {
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`
}
};

return rp(options).then((res) => {
try {
const res = await rp(options);
const noNumberprojectName = project.projectName.replace(/(\d+)/g, '');
if (noNumberprojectName === 'Instance Mode: Instance Container ') {
for (let i = 0; i < 4; i += 1) {
Expand All @@ -134,9 +133,9 @@ function getSketchContent(projectsInAllCategories) {
project.sketchContent = res;
}
return project;
}).catch((err) => {
throw err;
});
} catch (error) {
throw error;
}
}))));
}

Expand Down Expand Up @@ -183,14 +182,13 @@ async function addAssetsToProject(assets, response, project) {
};

// 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);
});
}));
const doRequest = async (optionsAsset) => {
try {
const res = await rp(optionsAsset);
return res;
} catch (error) {
throw error;
}
};

assetContent = await doRequest(assetOptions);
Expand Down Expand Up @@ -225,7 +223,7 @@ async function addAssetsToProject(assets, response, project) {
}


function createProjectsInP5user(projectsInAllCategories) {
async function createProjectsInP5user(projectsInAllCategories) {
const options = {
url: 'https://api.github.com/repos/processing/p5.js-website/contents/src/data/examples/assets',
method: 'GET',
Expand All @@ -236,125 +234,123 @@ function createProjectsInP5user(projectsInAllCategories) {
json: true
};

rp(options).then((res) => {
try {
const res = await rp(options);
User.findOne({ username: 'p5' }, (err, user) => {
if (err) throw err;

eachSeries(projectsInAllCategories, (projectsInOneCategory, categoryCallback) => {
eachSeries(projectsInOneCategory, async (project, projectCallback) => {
let newProject;
const a = objectID().toHexString();
const b = objectID().toHexString();
const c = objectID().toHexString();
const r = objectID().toHexString();
const noNumberprojectName = project.projectName.replace(/(\d+)/g, '');
if (noNumberprojectName === 'Instance Mode: Instance Container ') {
newProject = new Project({
name: project.projectName,
user: user._id,
files: [
{
name: 'root',
id: r,
_id: r,
children: [a, b, c],
fileType: 'folder'
},
{
name: 'sketch.js',
content: '// Instance Mode: Instance Container, please check its index.html file',
id: a,
_id: a,
fileType: 'file',
children: []
},
{
name: 'index.html',
content: project.sketchContent,
isSelectedFile: true,
id: b,
_id: b,
fileType: 'file',
children: []
},
{
name: 'style.css',
content: defaultCSS,
id: c,
_id: c,
fileType: 'file',
children: []
}
],
_id: shortid.generate()
});
} else {
newProject = new Project({
name: project.projectName,
user: user._id,
files: [
{
name: 'root',
id: r,
_id: r,
children: [a, b, c],
fileType: 'folder'
},
{
name: 'sketch.js',
content: project.sketchContent,
id: a,
_id: a,
isSelectedFile: true,
fileType: 'file',
children: []
},
{
name: 'index.html',
content: defaultHTML,
id: b,
_id: b,
fileType: 'file',
children: []
},
{
name: 'style.css',
content: defaultCSS,
id: c,
_id: c,
fileType: 'file',
children: []
}
],
_id: shortid.generate()
});
}
Q.all(projectsInAllCategories.map(projectsInOneCategory => Q.all(projectsInOneCategory.map(async (project) => {
let newProject;
const a = objectID().toHexString();
const b = objectID().toHexString();
const c = objectID().toHexString();
const r = objectID().toHexString();
const noNumberprojectName = project.projectName.replace(/(\d+)/g, '');
if (noNumberprojectName === 'Instance Mode: Instance Container ') {
newProject = new Project({
name: project.projectName,
user: user._id,
files: [
{
name: 'root',
id: r,
_id: r,
children: [a, b, c],
fileType: 'folder'
},
{
name: 'sketch.js',
content: '// Instance Mode: Instance Container, please check its index.html file',
id: a,
_id: a,
fileType: 'file',
children: []
},
{
name: 'index.html',
content: project.sketchContent,
isSelectedFile: true,
id: b,
_id: b,
fileType: 'file',
children: []
},
{
name: 'style.css',
content: defaultCSS,
id: c,
_id: c,
fileType: 'file',
children: []
}
],
_id: shortid.generate()
});
} else {
newProject = new Project({
name: project.projectName,
user: user._id,
files: [
{
name: 'root',
id: r,
_id: r,
children: [a, b, c],
fileType: 'folder'
},
{
name: 'sketch.js',
content: project.sketchContent,
id: a,
_id: a,
isSelectedFile: true,
fileType: 'file',
children: []
},
{
name: 'index.html',
content: defaultHTML,
id: b,
_id: b,
fileType: 'file',
children: []
},
{
name: 'style.css',
content: defaultCSS,
id: c,
_id: c,
fileType: 'file',
children: []
}
],
_id: shortid.generate()
});
}

const assetsInProject = project.sketchContent.match(/assets\/[\w-]+\.[\w]*/g)
|| project.sketchContent.match(/asset\/[\w-]*/g) || [];
const assetsInProject = project.sketchContent.match(/assets\/[\w-]+\.[\w]*/g)
|| project.sketchContent.match(/asset\/[\w-]*/g) || [];

try {
await addAssetsToProject(assetsInProject, res, newProject);
} catch (error) {
throw error;
}

newProject.save((saveErr, savedProject) => {
if (saveErr) throw saveErr;
console.log(`Created a new project in p5 user: ${savedProject.name}`);
projectCallback();
});
}, (categoryErr) => {
categoryCallback();
newProject.save((saveErr, savedProject) => {
if (saveErr) throw saveErr;
console.log(`Created a new project in p5 user: ${savedProject.name}`);
});
}, (examplesErr) => {
process.exit();
});
}))));
});
}).catch((err) => {
throw err;
});
} catch (error) {
throw error;
}
}

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

let p5User = user;
Expand All @@ -380,10 +376,11 @@ function getp5User() {
});
});

return getCategories()
.then(getSketchesInCategories)
.then(getSketchContent)
.then(createProjectsInP5user);
const categories = await getCategories();
const sketchesInCategories = await getSketchesInCategories(categories);
const sketchContent = await getSketchContent(sketchesInCategories);
const projects = createProjectsInP5user(sketchContent);
return projects;
});
}

Expand Down