import rp from 'request-promise'; import Q from 'q'; import mongoose from 'mongoose'; import objectID from 'bson-objectid'; import shortid from 'shortid'; import eachSeries from 'async/eachSeries'; import { URL } from 'url'; import User from '../models/user'; import Project from '../models/project'; const defaultHTML = `
`; const defaultCSS = `html, body { margin: 0; padding: 0; } `; const clientId = process.env.GITHUB_ID; const clientSecret = process.env.GITHUB_SECRET; const headers = { 'User-Agent': 'p5js-web-editor/0.0.1' }; let mongoConnectionString; if (process.env.NODE_ENV === 'production') { const { MONGO_RW_USERNAME, MONGO_RW_PASSWORD, MONGO_HOSTNAME, MONGO_PORT, MONGO_NAME } = process.env; const muo = new URL(`mongodb://${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_NAME}`); muo.username = MONGO_RW_USERNAME; muo.password = MONGO_RW_PASSWORD; mongoConnectionString = `${muo.href}?authSource=admin`; } else { mongoConnectionString = process.env.MONGO_URL; } mongoose.connect(mongoConnectionString); mongoose.connection.on('error', () => { console.error('MongoDB Connection Error. Please make sure that MongoDB is running.'); process.exit(1); }); function getCategories() { const categories = []; const options = { url: `https://api.github.com/repos/processing/p5.js-website/contents/dist/assets/examples/en?client_id=${ clientId}&client_secret=${clientSecret}`, method: 'GET', headers, json: true }; return rp(options).then((res) => { 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 }); }); 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=${clientId}&client_secret=${clientSecret}`, method: 'GET', headers, json: true }; return rp(options).then((res) => { const projectsInOneCategory = []; res.forEach((example) => { let projectName; if (example.name === '02_Instance_Container.js') { for (let i = 1; i < 5; i += 1) { 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 => Q.all(projectsInOneCategory.map((project) => { const options = { url: `${project.sketchUrl.replace('?ref=master', '')}?client_id=${clientId}&client_secret=${clientSecret}`, 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 += 1) { const splitedRes = `${res.split('*/')[1].split('