2018-02-03 19:14:44 +00:00
|
|
|
const gulp = require('gulp');
|
|
|
|
const gutil = require('gulp-util');
|
|
|
|
const wrap = require('gulp-wrap');
|
|
|
|
const concat = require('gulp-concat');
|
|
|
|
const merge = require('merge-stream');
|
|
|
|
const print = require('gulp-print');
|
|
|
|
const runSequence = require('run-sequence');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const webpack = require('webpack');
|
|
|
|
const WebpackDevServer = require('webpack-dev-server');
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
const path = require('path');
|
2017-10-18 04:41:17 +00:00
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
// Configuration
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
const defaultService = 'salix';
|
|
|
|
const defaultPort = 3000;
|
|
|
|
const devServerPort = 8081;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
const langs = ['es', 'en'];
|
|
|
|
const srcDir = './client';
|
|
|
|
const servicesDir = './services';
|
|
|
|
const nginxDir = `${servicesDir}/nginx`;
|
|
|
|
const buildDir = `${nginxDir}/static`;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
const modules = require('./client/modules.json');
|
|
|
|
const webpackConfig = require('./webpack.config.js');
|
|
|
|
|
|
|
|
// Development
|
2017-05-16 10:37:48 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
gulp.task('default', () => {
|
2017-06-02 13:23:19 +00:00
|
|
|
return gulp.start('services', 'client');
|
2017-05-16 10:37:48 +00:00
|
|
|
});
|
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
gulp.task('client', ['clean'], () => {
|
2017-05-16 10:37:48 +00:00
|
|
|
return gulp.start('watch', 'routes', 'locales', 'webpack-dev-server');
|
|
|
|
});
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-01-31 13:56:55 +00:00
|
|
|
gulp.task('services', callback => {
|
|
|
|
let isWindows = /^win/.test(process.platform);
|
|
|
|
let command = isWindows ? 'docker inspect dblocal | findstr Status' : 'docker inspect dblocal | grep Status';
|
|
|
|
|
|
|
|
exec(command, (err, stdout, stderr) => {
|
|
|
|
let isNotRunning = !stdout.includes('running');
|
|
|
|
if (isNotRunning) {
|
2018-02-01 11:52:58 +00:00
|
|
|
runSequence('docker-wait', 'services-run', callback);
|
2018-01-31 13:56:55 +00:00
|
|
|
} else {
|
2018-02-01 11:52:58 +00:00
|
|
|
runSequence('services-run', callback);
|
2018-01-31 13:56:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-01-31 11:17:17 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
gulp.task('services-run', async () => {
|
|
|
|
const services = await getServices();
|
|
|
|
|
|
|
|
for (let service of services)
|
|
|
|
require(service.index).start(service.port);
|
|
|
|
|
|
|
|
await renderNginxConf(services, 'development');
|
|
|
|
return gulp.start('nginx');
|
2017-06-02 13:23:19 +00:00
|
|
|
});
|
2018-01-11 07:12:21 +00:00
|
|
|
|
2018-02-01 15:01:49 +00:00
|
|
|
gulp.task('e2e', ['docker-wait'], () => {
|
2018-02-01 11:52:58 +00:00
|
|
|
return gulp.start('e2e-run');
|
2018-02-01 07:48:54 +00:00
|
|
|
});
|
|
|
|
|
2018-02-01 15:01:49 +00:00
|
|
|
gulp.task('e2e-run', () => {
|
2018-02-03 19:14:44 +00:00
|
|
|
const jasmine = require('gulp-jasmine');
|
2018-02-01 07:48:54 +00:00
|
|
|
gulp.src('./e2e_tests.js')
|
|
|
|
.pipe(jasmine({reporter: 'none'}));
|
|
|
|
});
|
|
|
|
|
2018-02-01 15:01:49 +00:00
|
|
|
gulp.task('clean', () => {
|
2018-02-03 19:14:44 +00:00
|
|
|
const del = require('del');
|
2018-01-29 11:37:54 +00:00
|
|
|
return del([`${buildDir}/*`, `!${buildDir}/templates`, `!${buildDir}/images`], {force: true});
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('install', () => {
|
2018-02-03 19:14:44 +00:00
|
|
|
const install = require('gulp-install');
|
2018-01-29 11:37:54 +00:00
|
|
|
const jsonFile = [];
|
2018-02-03 19:14:44 +00:00
|
|
|
const services = fs.readdirSync(servicesDir);
|
2018-01-29 11:37:54 +00:00
|
|
|
services.push('..');
|
|
|
|
services.forEach(service => {
|
2018-02-03 19:14:44 +00:00
|
|
|
jsonFile.push(`${servicesDir}/${service}/package.json`);
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
|
|
|
return gulp.src(jsonFile)
|
|
|
|
.pipe(print(filepath => {
|
|
|
|
return `Installing packages in ${filepath}`;
|
|
|
|
}))
|
|
|
|
.pipe(install({
|
|
|
|
npm: ['--no-package-lock']
|
|
|
|
}));
|
2018-01-11 07:12:21 +00:00
|
|
|
});
|
2017-06-02 13:23:19 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
// Deployment
|
|
|
|
|
2018-01-31 13:56:55 +00:00
|
|
|
gulp.task('build', ['clean'], () => {
|
2018-02-03 19:14:44 +00:00
|
|
|
return gulp.start('routes', 'locales', 'webpack', 'build-conf');
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('build-conf', async callback => {
|
|
|
|
let compose = await fs.readFile('./docker-compose.tpl.yml', 'utf8');
|
|
|
|
|
|
|
|
const yaml = require('js-yaml');
|
|
|
|
let composeYml = yaml.safeLoad(compose);
|
|
|
|
let services = await getServices();
|
|
|
|
|
|
|
|
for (let service of services) {
|
|
|
|
let dockerFile = `${__dirname}/Dockerfile`;
|
|
|
|
|
|
|
|
if (await fs.exists(`./services/${service.name}/Dockerfile`))
|
|
|
|
dockerFile = 'Dockerfile';
|
|
|
|
|
|
|
|
composeYml.services[service.name] = {
|
|
|
|
environment: ['NODE_ENV=${NODE_ENV}'],
|
|
|
|
container_name: `\${BRANCH_NAME}-${service.name}`,
|
|
|
|
image: `${service.name}:\${TAG}`,
|
|
|
|
build: {
|
|
|
|
context: `./services/${service.name}`,
|
|
|
|
dockerfile: dockerFile
|
|
|
|
},
|
|
|
|
ports: [`${defaultPort}:${service.port}`]
|
|
|
|
};
|
|
|
|
composeYml.services.nginx.links.push(
|
|
|
|
`${service.name}:\${BRANCH_NAME}-${service.name}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let ymlString = yaml.safeDump(composeYml);
|
|
|
|
await fs.writeFile('./docker-compose.yml', ymlString);
|
|
|
|
|
|
|
|
await renderNginxConf(services, 'production');
|
2018-01-08 14:06:20 +00:00
|
|
|
});
|
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
// Services
|
|
|
|
|
|
|
|
async function getServices() {
|
|
|
|
let startPort = defaultPort + 1;
|
|
|
|
let servicesData = [];
|
|
|
|
|
|
|
|
const services = fs.readdirSync(servicesDir);
|
|
|
|
const exclude = ['loopback'];
|
|
|
|
|
|
|
|
for (let service of services) {
|
|
|
|
let index = `${servicesDir}/${service}/server/server.js`;
|
|
|
|
if (!await fs.exists(index) || exclude.indexOf(service) !== -1) continue;
|
|
|
|
|
|
|
|
let port = service == defaultService ? defaultPort : startPort++;
|
|
|
|
servicesData.push({
|
|
|
|
name: service,
|
|
|
|
index: index,
|
|
|
|
port: port
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return servicesData;
|
|
|
|
}
|
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
// Nginx
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
gulp.task('nginx', callback => {
|
|
|
|
let isWindows = /^win/.test(process.platform);
|
|
|
|
let command = isWindows ? 'start.cmd' : 'start.sh';
|
2018-02-03 19:14:44 +00:00
|
|
|
command = path.join(`${nginxDir}/${command}`);
|
2018-01-29 11:37:54 +00:00
|
|
|
exec(command, (err, stdout, stderr) => {
|
2018-02-02 11:38:34 +00:00
|
|
|
if (stderr) console.log(stderr);
|
2018-01-29 11:37:54 +00:00
|
|
|
callback(err);
|
2017-09-26 14:59:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
async function renderNginxConf(services, env) {
|
|
|
|
const mustache = require('mustache');
|
|
|
|
|
|
|
|
let params = {
|
|
|
|
services: services,
|
|
|
|
defaultService: defaultService,
|
|
|
|
defaultPort: defaultPort,
|
|
|
|
devServerPort: devServerPort
|
|
|
|
};
|
|
|
|
let template = await fs.readFile(`${nginxDir}/nginx.${env}.mst`, 'utf8');
|
|
|
|
let nginxConf = mustache.render(template, params);
|
|
|
|
|
|
|
|
await fs.writeFile(`${nginxDir}/temp/nginx.${env}.conf`, nginxConf);
|
|
|
|
}
|
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
// Webpack
|
|
|
|
|
2017-10-05 10:38:28 +00:00
|
|
|
gulp.task('webpack', function(cb) {
|
2018-02-03 19:14:44 +00:00
|
|
|
let configCopy = Object.create(webpackConfig);
|
|
|
|
let compiler = webpack(configCopy);
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
compiler.run(function(err, stats) {
|
|
|
|
if (err) throw new gutil.PluginError('webpack', err);
|
|
|
|
gutil.log('[webpack]', stats.toString({colors: true}));
|
2017-05-17 10:07:42 +00:00
|
|
|
cb();
|
2017-01-31 13:13:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-05 10:38:28 +00:00
|
|
|
gulp.task('webpack-dev-server', function() {
|
2018-02-03 19:14:44 +00:00
|
|
|
let configCopy = Object.create(webpackConfig);
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
for (let entry in configCopy.entry) {
|
2017-01-31 13:13:06 +00:00
|
|
|
configCopy.entry[entry]
|
2018-02-03 19:14:44 +00:00
|
|
|
.unshift(`webpack-dev-server/client?http://127.0.0.1:${devServerPort}/`);
|
2017-03-01 08:55:17 +00:00
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
let compiler = webpack(configCopy);
|
2017-03-01 08:55:17 +00:00
|
|
|
new WebpackDevServer(compiler, {
|
2017-01-31 13:13:06 +00:00
|
|
|
publicPath: '/',
|
|
|
|
contentBase: buildDir,
|
|
|
|
quiet: false,
|
|
|
|
noInfo: false,
|
2017-10-24 06:56:18 +00:00
|
|
|
// hot: true,
|
2017-01-31 13:13:06 +00:00
|
|
|
stats: {
|
|
|
|
assets: true,
|
|
|
|
colors: true,
|
|
|
|
version: false,
|
|
|
|
hash: false,
|
|
|
|
timings: true,
|
|
|
|
chunks: false,
|
|
|
|
chunkModules: false
|
|
|
|
}
|
2018-02-03 19:14:44 +00:00
|
|
|
}).listen(devServerPort, '127.0.0.1', function(err) {
|
2017-01-31 13:13:06 +00:00
|
|
|
if (err) throw new gutil.PluginError('webpack-dev-server', err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Locale
|
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
let localeFiles = `${srcDir}/**/locale/*.json`;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
gulp.task('locales', function() {
|
2018-02-03 19:14:44 +00:00
|
|
|
const extend = require('gulp-extend');
|
|
|
|
let streams = [];
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
for (let mod in modules)
|
|
|
|
for (let lang of langs) {
|
|
|
|
let localeFiles = `./client/${mod}/**/locale/${lang}.json`;
|
2017-03-01 08:55:17 +00:00
|
|
|
streams.push(gulp.src(localeFiles)
|
|
|
|
.pipe(extend(`${lang}.json`))
|
|
|
|
.pipe(gulp.dest(`${buildDir}/locale/${mod}`)));
|
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
return merge(streams);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
let routeFiles = `${srcDir}/**/routes.json`;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
gulp.task('routes', function() {
|
|
|
|
return gulp.src(routeFiles)
|
2017-10-24 06:56:18 +00:00
|
|
|
.pipe(concat('routes.js', {newLine: ','}))
|
|
|
|
.pipe(wrap('var routes = [<%=contents%>\n];'))
|
|
|
|
.pipe(gulp.dest(buildDir));
|
2017-01-31 13:13:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Watch
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
gulp.task('watch', function() {
|
|
|
|
gulp.watch(routeFiles, ['routes']);
|
|
|
|
gulp.watch(localeFiles, ['locales']);
|
|
|
|
});
|
2017-10-18 04:41:17 +00:00
|
|
|
|
2018-02-01 15:01:49 +00:00
|
|
|
// Docker
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2017-10-24 06:56:18 +00:00
|
|
|
gulp.task('docker', callback => {
|
2018-02-01 11:52:58 +00:00
|
|
|
runSequence('docker-delete', 'docker-delete-image', 'docker-build', 'docker-run', callback);
|
2017-10-24 06:56:18 +00:00
|
|
|
});
|
|
|
|
|
2018-02-01 11:52:58 +00:00
|
|
|
gulp.task('docker-wait', ['docker'], callback => {
|
|
|
|
let maxInterval = 30 * 60000;
|
2018-02-01 07:48:54 +00:00
|
|
|
let interval = 1000;
|
|
|
|
let timer = 0;
|
|
|
|
console.log('Waiting for MySQL init process...');
|
|
|
|
let waitForLocaldb = setInterval(() => {
|
|
|
|
if (timer < maxInterval) {
|
|
|
|
timer += interval;
|
|
|
|
exec('docker logs --tail 4 dblocal', (err, stdout, stderr) => {
|
|
|
|
if (stdout.includes('MySQL init process done. Ready for start up.')) {
|
|
|
|
clearInterval(waitForLocaldb);
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log(`MySQL connection not established whithin ${maxInterval / 1000} secs!`);
|
|
|
|
clearInterval(waitForLocaldb);
|
|
|
|
}
|
|
|
|
}, interval);
|
|
|
|
});
|
2018-02-01 11:52:58 +00:00
|
|
|
|
|
|
|
gulp.task('docker-run', callback => {
|
|
|
|
exec('docker run -d --name dblocal -p 3306:3306 dblocal', (err, stdout, stderr) => {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('docker-build', callback => {
|
|
|
|
exec('docker build -t dblocal:latest ./services/db', (err, stdout, stderr) => {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('docker-delete-image', callback => {
|
|
|
|
exec('docker rmi dblocal:latest', (err, stdout, stderr) => {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('docker-delete', callback => {
|
|
|
|
exec('docker stop dblocal && docker wait dblocal && docker rm -f dblocal', (err, stdout, stderr) => {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
});
|