2016-12-01 23:57:50 +00:00
|
|
|
import rp from 'request-promise';
|
|
|
|
import Q from 'q';
|
|
|
|
import mongoose from 'mongoose';
|
2017-01-11 20:17:10 +00:00
|
|
|
import objectID from 'bson-objectid';
|
2016-12-01 23:57:50 +00:00
|
|
|
import shortid from 'shortid';
|
|
|
|
import User from './models/user';
|
|
|
|
import Project from './models/project';
|
|
|
|
import async from 'async';
|
|
|
|
import eachSeries from 'async/eachSeries';
|
|
|
|
|
|
|
|
const defaultHTML =
|
|
|
|
`<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.4/p5.min.js"></script>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.4/addons/p5.dom.min.js"></script>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.4/addons/p5.sound.min.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href="style.css">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<script src="sketch.js"></script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
|
|
|
|
const defaultCSS =
|
|
|
|
`html, body {
|
|
|
|
overflow: hidden;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const a = objectID().toHexString();
|
|
|
|
const b = objectID().toHexString();
|
|
|
|
const c = objectID().toHexString();
|
|
|
|
const r = objectID().toHexString();
|
|
|
|
|
|
|
|
const client_id = process.env.GITHUB_ID;
|
|
|
|
const client_secret = process.env.GITHUB_SECRET;
|
|
|
|
|
2017-01-11 20:17:10 +00:00
|
|
|
const headers = { 'User-Agent': 'p5js-web-editor/0.0.1' };
|
2016-12-01 23:57:50 +00: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);
|
|
|
|
});
|
|
|
|
|
|
|
|
getp5User();
|
|
|
|
|
|
|
|
function getp5User() {
|
2017-01-11 20:17:10 +00:00
|
|
|
User.findOne({ username: 'p5' }, (err, user) => {
|
2016-12-01 23:57:50 +00:00
|
|
|
if (err) throw err;
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
user = new User({
|
|
|
|
username: 'p5',
|
|
|
|
email: 'p5-examples@gmail.com',
|
|
|
|
password: 'test'
|
|
|
|
});
|
|
|
|
user.save(err => {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log('Created a user p5' + user);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:17:10 +00:00
|
|
|
Project.find({ user: user._id }, (err, projects) => {
|
2016-12-01 23:57:50 +00:00
|
|
|
// if there are already some sketches, delete them
|
|
|
|
console.log('Deleting old projects...');
|
|
|
|
projects.forEach(project => {
|
2017-01-11 20:17:10 +00:00
|
|
|
Project.remove({ _id: project._id }, err => {
|
2016-12-01 23:57:50 +00:00
|
|
|
if (err) throw err;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return getCategories()
|
|
|
|
.then(getSketchesInCategories)
|
|
|
|
.then(getSketchContent)
|
|
|
|
.then(createProjectsInP5user);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCategories() {
|
|
|
|
let categories = [];
|
|
|
|
const options = {
|
2017-01-11 20:17:10 +00:00
|
|
|
url: 'https://api.github.com/repos/processing/p5.js-website/contents/dist/assets/examples/en?client_id=' +
|
|
|
|
client_id + '&client_secret=' + client_secret,
|
2016-12-01 23:57:50 +00:00
|
|
|
method: 'GET',
|
2017-01-11 20:17:10 +00:00
|
|
|
headers
|
2016-12-01 23:57:50 +00:00
|
|
|
};
|
|
|
|
return rp(options).then(res => {
|
|
|
|
const json = JSON.parse(res);
|
|
|
|
|
|
|
|
json.forEach(metadata => {
|
|
|
|
let category = '';
|
|
|
|
for (let j = 1; j < metadata.name.split('_').length; j++) {
|
|
|
|
category += metadata.name.split('_')[j] + ' ';
|
|
|
|
}
|
2017-01-11 20:17:10 +00:00
|
|
|
categories.push({ url: metadata.url, name: category });
|
2016-12-01 23:57:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return categories;
|
|
|
|
}).catch(err => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSketchesInCategories(categories) {
|
|
|
|
return Q.all(categories.map(category => {
|
|
|
|
const options = {
|
2017-01-11 20:17:10 +00:00
|
|
|
url: category.url.replace('?ref=master', '') + '?client_id=' + client_id + '&client_secret=' + client_secret,
|
2016-12-01 23:57:50 +00:00
|
|
|
method: 'GET',
|
2017-01-11 20:17:10 +00:00
|
|
|
headers
|
2016-12-01 23:57:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return rp(options).then(res => {
|
|
|
|
let projectsInOneCategory = [];
|
|
|
|
const examples = JSON.parse(res);
|
|
|
|
examples.forEach(example => {
|
|
|
|
let projectName;
|
2016-12-09 17:19:02 +00:00
|
|
|
if (example.name === '02_Instance_Container.js') {
|
|
|
|
for (let i = 1; i < 5; i++) {
|
|
|
|
const instanceProjectName = category.name + ': ' + 'Instance Container ' + i;
|
2017-01-11 20:17:10 +00:00
|
|
|
projectsInOneCategory.push({ sketchUrl: example.download_url, projectName: instanceProjectName });
|
2016-12-09 17:19:02 +00:00
|
|
|
}
|
2016-12-01 23:57:50 +00:00
|
|
|
} else {
|
2016-12-09 17:19:02 +00:00
|
|
|
if (example.name.split('_')[1]) {
|
2017-01-11 20:17:10 +00:00
|
|
|
projectName = category.name + ': ' + example.name.split('_').slice(1).join(' ').replace('.js', '');
|
2016-12-09 17:19:02 +00:00
|
|
|
} else {
|
2017-01-11 20:17:10 +00:00
|
|
|
projectName = category.name + ': ' + example.name.replace('.js', '');
|
2016-12-09 17:19:02 +00:00
|
|
|
}
|
2017-01-11 20:17:10 +00:00
|
|
|
projectsInOneCategory.push({ sketchUrl: example.download_url, projectName });
|
2016-12-01 23:57:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return projectsInOneCategory;
|
|
|
|
}).catch(err => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSketchContent(projectsInAllCategories) {
|
|
|
|
return Q.all(projectsInAllCategories.map(projectsInOneCategory => {
|
2017-01-11 20:17:10 +00:00
|
|
|
return Q.all(projectsInOneCategory.map(project => {
|
2016-12-01 23:57:50 +00:00
|
|
|
const options = {
|
2017-01-11 20:17:10 +00:00
|
|
|
url: project.sketchUrl.replace('?ref=master', '') + '?client_id=' + client_id + '&client_secret=' + client_secret,
|
2016-12-01 23:57:50 +00:00
|
|
|
method: 'GET',
|
2017-01-11 20:17:10 +00:00
|
|
|
headers
|
2016-12-01 23:57:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return rp(options).then(res => {
|
2016-12-09 17:19:02 +00:00
|
|
|
const noNumberprojectName = project.projectName.replace(/(\d+)/g, '');
|
|
|
|
if (noNumberprojectName === 'Instance Mode : Instance Container ') {
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
let splitedRes = res.split('*/')[1].split('</html>')[i] + '</html>\n';
|
|
|
|
project.sketchContent = splitedRes.replace('p5.js', 'https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.4/p5.min.js');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
project.sketchContent = res;
|
|
|
|
}
|
2016-12-01 23:57:50 +00:00
|
|
|
return project;
|
|
|
|
}).catch(err => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function createProjectsInP5user(projectsInAllCategories) {
|
|
|
|
let assetsfiles = [];
|
|
|
|
const options = {
|
2017-01-11 20:17:10 +00:00
|
|
|
url: 'https://api.github.com/repos/processing/p5.js-website/contents/dist/assets/examples/assets?client_id=' +
|
|
|
|
client_id + '&client_secret=' + client_secret,
|
2016-12-01 23:57:50 +00:00
|
|
|
method: 'GET',
|
2017-01-11 20:17:10 +00:00
|
|
|
headers
|
2016-12-01 23:57:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
rp(options).then(res => {
|
|
|
|
const assets = JSON.parse(res);
|
|
|
|
|
2017-01-11 20:17:10 +00:00
|
|
|
User.findOne({ username: 'p5' }, (err, user) => {
|
2016-12-01 23:57:50 +00:00
|
|
|
if (err) throw err;
|
|
|
|
|
|
|
|
async.eachSeries(projectsInAllCategories, (projectsInOneCategory, categoryCallback) => {
|
|
|
|
async.eachSeries(projectsInOneCategory, (project, projectCallback) => {
|
2016-12-09 17:19:02 +00:00
|
|
|
let newProject;
|
|
|
|
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 17:28:20 +00:00
|
|
|
isSelectedFile: true,
|
2016-12-09 17:19:02 +00: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-01 23:57:50 +00:00
|
|
|
|
|
|
|
let assetsInProject = project.sketchContent.match(/assets\/[\w-]+\.[\w]*/g) || project.sketchContent.match(/assets\/[\w-]*/g) || [];
|
|
|
|
|
|
|
|
assetsInProject.forEach((assetName, i) => {
|
|
|
|
assetName = assetName.split('assets/')[1];
|
|
|
|
|
|
|
|
assets.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://rawgit.com/processing/p5.js-website/master/dist/assets/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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-11 20:17:10 +00:00
|
|
|
newProject.save((err, newProject) => {
|
2016-12-01 23:57:50 +00:00
|
|
|
if (err) throw err;
|
|
|
|
console.log('Created a new project in p5 user: ' + newProject.name);
|
|
|
|
projectCallback();
|
|
|
|
});
|
|
|
|
}, (err) => {
|
|
|
|
categoryCallback();
|
|
|
|
});
|
|
|
|
}, (err) => {
|
|
|
|
process.exit();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).catch(err => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|