import rp from 'request-promise'; import Q from 'q'; import mongoose from 'mongoose'; import objectID from 'bson-objectid'; 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 = `
`; 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; const headers = { 'User-Agent': 'p5js-web-editor/0.0.1' }; 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() { User.findOne({ username: 'p5' }, (err, user) => { 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); }); } Project.find({ user: user._id }, (err, projects) => { // if there are already some sketches, delete them console.log('Deleting old projects...'); projects.forEach(project => { Project.remove({ _id: project._id }, err => { if (err) throw err; }); }); }); return getCategories() .then(getSketchesInCategories) .then(getSketchContent) .then(createProjectsInP5user); }); } function getCategories() { let categories = []; const options = { url: 'https://api.github.com/repos/processing/p5.js-website/contents/dist/assets/examples/en?client_id=' + client_id + '&client_secret=' + client_secret, method: 'GET', headers }; 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] + ' '; } categories.push({ url: metadata.url, name: category }); }); return categories; }).catch(err => { throw err; }); } function getSketchesInCategories(categories) { return Q.all(categories.map(category => { const options = { url: category.url.replace('?ref=master', '') + '?client_id=' + client_id + '&client_secret=' + client_secret, method: 'GET', headers }; return rp(options).then(res => { let projectsInOneCategory = []; const examples = JSON.parse(res); examples.forEach(example => { let projectName; if (example.name === '02_Instance_Container.js') { for (let i = 1; i < 5; i++) { const instanceProjectName = category.name + ': ' + 'Instance Container ' + i; projectsInOneCategory.push({ sketchUrl: example.download_url, projectName: instanceProjectName }); } } else { if (example.name.split('_')[1]) { projectName = category.name + ': ' + example.name.split('_').slice(1).join(' ').replace('.js', ''); } else { projectName = category.name + ': ' + example.name.replace('.js', ''); } projectsInOneCategory.push({ sketchUrl: example.download_url, projectName }); } }); return projectsInOneCategory; }).catch(err => { throw err; }); })); } function getSketchContent(projectsInAllCategories) { return Q.all(projectsInAllCategories.map(projectsInOneCategory => { return Q.all(projectsInOneCategory.map(project => { const options = { url: project.sketchUrl.replace('?ref=master', '') + '?client_id=' + client_id + '&client_secret=' + client_secret, method: 'GET', headers }; 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++) { let splitedRes = res.split('*/')[1].split('