2017-01-31 13:13:06 +00:00
|
|
|
var gulp = require('gulp');
|
2018-01-08 12:40:05 +00:00
|
|
|
const jasmine = require('gulp-jasmine');
|
2017-01-31 13:13:06 +00:00
|
|
|
var gutil = require('gulp-util');
|
|
|
|
var wrap = require('gulp-wrap');
|
|
|
|
var concat = require('gulp-concat');
|
|
|
|
var merge = require('merge-stream');
|
2017-06-02 12:17:57 +00:00
|
|
|
var extend = require('gulp-extend');
|
2017-09-17 15:42:01 +00:00
|
|
|
var install = require('gulp-install');
|
|
|
|
var print = require('gulp-print');
|
2017-10-18 16:26:40 +00:00
|
|
|
var runSequence = require('run-sequence');
|
2017-01-31 13:13:06 +00:00
|
|
|
var del = require('del');
|
2017-05-29 16:48:50 +00:00
|
|
|
var fs = require('fs');
|
2017-01-31 13:13:06 +00:00
|
|
|
var webpack = require('webpack');
|
|
|
|
var WebpackDevServer = require('webpack-dev-server');
|
|
|
|
|
2017-10-18 04:41:17 +00:00
|
|
|
var exec = require('child_process').exec;
|
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
// Configuration
|
2017-03-01 08:55:17 +00:00
|
|
|
var srcDir = './client';
|
2017-01-31 13:13:06 +00:00
|
|
|
var buildDir = './services/nginx/static';
|
|
|
|
var langs = ['es', 'en'];
|
|
|
|
|
2017-10-05 10:38:28 +00:00
|
|
|
var modules = require('./client/modules.json');
|
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
var webpackConfig = require('./webpack.config.js');
|
|
|
|
|
2017-05-16 10:37:48 +00:00
|
|
|
// Main tasks
|
|
|
|
|
2017-06-02 13:23:19 +00:00
|
|
|
gulp.task('default', function() {
|
|
|
|
return gulp.start('services', 'client');
|
2017-05-16 10:37:48 +00:00
|
|
|
});
|
|
|
|
|
2017-06-02 13:23:19 +00:00
|
|
|
gulp.task('build', ['clean'], function() {
|
|
|
|
return gulp.start('routes', 'locales', 'webpack');
|
2017-05-16 10:37:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('client', ['clean'], function() {
|
|
|
|
return gulp.start('watch', 'routes', 'locales', 'webpack-dev-server');
|
|
|
|
});
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2017-10-24 06:56:18 +00:00
|
|
|
gulp.task('services', () => {
|
|
|
|
process.env.NODE_ENV = gutil.env.env || 'development';
|
|
|
|
const pathServices = './services/';
|
|
|
|
const services = fs.readdirSync(pathServices);
|
|
|
|
services.splice(services.indexOf('loopback'), 1);
|
|
|
|
return services.forEach(service => {
|
|
|
|
const serviceJs = pathServices.concat(service, '/server/server.js');
|
|
|
|
if (fs.existsSync(serviceJs))
|
|
|
|
require(serviceJs).start();
|
|
|
|
});
|
2017-06-02 13:23:19 +00:00
|
|
|
});
|
|
|
|
|
2018-01-08 14:06:20 +00:00
|
|
|
gulp.task('servicesDev', callback => {
|
|
|
|
let isWindows = /^win/.test(process.platform);
|
2018-01-08 14:35:37 +00:00
|
|
|
let command = isWindows ? 'docker inspect dblocal | findstr Status' : 'docker inspect dblocal | grep Status';
|
2018-01-08 14:06:20 +00:00
|
|
|
gutil.env.env = 'test';
|
|
|
|
exec(command, (err, stdout, stderr) => {
|
|
|
|
let isNotRunning = !stdout.includes('running');
|
|
|
|
if (isNotRunning) {
|
|
|
|
runSequence('docker', 'waitForMySQL', 'services');
|
|
|
|
} else {
|
|
|
|
runSequence('services');
|
|
|
|
}
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
gulp.task('clean', function() {
|
2017-07-12 05:49:45 +00:00
|
|
|
return del([`${buildDir}/*`, `!${buildDir}/templates`, `!${buildDir}/images`], {force: true});
|
2017-06-08 14:53:10 +00:00
|
|
|
});
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2017-09-26 14:59:24 +00:00
|
|
|
gulp.task('install', () => {
|
|
|
|
const pathServices = './services/';
|
|
|
|
const fileJson = [];
|
|
|
|
const services = fs.readdirSync(pathServices);
|
|
|
|
services.push('..');
|
|
|
|
services.forEach(service => {
|
|
|
|
fileJson.push(pathServices.concat(service, '/package.json'));
|
|
|
|
});
|
|
|
|
return gulp.src(fileJson)
|
|
|
|
.pipe(print(filepath => {
|
|
|
|
return `Installing packages in ${filepath}`;
|
|
|
|
}))
|
|
|
|
.pipe(install({
|
|
|
|
npm: ['--no-package-lock']
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
// Webpack
|
|
|
|
|
2017-10-05 10:38:28 +00:00
|
|
|
gulp.task('webpack', function(cb) {
|
2017-01-31 13:13:06 +00:00
|
|
|
var configCopy = Object.create(webpackConfig);
|
|
|
|
var compiler = webpack(configCopy);
|
|
|
|
|
|
|
|
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() {
|
2017-01-31 13:13:06 +00:00
|
|
|
var configCopy = Object.create(webpackConfig);
|
|
|
|
|
2017-03-01 08:55:17 +00:00
|
|
|
for (var entry in configCopy.entry) {
|
2017-01-31 13:13:06 +00:00
|
|
|
configCopy.entry[entry]
|
2017-09-17 13:09:10 +00:00
|
|
|
.unshift('webpack-dev-server/client?http://127.0.0.1:8081/');
|
2017-03-01 08:55:17 +00:00
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
var 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
|
|
|
|
}
|
2017-09-17 13:09:10 +00:00
|
|
|
}).listen(8081, '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
|
|
|
|
|
2017-06-06 11:06:47 +00:00
|
|
|
var localeFiles = `${srcDir}/**/locale/*.json`;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
gulp.task('locales', function() {
|
|
|
|
var streams = [];
|
|
|
|
|
2017-03-07 16:11:56 +00:00
|
|
|
for (var mod in modules)
|
2017-03-01 08:55:17 +00:00
|
|
|
for (var lang of langs) {
|
|
|
|
var localeFiles = `./client/${mod}/**/locale/${lang}.json`;
|
|
|
|
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
|
|
|
|
|
2017-06-06 11:06:47 +00:00
|
|
|
var 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
|
|
|
|
gulp.task('watch', function() {
|
|
|
|
gulp.watch(routeFiles, ['routes']);
|
|
|
|
gulp.watch(localeFiles, ['locales']);
|
|
|
|
});
|
2017-10-18 04:41:17 +00:00
|
|
|
|
2018-01-08 08:42:16 +00:00
|
|
|
// Server side unit tests
|
2017-10-24 06:56:18 +00:00
|
|
|
gulp.task('test', callback => {
|
|
|
|
return require('./services_tests').start();
|
2017-10-18 16:26:40 +00:00
|
|
|
});
|
|
|
|
|
2018-01-08 07:12:39 +00:00
|
|
|
// e2e tests
|
|
|
|
gulp.task('e2e', callback => {
|
2018-01-08 12:40:05 +00:00
|
|
|
runSequence('docker', 'waitForMySQL', 'endToEndTests', callback);
|
2018-01-08 07:12:39 +00:00
|
|
|
});
|
|
|
|
|
2018-01-08 12:40:05 +00:00
|
|
|
gulp.task('waitForMySQL', callback => {
|
2018-01-08 14:35:37 +00:00
|
|
|
let maxInterval = 30000;
|
2018-01-08 12:40:05 +00:00
|
|
|
let interval = 1000;
|
2018-01-08 07:12:39 +00:00
|
|
|
let timer = 0;
|
2018-01-08 09:49:41 +00:00
|
|
|
console.log('Waiting for MySQL init process...');
|
2018-01-08 07:12:39 +00:00
|
|
|
let waitForLocaldb = setInterval(() => {
|
2018-01-08 12:40:05 +00:00
|
|
|
if (timer < maxInterval) {
|
|
|
|
timer += interval;
|
2018-01-08 08:42:16 +00:00
|
|
|
exec('docker logs --tail 4 dblocal', (err, stdout, stderr) => {
|
|
|
|
if (stdout.includes('MySQL init process done. Ready for start up.')) {
|
2018-01-08 07:12:39 +00:00
|
|
|
clearInterval(waitForLocaldb);
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2018-01-09 14:31:25 +00:00
|
|
|
console.log(`MySQL connection not established whithin ${maxInterval / 1000} secs!`);
|
2018-01-08 07:12:39 +00:00
|
|
|
clearInterval(waitForLocaldb);
|
|
|
|
}
|
2018-01-08 12:40:05 +00:00
|
|
|
}, interval);
|
2018-01-08 07:12:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('endToEndTests', callback => {
|
2018-01-08 12:40:05 +00:00
|
|
|
gulp.src('./e2e_tests.js')
|
|
|
|
.pipe(jasmine({reporter: 'none'}));
|
2018-01-08 07:12:39 +00:00
|
|
|
});
|
|
|
|
|
2017-10-24 06:56:18 +00:00
|
|
|
// docker dblocal
|
|
|
|
gulp.task('docker', callback => {
|
|
|
|
runSequence('deleteDockerDb', 'deleteDockerImageDb', 'buildDockerDb', 'runDockerDb', callback);
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('runDockerDb', callback => {
|
|
|
|
exec('docker run -d --name dblocal -p 3306:3306 dblocal', (err, stdout, stderr) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
callback(err);
|
|
|
|
}, 15000);
|
2017-10-18 04:41:17 +00:00
|
|
|
});
|
|
|
|
});
|
2017-10-18 16:26:40 +00:00
|
|
|
|
2017-10-24 06:56:18 +00:00
|
|
|
gulp.task('buildDockerDb', callback => {
|
2017-10-18 04:41:17 +00:00
|
|
|
exec('docker build -t dblocal:latest ./services/db', (err, stdout, stderr) => {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
});
|
2017-10-24 06:56:18 +00:00
|
|
|
|
|
|
|
gulp.task('deleteDockerImageDb', callback => {
|
|
|
|
exec('docker rmi dblocal:latest', (err, stdout, stderr) => {
|
|
|
|
callback(err = null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('deleteDockerDb', callback => {
|
|
|
|
exec('docker stop dblocal && docker wait dblocal && docker rm -f dblocal', (err, stdout, stderr) => {
|
|
|
|
callback(err = null);
|
2017-10-18 04:41:17 +00:00
|
|
|
});
|
|
|
|
});
|