385 lines
10 KiB
JavaScript
385 lines
10 KiB
JavaScript
require('require-yaml');
|
|
const gulp = require('gulp');
|
|
const gutil = require('gulp-util');
|
|
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;
|
|
|
|
// Configuration
|
|
|
|
const isWindows = /^win/.test(process.platform);
|
|
|
|
if (gutil.env.NODE_ENV)
|
|
process.env.NODE_ENV = gutil.env.NODE_ENV;
|
|
|
|
const env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
|
|
|
|
const langs = ['es', 'en'];
|
|
const srcDir = './client';
|
|
const servicesDir = './services';
|
|
const nginxDir = `${servicesDir}/nginx`;
|
|
const buildDir = `${nginxDir}/static`;
|
|
|
|
const modules = require('./client/modules.yml');
|
|
const webpackConfig = require('./webpack.config.js');
|
|
|
|
let proxyConf = require(`${nginxDir}/config.yml`);
|
|
let proxyEnvFile = `${nginxDir}/config.${env}.yml`;
|
|
|
|
if (fs.existsSync(proxyEnvFile))
|
|
Object.assign(proxyConf, require(proxyEnvFile));
|
|
|
|
const defaultService = proxyConf.main;
|
|
const defaultPort = proxyConf.defaultPort;
|
|
const devServerPort = proxyConf.devServerPort;
|
|
|
|
// Development
|
|
|
|
gulp.task('default', () => {
|
|
return gulp.start('services', 'client');
|
|
});
|
|
|
|
gulp.task('client', ['clean'], () => {
|
|
return gulp.start('watch', 'routes', 'locales', 'webpack-dev-server');
|
|
});
|
|
|
|
gulp.task('services', callback => {
|
|
let command = isWindows
|
|
? 'docker inspect dblocal | findstr Status'
|
|
: 'docker inspect dblocal | grep Status';
|
|
|
|
exec(command, (err, stdout, stderr) => {
|
|
if (err) return callback(err);
|
|
let isNotRunning = !stdout.includes('running');
|
|
if (isNotRunning)
|
|
runSequence('docker-wait', 'services-run', callback);
|
|
else
|
|
runSequence('services-run', callback);
|
|
});
|
|
});
|
|
|
|
gulp.task('services-run', async () => {
|
|
const services = await getServices();
|
|
for (let service of services)
|
|
require(service.index).start(service.port);
|
|
|
|
return gulp.start('nginx');
|
|
});
|
|
|
|
gulp.task('e2e', ['docker-wait'], () => {
|
|
return gulp.start('e2e-run');
|
|
});
|
|
|
|
gulp.task('e2e-run', () => {
|
|
const jasmine = require('gulp-jasmine');
|
|
gulp.src('./e2e_tests.js')
|
|
.pipe(jasmine({reporter: 'none'}));
|
|
});
|
|
|
|
gulp.task('clean', () => {
|
|
const del = require('del');
|
|
const files = [
|
|
`${buildDir}/*`,
|
|
`!${buildDir}/templates`,
|
|
`!${buildDir}/images`,
|
|
`docker-compose.yml`
|
|
];
|
|
return del(files, {force: true});
|
|
});
|
|
|
|
gulp.task('i', ['install']);
|
|
|
|
gulp.task('install', () => {
|
|
const install = require('gulp-install');
|
|
let jsonFile = [];
|
|
let services = fs.readdirSync(servicesDir);
|
|
services.push('..');
|
|
services.forEach(service => {
|
|
jsonFile.push(`${servicesDir}/${service}/package.json`);
|
|
});
|
|
return gulp.src(jsonFile)
|
|
.pipe(print(filepath => {
|
|
return `Installing packages in ${filepath}`;
|
|
}))
|
|
.pipe(install({
|
|
npm: ['--no-package-lock']
|
|
}));
|
|
});
|
|
|
|
// Deployment
|
|
|
|
gulp.task('build', ['clean'], () => {
|
|
return gulp.start('routes', 'locales', 'webpack', 'docker-compose', 'nginx-conf');
|
|
});
|
|
|
|
gulp.task('docker-compose', async () => {
|
|
const yaml = require('js-yaml');
|
|
let compose = await fs.readFile('./docker-compose.tpl.yml', 'utf8');
|
|
let composeYml = yaml.safeLoad(compose);
|
|
let services = await getServices();
|
|
|
|
for (let service of services) {
|
|
let dockerFile = `${__dirname}/services/${service.name}/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`,
|
|
dockerfile: dockerFile
|
|
},
|
|
ports: [`${service.port}:${defaultPort}`]
|
|
};
|
|
composeYml.services.nginx.links.push(
|
|
`${service.name}:\${BRANCH_NAME}-${service.name}`
|
|
);
|
|
}
|
|
|
|
let ymlString = yaml.safeDump(composeYml);
|
|
await fs.writeFile('./docker-compose.yml', ymlString);
|
|
});
|
|
|
|
// Nginx & services
|
|
|
|
let nginxConf = 'temp/nginx.conf';
|
|
let nginxTemp = `${nginxDir}/temp`;
|
|
|
|
async function nginxGetBin() {
|
|
if (isWindows)
|
|
return 'nginx';
|
|
try {
|
|
let nginxBin = '/usr/sbin/nginx';
|
|
await fs.stat(nginxBin);
|
|
return nginxBin;
|
|
} catch (e) {
|
|
return 'nginx';
|
|
}
|
|
}
|
|
|
|
gulp.task('nginx', ['nginx-stop'], async () => {
|
|
let nginxBin = await nginxGetBin();
|
|
|
|
if (isWindows)
|
|
nginxBin = `start /B ${nginxBin}`;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
exec(`${nginxBin} -c "${nginxConf}" -p "${nginxDir}"`, err => {
|
|
if (err) return reject(err);
|
|
resolve();
|
|
});
|
|
});
|
|
});
|
|
|
|
gulp.task('nginx-stop', ['nginx-conf'], async () => {
|
|
try {
|
|
let nginxBin = await nginxGetBin();
|
|
await fs.stat(`${nginxTemp}/nginx.pid`);
|
|
let command = `${nginxBin} -c "${nginxConf}" -p "${nginxDir}" -s stop`;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
exec(command, err => {
|
|
if (err && err.code != 1) return reject(err);
|
|
resolve();
|
|
});
|
|
});
|
|
} catch (e) {}
|
|
});
|
|
|
|
gulp.task('nginx-conf', async () => {
|
|
const mustache = require('mustache');
|
|
|
|
if (!await fs.exists(nginxTemp))
|
|
await fs.mkdir(nginxTemp);
|
|
|
|
let params = {
|
|
services: await getServices(),
|
|
defaultService: defaultService,
|
|
defaultPort: defaultPort,
|
|
devServerPort: devServerPort,
|
|
port: proxyConf.port,
|
|
host: proxyConf.host
|
|
};
|
|
|
|
let confFile = `${nginxDir}/nginx.${env}.mst`;
|
|
|
|
if (!await fs.exists(confFile))
|
|
confFile = `${nginxDir}/nginx.mst`;
|
|
|
|
let template = await fs.readFile(confFile, 'utf8');
|
|
let nginxConf = mustache.render(template, params);
|
|
|
|
await fs.writeFile(`${nginxTemp}/nginx.conf`, nginxConf);
|
|
});
|
|
|
|
gulp.task('nginx-clean', () => {
|
|
const del = require('del');
|
|
return del([`${nginxTemp}/*`], {force: true});
|
|
});
|
|
|
|
async function getServices() {
|
|
let services;
|
|
let startPort = defaultPort + 1;
|
|
services = [];
|
|
|
|
const serviceDirs = await fs.readdir(servicesDir);
|
|
const exclude = ['loopback'];
|
|
|
|
for (let service of serviceDirs) {
|
|
let index = `${servicesDir}/${service}/server/server.js`;
|
|
if (!await fs.exists(index) || exclude.indexOf(service) !== -1) continue;
|
|
|
|
let port = service == defaultService ? defaultPort : startPort++;
|
|
services.push({
|
|
name: service,
|
|
index: index,
|
|
port: port
|
|
});
|
|
}
|
|
|
|
return services;
|
|
}
|
|
|
|
// Webpack
|
|
|
|
gulp.task('webpack', function(cb) {
|
|
let configCopy = Object.create(webpackConfig);
|
|
let compiler = webpack(configCopy);
|
|
|
|
compiler.run(function(err, stats) {
|
|
if (err) throw new gutil.PluginError('webpack', err);
|
|
gutil.log('[webpack]', stats.toString({colors: true}));
|
|
cb();
|
|
});
|
|
});
|
|
|
|
gulp.task('webpack-dev-server', function() {
|
|
let configCopy = Object.create(webpackConfig);
|
|
|
|
for (let entry in configCopy.entry) {
|
|
configCopy.entry[entry]
|
|
.unshift(`webpack-dev-server/client?http://127.0.0.1:${devServerPort}/`);
|
|
}
|
|
|
|
let compiler = webpack(configCopy);
|
|
new WebpackDevServer(compiler, {
|
|
publicPath: '/',
|
|
contentBase: buildDir,
|
|
quiet: false,
|
|
noInfo: false,
|
|
// hot: true,
|
|
stats: {
|
|
assets: true,
|
|
colors: true,
|
|
version: false,
|
|
hash: false,
|
|
timings: true,
|
|
chunks: false,
|
|
chunkModules: false
|
|
}
|
|
}).listen(devServerPort, '127.0.0.1', function(err) {
|
|
if (err) throw new gutil.PluginError('webpack-dev-server', err);
|
|
});
|
|
});
|
|
|
|
// Locale
|
|
|
|
let localeFiles = `${srcDir}/**/locale/*.yml`;
|
|
|
|
gulp.task('locales', function() {
|
|
const extend = require('gulp-extend');
|
|
const yaml = require('gulp-yaml');
|
|
const merge = require('merge-stream');
|
|
|
|
let streams = [];
|
|
|
|
for (let mod in modules)
|
|
for (let lang of langs) {
|
|
let localeFiles = `./client/${mod}/**/locale/${lang}.yml`;
|
|
streams.push(gulp.src(localeFiles)
|
|
.pipe(yaml())
|
|
.pipe(extend(`${lang}.json`))
|
|
.pipe(gulp.dest(`${buildDir}/locale/${mod}`)));
|
|
}
|
|
|
|
return merge(streams);
|
|
});
|
|
|
|
// Routes
|
|
|
|
let routeFiles = `${srcDir}/**/routes.json`;
|
|
|
|
gulp.task('routes', function() {
|
|
const concat = require('gulp-concat');
|
|
const wrap = require('gulp-wrap');
|
|
|
|
return gulp.src(routeFiles)
|
|
.pipe(concat('routes.js', {newLine: ','}))
|
|
.pipe(wrap('var routes = [<%=contents%>\n];'))
|
|
.pipe(gulp.dest(buildDir));
|
|
});
|
|
|
|
// Watch
|
|
|
|
gulp.task('watch', function() {
|
|
gulp.watch(routeFiles, ['routes']);
|
|
gulp.watch(localeFiles, ['locales']);
|
|
});
|
|
|
|
// Docker
|
|
|
|
gulp.task('docker', callback => {
|
|
runSequence('docker-delete', 'docker-delete-image', 'docker-build', 'docker-run', callback);
|
|
});
|
|
|
|
gulp.task('docker-wait', ['docker'], callback => {
|
|
let maxInterval = 30 * 60000;
|
|
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);
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|