2016-12-02 00:57:50 +01:00
|
|
|
import rp from 'request-promise';
|
|
|
|
import Q from 'q';
|
|
|
|
import mongoose from 'mongoose';
|
2017-01-11 21:17:10 +01:00
|
|
|
import objectID from 'bson-objectid';
|
2016-12-02 00:57:50 +01:00
|
|
|
import shortid from 'shortid';
|
2017-02-22 20:29:35 +01:00
|
|
|
import eachSeries from 'async/eachSeries';
|
2016-12-02 00:57:50 +01:00
|
|
|
import User from './models/user';
|
|
|
|
import Project from './models/project';
|
|
|
|
|
|
|
|
const defaultHTML =
|
|
|
|
`<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2018-05-02 21:44:16 +02:00
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.min.js"></script>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/addons/p5.dom.min.js"></script>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/addons/p5.sound.min.js"></script>
|
2016-12-02 00:57:50 +01:00
|
|
|
<link rel="stylesheet" type="text/css" href="style.css">
|
2017-10-30 20:36:09 +01:00
|
|
|
<meta charset="utf-8" />
|
2016-12-02 00:57:50 +01:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<script src="sketch.js"></script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
|
|
|
|
const defaultCSS =
|
|
|
|
`html, body {
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
const clientId = process.env.GITHUB_ID;
|
|
|
|
const clientSecret = process.env.GITHUB_SECRET;
|
2016-12-02 00:57:50 +01:00
|
|
|
|
2017-01-11 21:17:10 +01:00
|
|
|
const headers = { 'User-Agent': 'p5js-web-editor/0.0.1' };
|
2016-12-02 00:57:50 +01:00
|
|
|
|
|
|
|
mongoose.connect(process.env.MONGO_URL);
|
|
|
|
mongoose.connection.on('error', () => {
|
|
|
|
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
function getCategories() {
|
2017-02-22 20:29:35 +01:00
|
|
|
const categories = [];
|
2016-12-02 00:57:50 +01:00
|
|
|
const options = {
|
2017-02-22 20:29:35 +01:00
|
|
|
url: `https://api.github.com/repos/processing/p5.js-website/contents/dist/assets/examples/en?client_id=${
|
|
|
|
clientId}&client_secret=${clientSecret}`,
|
2016-12-02 00:57:50 +01:00
|
|
|
method: 'GET',
|
2018-03-02 00:13:19 +01:00
|
|
|
headers,
|
|
|
|
json: true
|
2016-12-02 00:57:50 +01:00
|
|
|
};
|
2017-02-22 20:29:35 +01:00
|
|
|
return rp(options).then((res) => {
|
2018-03-02 00:13:19 +01:00
|
|
|
res.forEach((metadata) => {
|
2016-12-02 00:57:50 +01:00
|
|
|
let category = '';
|
2017-02-22 20:29:35 +01:00
|
|
|
for (let j = 1; j < metadata.name.split('_').length; j += 1) {
|
|
|
|
category += `${metadata.name.split('_')[j]} `;
|
2016-12-02 00:57:50 +01:00
|
|
|
}
|
2017-01-11 21:17:10 +01:00
|
|
|
categories.push({ url: metadata.url, name: category });
|
2016-12-02 00:57:50 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return categories;
|
2017-02-22 20:29:35 +01:00
|
|
|
}).catch((err) => {
|
2016-12-02 00:57:50 +01:00
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSketchesInCategories(categories) {
|
2017-02-22 20:29:35 +01:00
|
|
|
return Q.all(categories.map((category) => {
|
2016-12-02 00:57:50 +01:00
|
|
|
const options = {
|
2017-02-22 20:29:35 +01:00
|
|
|
url: `${category.url.replace('?ref=master', '')}?client_id=${clientId}&client_secret=${clientSecret}`,
|
2016-12-02 00:57:50 +01:00
|
|
|
method: 'GET',
|
2018-03-02 00:13:19 +01:00
|
|
|
headers,
|
|
|
|
json: true
|
2016-12-02 00:57:50 +01:00
|
|
|
};
|
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
return rp(options).then((res) => {
|
|
|
|
const projectsInOneCategory = [];
|
2018-03-02 00:13:19 +01:00
|
|
|
res.forEach((example) => {
|
2016-12-02 00:57:50 +01:00
|
|
|
let projectName;
|
2016-12-09 18:19:02 +01:00
|
|
|
if (example.name === '02_Instance_Container.js') {
|
2017-02-22 20:29:35 +01:00
|
|
|
for (let i = 1; i < 5; i += 1) {
|
|
|
|
const instanceProjectName = `${category.name}: Instance Container ${i}`;
|
2017-01-11 21:17:10 +01:00
|
|
|
projectsInOneCategory.push({ sketchUrl: example.download_url, projectName: instanceProjectName });
|
2016-12-09 18:19:02 +01:00
|
|
|
}
|
2016-12-02 00:57:50 +01:00
|
|
|
} else {
|
2016-12-09 18:19:02 +01:00
|
|
|
if (example.name.split('_')[1]) {
|
2017-02-22 20:29:35 +01:00
|
|
|
projectName = `${category.name}: ${example.name.split('_').slice(1).join(' ').replace('.js', '')}`;
|
2016-12-09 18:19:02 +01:00
|
|
|
} else {
|
2017-02-22 20:29:35 +01:00
|
|
|
projectName = `${category.name}: ${example.name.replace('.js', '')}`;
|
2016-12-09 18:19:02 +01:00
|
|
|
}
|
2017-01-11 21:17:10 +01:00
|
|
|
projectsInOneCategory.push({ sketchUrl: example.download_url, projectName });
|
2016-12-02 00:57:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return projectsInOneCategory;
|
2017-02-22 20:29:35 +01:00
|
|
|
}).catch((err) => {
|
2016-12-02 00:57:50 +01:00
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSketchContent(projectsInAllCategories) {
|
2017-02-22 20:29:35 +01:00
|
|
|
return Q.all(projectsInAllCategories.map(projectsInOneCategory => Q.all(projectsInOneCategory.map((project) => {
|
|
|
|
const options = {
|
|
|
|
url: `${project.sketchUrl.replace('?ref=master', '')}?client_id=${clientId}&client_secret=${clientSecret}`,
|
|
|
|
method: 'GET',
|
|
|
|
headers
|
|
|
|
};
|
2016-12-02 00:57:50 +01:00
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
return rp(options).then((res) => {
|
|
|
|
const noNumberprojectName = project.projectName.replace(/(\d+)/g, '');
|
|
|
|
if (noNumberprojectName === 'Instance Mode : Instance Container ') {
|
|
|
|
for (let i = 0; i < 4; i += 1) {
|
|
|
|
const splitedRes = `${res.split('*/')[1].split('</html>')[i]}</html>\n`;
|
2017-06-13 22:47:36 +02:00
|
|
|
project.sketchContent = splitedRes.replace('p5.js',
|
|
|
|
'https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.4/p5.min.js');
|
2016-12-09 18:19:02 +01:00
|
|
|
}
|
2017-02-22 20:29:35 +01:00
|
|
|
} else {
|
|
|
|
project.sketchContent = res;
|
|
|
|
}
|
|
|
|
return project;
|
|
|
|
}).catch((err) => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}))));
|
2016-12-02 00:57:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function createProjectsInP5user(projectsInAllCategories) {
|
|
|
|
const options = {
|
2017-02-22 20:29:35 +01:00
|
|
|
url: `https://api.github.com/repos/processing/p5.js-website/contents/dist/assets/examples/assets?client_id=${
|
|
|
|
clientId}&client_secret=${clientSecret}`,
|
2016-12-02 00:57:50 +01:00
|
|
|
method: 'GET',
|
2018-03-02 00:13:19 +01:00
|
|
|
headers,
|
|
|
|
json: true
|
2016-12-02 00:57:50 +01:00
|
|
|
};
|
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
rp(options).then((res) => {
|
2018-05-02 21:15:33 +02:00
|
|
|
User.findOne({ username: process.env.EXAMPLE_USERNAME }, (err, user) => {
|
2016-12-02 00:57:50 +01:00
|
|
|
if (err) throw err;
|
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
eachSeries(projectsInAllCategories, (projectsInOneCategory, categoryCallback) => {
|
|
|
|
eachSeries(projectsInOneCategory, (project, projectCallback) => {
|
2016-12-09 18:19:02 +01:00
|
|
|
let newProject;
|
2017-01-24 23:47:20 +01:00
|
|
|
const a = objectID().toHexString();
|
|
|
|
const b = objectID().toHexString();
|
|
|
|
const c = objectID().toHexString();
|
|
|
|
const r = objectID().toHexString();
|
2016-12-09 18:19:02 +01:00
|
|
|
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,
|
2016-12-09 18:28:20 +01:00
|
|
|
isSelectedFile: true,
|
2016-12-09 18:19:02 +01:00
|
|
|
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()
|
|
|
|
});
|
|
|
|
}
|
2016-12-02 00:57:50 +01:00
|
|
|
|
2017-06-13 22:47:36 +02:00
|
|
|
const assetsInProject = project.sketchContent.match(/assets\/[\w-]+\.[\w]*/g)
|
2018-05-02 21:29:38 +02:00
|
|
|
|| project.sketchContent.match(/asset\/[\w-]*/g) || [];
|
2016-12-02 00:57:50 +01:00
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
assetsInProject.forEach((assetNamePath, i) => {
|
|
|
|
let assetName = assetNamePath.split('assets/')[1];
|
2016-12-02 00:57:50 +01:00
|
|
|
|
2018-03-02 00:13:19 +01:00
|
|
|
res.forEach((asset) => {
|
2016-12-02 00:57:50 +01:00
|
|
|
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://rawgit.com/processing/p5.js-website/master/dist/assets/examples/assets/${assetName}`,
|
|
|
|
id: fileID,
|
|
|
|
_id: fileID,
|
|
|
|
children: [],
|
|
|
|
fileType: 'file'
|
|
|
|
});
|
2017-02-22 20:29:35 +01:00
|
|
|
console.log(`create assets: ${assetName}`);
|
2016-12-02 00:57:50 +01:00
|
|
|
// add asset file inside the newly created assets folder at index 4
|
|
|
|
newProject.files[4].children.push(fileID);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
newProject.save((saveErr, savedProject) => {
|
|
|
|
if (saveErr) throw saveErr;
|
|
|
|
console.log(`Created a new project in p5 user: ${savedProject.name}`);
|
2016-12-02 00:57:50 +01:00
|
|
|
projectCallback();
|
|
|
|
});
|
2017-02-22 20:29:35 +01:00
|
|
|
}, (categoryErr) => {
|
2016-12-02 00:57:50 +01:00
|
|
|
categoryCallback();
|
|
|
|
});
|
2017-02-22 20:29:35 +01:00
|
|
|
}, (examplesErr) => {
|
2016-12-02 00:57:50 +01:00
|
|
|
process.exit();
|
|
|
|
});
|
|
|
|
});
|
2017-02-22 20:29:35 +01:00
|
|
|
}).catch((err) => {
|
2016-12-02 00:57:50 +01:00
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
2017-02-22 20:29:35 +01:00
|
|
|
|
|
|
|
function getp5User() {
|
2018-05-02 21:15:33 +02:00
|
|
|
User.findOne({ username: process.env.EXAMPLE_USERNAME }, (err, user) => {
|
2017-02-22 20:29:35 +01:00
|
|
|
if (err) throw err;
|
|
|
|
|
|
|
|
let p5User = user;
|
|
|
|
if (!p5User) {
|
|
|
|
p5User = new User({
|
2018-05-02 21:15:33 +02:00
|
|
|
username: process.env.EXAMPLE_USERNAME,
|
2018-05-02 21:16:20 +02:00
|
|
|
email: process.env.EXAMPLE_USER_EMAIL,
|
|
|
|
password: process.env.EXAMPLE_USER_PASSWORD
|
2017-02-22 20:29:35 +01:00
|
|
|
});
|
|
|
|
p5User.save((saveErr) => {
|
|
|
|
if (saveErr) throw saveErr;
|
|
|
|
console.log(`Created a user p5${p5User}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Project.find({ user: p5User._id }, (projectsErr, projects) => {
|
|
|
|
// if there are already some sketches, delete them
|
|
|
|
console.log('Deleting old projects...');
|
|
|
|
projects.forEach((project) => {
|
|
|
|
Project.remove({ _id: project._id }, (removeErr) => {
|
|
|
|
if (removeErr) throw removeErr;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return getCategories()
|
|
|
|
.then(getSketchesInCategories)
|
|
|
|
.then(getSketchContent)
|
|
|
|
.then(createProjectsInP5user);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getp5User();
|