249 lines
6.8 KiB
JavaScript
249 lines
6.8 KiB
JavaScript
var gulp = require('gulp');
|
|
var jasmine = require('gulp-jasmine');
|
|
var gutil = require('gulp-util');
|
|
var wrap = require('gulp-wrap');
|
|
var concat = require('gulp-concat');
|
|
var merge = require('merge-stream');
|
|
var extend = require('gulp-extend');
|
|
var install = require('gulp-install');
|
|
var print = require('gulp-print');
|
|
var runSequence = require('run-sequence');
|
|
var del = require('del');
|
|
var fs = require('fs');
|
|
var webpack = require('webpack');
|
|
var WebpackDevServer = require('webpack-dev-server');
|
|
var exec = require('child_process').exec;
|
|
|
|
// Configuration
|
|
|
|
var srcDir = './client';
|
|
var buildDir = './services/nginx/static';
|
|
var langs = ['es', 'en'];
|
|
|
|
var modules = require('./client/modules.json');
|
|
var webpackConfig = require('./webpack.config.js');
|
|
|
|
// Main tasks
|
|
|
|
gulp.task('default', () => {
|
|
return gulp.start('services', 'client');
|
|
});
|
|
|
|
gulp.task('build', ['clean'], () => {
|
|
return gulp.start('routes', 'locales', 'webpack');
|
|
});
|
|
|
|
gulp.task('client', ['clean'], () => {
|
|
return gulp.start('watch', 'routes', 'locales', 'webpack-dev-server');
|
|
});
|
|
|
|
gulp.task('services', ['nginx'], () => {
|
|
if (gutil.env.env && !process.env.NODE_ENV)
|
|
process.env.NODE_ENV = gutil.env.env;
|
|
|
|
const servicesPath = './services/';
|
|
const services = fs.readdirSync(servicesPath);
|
|
services.splice(services.indexOf('loopback'), 1);
|
|
return services.forEach(service => {
|
|
const serviceJs = servicesPath.concat(service, '/server/server.js');
|
|
if (fs.existsSync(serviceJs))
|
|
require(serviceJs).start();
|
|
});
|
|
});
|
|
|
|
gulp.task('clean', function() {
|
|
return del([`${buildDir}/*`, `!${buildDir}/templates`, `!${buildDir}/images`], {force: true});
|
|
});
|
|
|
|
gulp.task('install', () => {
|
|
const servicesPath = './services/';
|
|
const jsonFile = [];
|
|
const services = fs.readdirSync(servicesPath);
|
|
services.push('..');
|
|
services.forEach(service => {
|
|
jsonFile.push(servicesPath.concat(service, '/package.json'));
|
|
});
|
|
return gulp.src(jsonFile)
|
|
.pipe(print(filepath => {
|
|
return `Installing packages in ${filepath}`;
|
|
}))
|
|
.pipe(install({
|
|
npm: ['--no-package-lock']
|
|
}));
|
|
});
|
|
|
|
gulp.task('servicesDev', callback => {
|
|
let isWindows = /^win/.test(process.platform);
|
|
let command = isWindows ? 'docker inspect dblocal | findstr Status' : 'docker inspect dblocal | grep Status';
|
|
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);
|
|
});
|
|
});
|
|
|
|
// Nginx
|
|
|
|
gulp.task('nginx', callback => {
|
|
let isWindows = /^win/.test(process.platform);
|
|
let command = isWindows ? 'start.cmd' : 'start.sh';
|
|
command = `./services/nginx/${command}`;
|
|
exec(command, (err, stdout, stderr) => {
|
|
console.log(stdout);
|
|
callback(err);
|
|
});
|
|
});
|
|
|
|
// Webpack
|
|
|
|
gulp.task('webpack', function(cb) {
|
|
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}));
|
|
cb();
|
|
});
|
|
});
|
|
|
|
gulp.task('webpack-dev-server', function() {
|
|
var configCopy = Object.create(webpackConfig);
|
|
|
|
for (var entry in configCopy.entry) {
|
|
configCopy.entry[entry]
|
|
.unshift('webpack-dev-server/client?http://127.0.0.1:8081/');
|
|
}
|
|
|
|
var 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(8081, '127.0.0.1', function(err) {
|
|
if (err) throw new gutil.PluginError('webpack-dev-server', err);
|
|
});
|
|
});
|
|
|
|
// Locale
|
|
|
|
var localeFiles = `${srcDir}/**/locale/*.json`;
|
|
|
|
gulp.task('locales', function() {
|
|
var streams = [];
|
|
|
|
for (var mod in modules)
|
|
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}`)));
|
|
}
|
|
|
|
return merge(streams);
|
|
});
|
|
|
|
// Routes
|
|
|
|
var routeFiles = `${srcDir}/**/routes.json`;
|
|
|
|
gulp.task('routes', function() {
|
|
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']);
|
|
});
|
|
|
|
// Services tests
|
|
|
|
gulp.task('test', callback => {
|
|
return require('./services_tests').start();
|
|
});
|
|
|
|
// E2E tests
|
|
|
|
gulp.task('e2e', callback => {
|
|
runSequence('docker', 'waitForMySQL', 'endToEndTests', callback);
|
|
});
|
|
|
|
gulp.task('waitForMySQL', callback => {
|
|
let maxInterval = 30000;
|
|
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('endToEndTests', callback => {
|
|
gulp.src('./e2e_tests.js')
|
|
.pipe(jasmine({reporter: 'none'}));
|
|
});
|
|
|
|
// Docker
|
|
|
|
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);
|
|
});
|
|
});
|
|
|
|
gulp.task('buildDockerDb', callback => {
|
|
exec('docker build -t dblocal:latest ./services/db', (err, stdout, stderr) => {
|
|
callback(err);
|
|
});
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|