salix/gulpfile.js

590 lines
15 KiB
JavaScript
Raw Normal View History

require('require-yaml');
const gulp = require('gulp');
const runSequence = require('run-sequence');
const fs = require('fs-extra');
const exec = require('child_process').exec;
const PluginError = require('plugin-error');
const argv = require('minimist')(process.argv.slice(2));
const log = require('fancy-log');
2018-03-13 10:15:39 +00:00
const environment = require('gulp-env');
2017-10-18 04:41:17 +00:00
// Configuration
let isWindows = /^win/.test(process.platform);
if (argv.NODE_ENV)
process.env.NODE_ENV = argv.NODE_ENV;
let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
let langs = ['es', 'en'];
let srcDir = './client';
let servicesDir = './services';
let wpConfig = require('./webpack.config.yml');
let buildDir = wpConfig.buildDir;
let devServerPort = wpConfig.devServerPort;
let nginxDir = `${servicesDir}/nginx`;
let proxyConf = require(`${nginxDir}/config.yml`);
let proxyEnvFile = `${nginxDir}/config.${env}.yml`;
2018-02-03 21:53:02 +00:00
if (fs.existsSync(proxyEnvFile))
Object.assign(proxyConf, require(proxyEnvFile));
let defaultService = proxyConf.main;
let defaultPort = proxyConf.defaultPort;
2018-02-03 21:53:02 +00:00
// Development
2017-05-16 10:37:48 +00:00
gulp.task('default', () => {
return gulp.start('services', 'client');
2018-03-13 10:15:39 +00:00
});
gulp.task('environment', async () => {
await environment({
file: '.env.json'
});
2017-05-16 10:37:48 +00:00
});
gulp.task('client', ['build-clean'], async () => {
await runSequenceP(['routes', 'locales'], 'watch', 'webpack-dev-server');
2017-05-16 10:37:48 +00:00
});
/**
* Starts all backend services, including the nginx proxy and the database.
*/
gulp.task('services', async () => {
await runSequenceP('docker-start', 'services-only', 'nginx');
});
2018-01-31 11:17:17 +00:00
/**
2018-02-14 09:03:00 +00:00
* Starts backend services.
*/
gulp.task('services-only', ['environment'], async () => {
const services = await getServices();
for (let service of services)
require(service.index).start(service.port);
});
/**
2018-02-13 21:45:30 +00:00
* Runs the e2e tests, restoring the fixtures first.
*/
gulp.task('e2e', ['docker'], async () => {
await runSequenceP('e2e-only');
2018-02-01 07:48:54 +00:00
});
gulp.task('smokes', ['docker'], async () => {
2018-03-09 19:04:03 +00:00
await runSequenceP('smokes-only');
});
/**
* Runs the e2e tests.
*/
gulp.task('e2e-only', () => {
const jasmine = require('gulp-jasmine');
2018-09-05 11:01:21 +00:00
if (argv.show || argv.s)
process.env.E2E_SHOW = true;
return gulp.src('./e2e_tests.js')
.pipe(jasmine({reporter: 'none'}));
2018-02-01 07:48:54 +00:00
});
2018-03-09 19:04:03 +00:00
gulp.task('smokes-only', () => {
const jasmine = require('gulp-jasmine');
return gulp.src('./smokes_tests.js')
.pipe(jasmine({reporter: 'none'}));
2018-03-09 19:04:03 +00:00
});
/**
* Runs the backend tests.
*/
// gulp.task('test', ['test-only'], async () => {
// gulp.watch('./services/**/*', ['test-only']);
// gulp.unwatch('./services/node_modules');
// });
// gulp.task('test-only', () => {
// const jasmine = require('gulp-jasmine');
// gulp.src('./services/loopback/common/**/*[sS]pec.js')
// .pipe(jasmine(
// require('./services-test.config')
// ));
// });
/**
* Cleans all generated project files.
*/
gulp.task('clean', ['build-clean', 'nginx-clean']);
/**
2018-02-13 21:45:30 +00:00
* Alias for the 'install' task.
*/
2018-02-07 12:24:46 +00:00
gulp.task('i', ['install']);
/**
* Installs node dependencies in all project directories.
*/
gulp.task('install', () => {
const install = require('gulp-install');
const print = require('gulp-print');
let packageFiles = [];
2018-02-03 21:53:02 +00:00
let services = fs.readdirSync(servicesDir);
services.forEach(service => {
packageFiles.push(`${servicesDir}/${service}/package.json`);
});
return gulp.src(packageFiles)
.pipe(print(filepath => {
return `Installing packages in ${filepath}`;
}))
.pipe(install({
npm: ['--no-package-lock']
}));
});
// Deployment
gulp.task('build', ['clean'], async () => {
2018-03-13 10:15:39 +00:00
await runSequenceP(['environment', 'routes', 'locales', 'webpack', 'docker-compose', 'nginx-conf']);
});
2018-02-03 22:07:28 +00:00
gulp.task('docker-compose', async () => {
const yaml = require('js-yaml');
2018-02-05 18:34:04 +00:00
let compose = await fs.readFile('./docker-compose.tpl.yml', 'utf8');
let composeYml = yaml.safeLoad(compose);
let services = await getServices();
let imageTag = 'latest';
if (process.env.BUILD_NUMBER)
imageTag = process.env.BUILD_NUMBER;
let namePrefix = '';
if (process.env.BRANCH_NAME)
namePrefix = `${process.env.BRANCH_NAME}-`;
for (let service of services) {
2018-06-11 10:31:11 +00:00
let dockerFile = `Dockerfile`;
let localDockerFile = `${__dirname}/services/${service.name}/Dockerfile`;
if (await fs.exists(localDockerFile))
dockerFile = localDockerFile;
composeYml.services[service.name] = {
build: {
context: `./services`,
dockerfile: dockerFile
},
ports: [`${service.port}:${defaultPort}`],
2018-06-12 09:24:43 +00:00
environment: {
NODE_ENV: '${NODE_ENV}',
salixHost: '${salixHost}',
salixPort: '${salixPort}',
salixUser: '${salixUser}',
salixPassword: '${salixPassword}'
}
};
2018-06-12 09:24:43 +00:00
composeYml.services.nginx.links.push(
2018-06-12 09:24:43 +00:00
`${service.name}:${namePrefix}${service.name}`
);
}
2018-06-12 09:24:43 +00:00
for (let serviceName in composeYml.services) {
let service = composeYml.services[serviceName];
2018-06-12 09:59:34 +00:00
Object.assign(service, {
2018-06-12 09:24:43 +00:00
container_name: `${namePrefix}${serviceName}`,
2018-06-12 11:45:20 +00:00
image: `${serviceName}:${imageTag}`,
restart: 'unless-stopped',
2018-06-12 11:45:20 +00:00
volumes: ['/config:/config']
2018-06-12 09:59:34 +00:00
});
2018-06-12 10:04:15 +00:00
service.build.labels = {
'salix.tag': imageTag
};
2018-06-12 09:24:43 +00:00
}
let ymlString = yaml.safeDump(composeYml);
await fs.writeFile('./docker-compose.yml', ymlString);
});
/**
* Cleans all files generated by the 'build' task.
*/
gulp.task('build-clean', () => {
const del = require('del');
const files = [
`${buildDir}/*`,
`docker-compose.yml`
];
return del(files, {force: true});
});
2018-02-03 22:07:28 +00:00
// Nginx & services
let nginxConf = 'temp/nginx.conf';
let nginxTemp = `${nginxDir}/temp`;
/**
* Starts the nginx process, if it is started, restarts it.
*/
gulp.task('nginx', async () => {
await runSequenceP('nginx-stop', 'nginx-start');
});
/**
* Starts the nginx process, generating it's configuration file first.
*/
gulp.task('nginx-start', ['nginx-conf'], async () => {
let nginxBin = await nginxGetBin();
if (isWindows)
nginxBin = `start /B ${nginxBin}`;
log(`Application will be available at http://${proxyConf.host}:${proxyConf.port}/`);
await execP(`${nginxBin} -c "${nginxConf}" -p "${nginxDir}"`);
});
/**
* Stops the nginx process.
*/
gulp.task('nginx-stop', async () => {
try {
let nginxBin = await nginxGetBin();
await fs.stat(`${nginxTemp}/nginx.pid`);
await execP(`${nginxBin} -c "${nginxConf}" -p "${nginxDir}" -s stop`);
} catch (e) {}
});
/**
* Generates the nginx configuration file. If NODE_ENV is defined and the
* 'nginx.[environment].mst' file exists, it is used as a template, otherwise,
* the 'nginx.mst' template file is used.
*/
gulp.task('nginx-conf', ['nginx-stop'], async () => {
const mustache = require('mustache');
2018-02-07 12:24:46 +00:00
if (!await fs.exists(nginxTemp))
await fs.mkdir(nginxTemp);
2018-02-07 08:51:51 +00:00
let params = {
2018-02-03 21:53:02 +00:00
services: await getServices(),
defaultService: defaultService,
defaultPort: defaultPort,
2018-02-03 21:53:02 +00:00
devServerPort: devServerPort,
port: proxyConf.port,
host: proxyConf.host
};
2018-02-03 21:53:02 +00:00
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);
2018-02-07 08:51:51 +00:00
await fs.writeFile(`${nginxTemp}/nginx.conf`, nginxConf);
2018-02-03 22:07:28 +00:00
});
/**
* Cleans all files generated by nginx.
*/
gulp.task('nginx-clean', ['nginx-stop'], () => {
const del = require('del');
2018-02-07 08:51:51 +00:00
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';
}
}
2018-02-03 22:07:28 +00:00
async function getServices() {
2018-02-07 12:24:46 +00:00
let services;
2018-02-03 22:07:28 +00:00
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(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 PluginError('webpack', err);
log('[webpack]', stats.toString({colors: true}));
callback();
});
});
2017-10-05 10:38:28 +00:00
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)
configCopy.entry[entry]
.unshift(`webpack-dev-server/client?http://127.0.0.1:${devServerPort}/`);
let compiler = webpack(configCopy);
2017-03-01 08:55:17 +00:00
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 PluginError('webpack-dev-server', err);
});
});
// Locale
2018-02-07 12:24:46 +00:00
let localeFiles = `${srcDir}/**/locale/*.yml`;
/**
* Mixes all locale files into one JSON file per module and language. It looks
* recursively in all project directories for locale folders with per language
* yaml translation files.
*/
gulp.task('locales', function() {
const extend = require('gulp-extend');
2018-02-04 14:46:46 +00:00
const yaml = require('gulp-yaml');
2018-02-05 18:34:04 +00:00
const merge = require('merge-stream');
const modules = require('./client/modules.yml');
2018-02-05 18:34:04 +00:00
let streams = [];
for (let mod in modules)
for (let lang of langs) {
2018-02-04 14:46:46 +00:00
let localeFiles = `./client/${mod}/**/locale/${lang}.yml`;
2017-03-01 08:55:17 +00:00
streams.push(gulp.src(localeFiles)
2018-02-04 14:46:46 +00:00
.pipe(yaml())
2017-03-01 08:55:17 +00:00
.pipe(extend(`${lang}.json`))
.pipe(gulp.dest(`${buildDir}/locale/${mod}`)));
}
return merge(streams);
});
// Routes
let routeFiles = `${srcDir}/**/routes.json`;
gulp.task('routes', function() {
2018-02-05 18:34:04 +00:00
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']);
});
2017-10-18 04:41:17 +00:00
2018-02-01 15:01:49 +00:00
// Docker
/**
2018-04-26 13:13:56 +00:00
* Rebuilds the docker, if already exists, destroys and
* rebuild it.
*/
2018-03-09 19:04:03 +00:00
gulp.task('docker', async () => {
try {
await execP('docker rm -fv dblocal');
} catch (e) {}
2018-04-26 13:13:56 +00:00
await runSequenceP('docker-run');
});
/**
* Rebuilds the docker image, if already exists, destroys and
* rebuild it. calls upon docker task afterwards.
2018-04-26 13:13:56 +00:00
*/
gulp.task('docker-build', async () => {
2018-04-26 13:13:56 +00:00
try {
await execP('docker rm -fv dblocal');
2018-04-26 13:13:56 +00:00
} catch (e) {}
try {
await execP('docker rmi dblocal:latest');
} catch (e) {}
try {
await execP('docker volume rm data');
} catch (e) {}
2018-04-26 13:13:56 +00:00
log('Building image...');
await execP('docker build -t dblocal:latest ./services/db');
await runSequenceP('docker');
});
/**
* Does the minium effort to start the docker, if it doesn't exists calls
2018-02-13 21:45:30 +00:00
* the 'docker-run' task, if it is started does nothing. Keep in mind that when
* you do not rebuild the docker you may be using an outdated version of it.
* See the 'docker' task for more info.
*/
gulp.task('docker-start', async () => {
let state;
try {
let result = await execP('docker container inspect -f "{{json .State}}" dblocal');
state = JSON.parse(result.stdout);
} catch (err) {
return await runSequenceP('docker-run');
}
switch (state.Status) {
case 'running':
return;
case 'exited':
return await execP('docker start dblocal');
default:
throw new Error(`Unknown docker status: ${status}`);
}
});
/**
* Runs the docker, if the container or it's image doesn't exists builds them.
*/
gulp.task('docker-run', async () => {
try {
await execP('docker image inspect -f "{{json .Id}}" dblocal');
await execP('docker run -d --name dblocal --volume data:/data -p 3306:3306 dblocal');
await runSequenceP('docker-wait');
} catch (err) {
2018-04-30 06:46:41 +00:00
await runSequenceP('docker-build');
}
});
/**
* Waits until MySQL docker is started and ready to serve connections.
*/
gulp.task('docker-wait', callback => {
const mysql = require('mysql2');
let interval = 1;
let elapsedTime = 0;
let maxInterval = 30 * 60;
log('Waiting for MySQL init process...');
checker();
async function checker() {
elapsedTime += interval;
let state;
try {
let result = await execP('docker container inspect -f "{{json .State}}" dblocal');
state = JSON.parse(result.stdout);
} catch (err) {
return callback(new Error(err.message));
2018-02-01 07:48:54 +00:00
}
if (state.Status === 'exited')
return callback(new Error('Docker exited, please see the docker logs for more info'));
let conn = mysql.createConnection({
host: 'localhost',
2018-03-13 10:15:39 +00:00
user: 'root',
password: 'root'
});
conn.on('error', () => {});
conn.connect(err => {
conn.destroy();
if (!err) return callback();
if (elapsedTime >= maxInterval)
callback(new Error(`MySQL not initialized whithin ${elapsedTime} secs`));
else
setTimeout(checker, interval * 1000);
});
}
2018-02-01 07:48:54 +00:00
});
2018-02-01 11:52:58 +00:00
// Helpers
2018-02-01 11:52:58 +00:00
/**
* Promisified version of exec().
*
* @param {String} command The exec command
* @return {Promise} The promise
*/
function execP(command) {
return new Promise((resolve, reject) => {
exec(command, (err, stdout, stderr) => {
if (err)
reject(err);
else
resolve({
stdout: stdout,
stderr: stderr
});
});
2018-02-01 11:52:58 +00:00
});
}
2018-02-01 11:52:58 +00:00
/**
* Promisified version of runSequence().
*
* @param {String} args The list of gulp task names
* @return {Promise} The promise
*/
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);
2018-02-01 11:52:58 +00:00
});
}