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

983
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

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