Gulpfile: known bugs solved, performance improbements

This commit is contained in:
Juan Ferrer Toribio 2018-02-09 08:40:23 +01:00
parent fd14e29884
commit 7b7ee2662f
3 changed files with 776 additions and 453 deletions

View File

@ -1,19 +1,18 @@
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;
const PluginError = require('plugin-error');
const argv = require('minimist')(process.argv.slice(2));
const log = require('fancy-log');
// Configuration
const isWindows = /^win/.test(process.platform);
if (gutil.env.NODE_ENV)
process.env.NODE_ENV = gutil.env.NODE_ENV;
if (argv.NODE_ENV)
process.env.NODE_ENV = argv.NODE_ENV;
const env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
@ -23,9 +22,6 @@ 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`;
@ -42,65 +38,45 @@ gulp.task('default', () => {
return gulp.start('services', 'client');
});
gulp.task('client', ['clean'], () => {
gulp.task('client', ['build-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', async () => {
await runSequenceP('docker-start', 'services-only', 'nginx');
});
gulp.task('services-run', async () => {
gulp.task('services-only', 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', ['docker-rebuild'], async () => {
await runSequenceP('e2e-only');
});
gulp.task('e2e-run', () => {
gulp.task('e2e-only', () => {
const jasmine = require('gulp-jasmine');
gulp.src('./e2e_tests.js')
return 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('clean', ['build-clean', 'nginx-clean']);
gulp.task('i', ['install']);
gulp.task('install', () => {
const install = require('gulp-install');
let jsonFile = [];
const print = require('gulp-print');
let packageFiles = [];
let services = fs.readdirSync(servicesDir);
services.push('..');
services.forEach(service => {
jsonFile.push(`${servicesDir}/${service}/package.json`);
packageFiles.push(`${servicesDir}/${service}/package.json`);
});
return gulp.src(jsonFile)
return gulp.src(packageFiles)
.pipe(print(filepath => {
return `Installing packages in ${filepath}`;
}))
@ -117,6 +93,7 @@ gulp.task('build', ['clean'], () => {
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();
@ -146,49 +123,40 @@ gulp.task('docker-compose', async () => {
await fs.writeFile('./docker-compose.yml', ymlString);
});
gulp.task('build-clean', () => {
const del = require('del');
const files = [
`${buildDir}/*`,
`!${buildDir}/templates`,
`!${buildDir}/images`,
`docker-compose.yml`
];
return del(files, {force: true});
});
// 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', async () => {
await runSequenceP('nginx-stop', 'nginx-start');
});
gulp.task('nginx', ['nginx-stop'], async () => {
gulp.task('nginx-start', ['nginx-conf'], 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();
});
});
await execP(`${nginxBin} -c "${nginxConf}" -p "${nginxDir}"`);
});
gulp.task('nginx-stop', ['nginx-conf'], async () => {
gulp.task('nginx-stop', 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();
});
});
await execP(`${nginxBin} -c "${nginxConf}" -p "${nginxDir}" -s stop`);
} catch (e) {}
});
@ -218,11 +186,23 @@ gulp.task('nginx-conf', async () => {
await fs.writeFile(`${nginxTemp}/nginx.conf`, nginxConf);
});
gulp.task('nginx-clean', () => {
gulp.task('nginx-clean', ['nginx-stop'], () => {
const del = require('del');
return del([`${nginxTemp}/*`], {force: true});
});
async function nginxGetBin() {
if (isWindows)
return 'nginx';
try {
let nginxBin = '/usr/sbin/nginx';
await fs.stat(nginxBin);
return nginxBin;
} catch (e) {
return 'nginx';
}
}
async function getServices() {
let services;
let startPort = defaultPort + 1;
@ -248,18 +228,25 @@ async function getServices() {
// Webpack
gulp.task('webpack', function(cb) {
gulp.task('webpack', function(callback) {
const webpack = require('webpack');
const webpackConfig = require('./webpack.config.js');
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();
if (err) throw new PluginError('webpack', err);
log('[webpack]', stats.toString({colors: true}));
callback();
});
});
gulp.task('webpack-dev-server', function() {
const WebpackDevServer = require('webpack-dev-server');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config.js');
let configCopy = Object.create(webpackConfig);
for (let entry in configCopy.entry) {
@ -284,7 +271,7 @@ gulp.task('webpack-dev-server', function() {
chunkModules: false
}
}).listen(devServerPort, '127.0.0.1', function(err) {
if (err) throw new gutil.PluginError('webpack-dev-server', err);
if (err) throw new PluginError('webpack-dev-server', err);
});
});
@ -296,6 +283,7 @@ gulp.task('locales', function() {
const extend = require('gulp-extend');
const yaml = require('gulp-yaml');
const merge = require('merge-stream');
const modules = require('./client/modules.yml');
let streams = [];
@ -334,51 +322,93 @@ gulp.task('watch', function() {
// Docker
gulp.task('docker', callback => {
runSequence('docker-delete', 'docker-delete-image', 'docker-build', 'docker-run', callback);
gulp.task('docker-rebuild', async () => {
try {
await execP('docker rm -f dblocal');
} catch (e) {}
try {
await execP('docker rmi dblocal:latest');
} catch (e) {}
await runSequenceP('docker-run');
});
gulp.task('docker-wait', ['docker'], callback => {
gulp.task('docker-start', async () => {
let result;
try {
result = await execP('docker container inspect -f "{{json .State}}" dblocal');
} catch (err) {
return await runSequenceP('docker-run');
}
switch (JSON.parse(result.stdout).Status) {
case 'running':
return;
case 'exited':
return await execP('docker start dblocal');
default:
throw new Error(`Unknown docker status: ${status}`);
}
});
gulp.task('docker-run', async () => {
try {
await execP('docker image inspect -f "{{json .Id}}" dblocal');
} catch (err) {
await execP('docker build -t dblocal:latest ./services/db');
}
await execP('docker run -d --name dblocal -p 3306:3306 dblocal');
await runSequenceP('docker-wait');
});
gulp.task('docker-wait', callback => {
let maxInterval = 30 * 60000;
let interval = 1000;
let timer = 0;
console.log('Waiting for MySQL init process...');
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.')) {
exec('docker logs --tail 1 dblocal', (err, stdout, stderr) => {
if (stderr.includes('starting as process 1') || err) {
clearInterval(waitForLocaldb);
callback(err);
}
});
} else {
console.log(`MySQL connection not established whithin ${maxInterval / 1000} secs!`);
clearInterval(waitForLocaldb);
callback(new Error(`MySQL not initialized whithin ${maxInterval / 1000} secs`));
}
}, interval);
});
gulp.task('docker-run', callback => {
exec('docker run -d --name dblocal -p 3306:3306 dblocal', (err, stdout, stderr) => {
callback(err);
});
});
// Helpers
gulp.task('docker-build', callback => {
exec('docker build -t dblocal:latest ./services/db', (err, stdout, stderr) => {
callback(err);
function execP(command) {
return new Promise((resolve, reject) => {
exec(command, (err, stdout, stderr) => {
if (err)
reject(err);
else
resolve({
stdout: stdout,
stderr: stderr
});
});
});
}
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);
function runSequenceP() {
return new Promise((resolve, reject) => {
let args = Array.prototype.slice.call(arguments);
args.push(err => {
if (err)
reject(err);
else
resolve();
});
runSequence(...args);
});
}

983
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -25,10 +25,10 @@
},
"devDependencies": {
"angular-mocks": "^1.6.6",
"babel": "^6.5.2",
"babel-core": "^6.22.1",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.22.0",
"babel": "^6.23.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"cors": "^2.8.1",
"css-loader": "^0.25.0",
"del": "^2.2.2",
@ -40,6 +40,7 @@
"eslint-config-xo": "^0.17.0",
"eslint-plugin-angular": "^1.4.1",
"eslint-plugin-jasmine": "^2.8.4",
"fancy-log": "^1.3.2",
"file-loader": "^1.1.6",
"gulp": "^3.9.1",
"gulp-concat": "^2.6.0",
@ -47,7 +48,6 @@
"gulp-install": "^1.1.0",
"gulp-jasmine": "^3.0.0",
"gulp-print": "^2.0.1",
"gulp-util": "^3.0.7",
"gulp-wrap": "^0.13.0",
"gulp-yaml": "^1.0.1",
"html-loader": "^0.4.4",
@ -61,11 +61,13 @@
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.9",
"merge-stream": "^1.0.1",
"minimist": "^1.2.0",
"mustache": "^2.3.0",
"mysql": "^2.15.0",
"nightmare": "^2.10.0",
"node-sass": "^4.7.2",
"nodemon": "^1.12.1",
"plugin-error": "^1.0.1",
"raw-loader": "*",
"run-sequence": "^2.2.0",
"sass-loader": "^6.0.6",