const colors = require('colors'); const docker = require('docker-compose/dist/v2'); const path = require('path'); const ProjectEnvironment = require('../classes/ProjectEnvironment'); const log = require('../inc/log'); const runServiceStart = (service, projectEnv) => { const env = projectEnv.getEnv(); const scripts = [ 'start.before', `start.before.${env}`, 'start', `start.${env}`, 'start.after', `start.after.${env}`, ]; let chain = Promise.resolve(); scripts.forEach(script => { chain = chain.then(() => { return new Promise(resolve => { try { const start = require(path.join(projectEnv.getCwd(), service, script)); log.log(`\nExecuting %s script for %s...`, colors.bold(script), colors.bold(service)); Promise .all([start({ exec: (command, options) => { const opts = { ...projectEnv.getDockerOptions(), ...options }; return docker.exec(service, command, opts); }, log: (message) => { log.log('%s - %s', colors.bold(service), message); }, env: projectEnv.getEnv() })]) .then(() => { resolve(); }) .catch(() => {}); } catch (ex) { resolve(); } }); }); }); return chain.then(() => {}); }; const runServices = async (projectEnv) => { let promises = []; await docker.buildAll(projectEnv.getDockerOptions()); await docker.upAll(projectEnv.getDockerOptions()); const services = await projectEnv.getServices(); services.forEach(service => { promises.push(runServiceStart(service, projectEnv)); }); await Promise.all(promises); }; module.exports = (env) => { try { const projectEnv = new ProjectEnvironment(process.cwd(), env); log.log("Starting project (%s environment)...", colors.bold(env)); // Docker up services runServices(projectEnv); } catch (ex) { log.error(JSON.stringify(ex)); } };