salix/gulpfile.js

442 lines
12 KiB
JavaScript
Raw Normal View History

require('require-yaml');
const gulp = require('gulp');
const PluginError = require('plugin-error');
const argv = require('minimist')(process.argv.slice(2));
const log = require('fancy-log');
const request = require('request');
const e2eConfig = require('./e2e/helpers/config.js');
const Docker = require('./db/docker.js');
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 langs = ['es', 'en'];
let srcDir = './front';
2018-12-27 11:54:16 +00:00
let modulesDir = './modules';
2019-01-25 22:02:29 +00:00
let buildDir = 'dist';
2019-02-11 21:57:48 +00:00
let backSources = [
'!node_modules',
'!loopback/locale/*.json',
'loopback',
'modules/*/back/**',
2019-02-06 15:08:23 +00:00
'modules/*/back/*',
'back',
'print'
];
// Development
2017-05-16 10:37:48 +00:00
const localesRoutes = gulp.parallel(locales, routes);
localesRoutes.description = `Builds locales and routes`;
const front = gulp.series(clean, gulp.parallel(localesRoutes, watch, webpackDevServer));
front.description = `Starts frontend service`;
2018-01-31 11:17:17 +00:00
function backOnly(done) {
2018-12-27 11:54:16 +00:00
let app = require(`./loopback/server/server`);
2019-01-25 22:02:29 +00:00
app.start();
app.on('started', done);
}
backOnly.description = `Starts backend service`;
function backWatch(done) {
const nodemon = require('gulp-nodemon');
2019-01-28 11:14:22 +00:00
// XXX: Workaround to avoid nodemon bug
// https://github.com/remy/nodemon/issues/1346
2020-06-17 12:03:14 +00:00
let commands = ['node --tls-min-v1.0 --inspect ./node_modules/gulp/bin/gulp.js'];
2019-01-29 20:00:27 +00:00
if (!isWindows) commands.unshift('sleep 1');
nodemon({
2019-01-29 20:00:27 +00:00
exec: commands.join(' && '),
2019-02-18 11:55:22 +00:00
ext: 'js html css json',
args: ['backOnly'],
watch: backSources,
done: done
});
}
backWatch.description = `Starts backend in watcher mode`;
const back = gulp.series(dockerStart, backWatch);
back.description = `Starts backend and database service`;
const defaultTask = gulp.parallel(front, back);
defaultTask.description = `Starts all application services`;
// Backend tests - Private method
async function launchBackTest(done) {
let err;
let dataSources = require('./loopback/server/datasources.json');
const container = new Docker();
2020-06-11 08:37:55 +00:00
await container.run(argv.ci);
dataSources = JSON.parse(JSON.stringify(dataSources));
2019-02-11 21:57:48 +00:00
Object.assign(dataSources.vn, {
host: container.dbConf.host,
port: container.dbConf.port
2019-02-11 21:57:48 +00:00
});
let bootOptions = {dataSources};
2019-02-13 09:00:56 +00:00
let app = require(`./loopback/server/server`);
2019-12-31 08:42:08 +00:00
2019-12-19 13:27:29 +00:00
try {
app.boot(bootOptions);
2019-12-31 08:42:08 +00:00
await new Promise((resolve, reject) => {
const jasmine = require('gulp-jasmine');
let options = {
errorOnFail: false,
2020-10-05 08:04:00 +00:00
config: {}
};
2019-12-31 08:42:08 +00:00
2020-06-11 08:37:55 +00:00
if (argv.ci) {
const reporters = require('jasmine-reporters');
options.reporter = new reporters.JUnitXmlReporter();
}
let backSpecFiles = [
'back/**/*.spec.js',
'loopback/**/*.spec.js',
'modules/*/back/**/*.spec.js'
];
gulp.src(backSpecFiles)
.pipe(jasmine(options))
.on('end', resolve)
.on('error', reject)
.resume();
});
2019-12-19 13:27:29 +00:00
} catch (e) {
2019-12-31 08:42:08 +00:00
err = e;
2019-12-19 13:27:29 +00:00
}
await app.disconnect();
await container.rm();
done();
if (err)
throw err;
2019-02-11 21:57:48 +00:00
}
launchBackTest.description = `Runs the backend tests once using a random container, can receive --ci arg to save reports on a xml file`;
// Backend tests
2019-01-24 11:01:35 +00:00
function backTest(done) {
const nodemon = require('gulp-nodemon');
nodemon({
2020-06-17 12:03:14 +00:00
exec: ['node --tls-min-v1.0 ./node_modules/gulp/bin/gulp.js'],
args: ['launchBackTest'],
watch: backSources,
2019-01-24 11:01:35 +00:00
done: done
});
}
backTest.description = `Watches for changes in modules to execute backTest task`;
// End to end tests
2019-11-25 08:13:20 +00:00
function e2eSingleRun() {
require('@babel/register')({presets: ['@babel/preset-env']});
require('@babel/polyfill');
const jasmine = require('gulp-jasmine');
const SpecReporter = require('jasmine-spec-reporter').SpecReporter;
2020-02-05 08:08:55 +00:00
if (argv.show || argv.s)
process.env.E2E_SHOW = true;
2019-11-25 08:13:20 +00:00
const specFiles = [
`${__dirname}/e2e/paths/01*/*[sS]pec.js`,
2019-12-12 07:37:35 +00:00
`${__dirname}/e2e/paths/02*/*[sS]pec.js`,
`${__dirname}/e2e/paths/03*/*[sS]pec.js`,
`${__dirname}/e2e/paths/04*/*[sS]pec.js`,
`${__dirname}/e2e/paths/05*/*[sS]pec.js`,
2020-01-26 23:48:00 +00:00
`${__dirname}/e2e/paths/06*/*[sS]pec.js`,
`${__dirname}/e2e/paths/07*/*[sS]pec.js`,
`${__dirname}/e2e/paths/08*/*[sS]pec.js`,
`${__dirname}/e2e/paths/09*/*[sS]pec.js`,
`${__dirname}/e2e/paths/10*/*[sS]pec.js`,
`${__dirname}/e2e/paths/11*/*[sS]pec.js`,
`${__dirname}/e2e/paths/12*/*[sS]pec.js`,
`${__dirname}/e2e/paths/13*/*[sS]pec.js`,
2020-01-26 23:48:00 +00:00
`${__dirname}/e2e/paths/**/*[sS]pec.js`
2019-11-25 08:13:20 +00:00
];
return gulp.src(specFiles).pipe(jasmine({
errorOnFail: false,
timeout: 30000,
2020-03-25 19:44:59 +00:00
config: {
random: false,
// TODO: Waiting for this option to be implemented
// https://github.com/jasmine/jasmine/issues/1533
stopSpecOnExpectationFailure: false
},
2019-11-25 08:13:20 +00:00
reporter: [
new SpecReporter({
spec: {
2020-04-08 09:24:40 +00:00
displayStacktrace: 'none',
2019-11-25 08:13:20 +00:00
displaySuccessful: true,
displayFailedSpec: true,
displaySpecDuration: true,
2020-04-08 09:24:40 +00:00
},
summary: {
2020-11-09 17:25:02 +00:00
displayStacktrace: 'raw',
2020-04-08 09:24:40 +00:00
displayPending: false
2020-11-09 13:52:25 +00:00
},
colors: {
enabled: true,
successful: 'brightGreen',
failed: 'brightRed'
},
2020-11-09 17:25:02 +00:00
// stacktrace: {
// filter: stacktrace => {
// const lines = stacktrace.split('\n');
// const filtered = [];
// for (let i = 1; i < lines.length; i++) {
// if (/e2e\/paths/.test(lines[i]))
// filtered.push(lines[i]);
// }
// return filtered.join('\n');
// }
// }
2019-11-25 08:13:20 +00:00
})
]
}));
2019-11-25 08:13:20 +00:00
}
e2e = gulp.series(docker, async function isBackendReady() {
const attempts = await backendStatus();
log(`Backend ready after ${attempts} attempt(s)`);
return attempts;
}, e2eSingleRun);
e2e.description = `Restarts database and runs the e2e tests`;
async function backendStatus() {
const milliseconds = 250;
return new Promise(resolve => {
let timer;
let attempts = 1;
timer = setInterval(() => {
const url = `${e2eConfig.url}/api/Applications/status`;
request.get(url, (err, res) => {
if (err || attempts > 100) // 250ms * 100 => 25s timeout
throw new Error('Could not connect to backend');
else if (res && res.body == 'true') {
clearInterval(timer);
resolve(attempts);
} else
attempts++;
});
}, milliseconds);
});
}
backendStatus.description = `Performs a simple requests to check the backend status`;
function install() {
const install = require('gulp-install');
const print = require('gulp-print');
2019-12-30 15:57:55 +00:00
let npmArgs = [];
if (argv.ci) npmArgs = ['--no-audit', '--prefer-offline'];
2019-01-22 10:16:14 +00:00
let packageFiles = ['front/package.json', 'print/package.json'];
return gulp.src(packageFiles)
.pipe(print(filepath => {
return `Installing packages in ${filepath}`;
}))
2019-12-30 15:57:55 +00:00
.pipe(install({npm: npmArgs}));
}
install.description = `Installs node dependencies in all directories`;
2018-06-12 09:24:43 +00:00
const i = gulp.series(install);
i.description = `Alias for the 'install' task`;
// Deployment
2018-06-12 09:24:43 +00:00
2019-01-25 22:02:29 +00:00
const build = gulp.series(clean, gulp.parallel(localesRoutes, webpack));
build.description = `Generates binaries and configuration files`;
function clean() {
const del = require('del');
const files = [
2018-12-27 15:10:55 +00:00
`${buildDir}/*`
];
return del(files, {force: true});
}
clean.description = `Cleans all files generated by the 'build' task`;
// Webpack
function webpack(done) {
const webpackCompile = require('webpack');
const merge = require('webpack-merge');
let wpConfig = require('./webpack.config.js');
wpConfig = merge(wpConfig, {});
let compiler = webpackCompile(wpConfig);
compiler.run(function(err, stats) {
if (err) throw new PluginError('webpack', err);
log('[webpack]', stats.toString(wpConfig.stats));
done();
});
}
webpack.description = `Transpiles application into files`;
function webpackDevServer(done) {
const webpack = require('webpack');
const merge = require('webpack-merge');
const WebpackDevServer = require('webpack-dev-server');
let wpConfig = require('./webpack.config.js');
wpConfig = merge(wpConfig, {});
let devServer = wpConfig.devServer;
for (let entryName in wpConfig.entry) {
let entry = wpConfig.entry[entryName];
if (!Array.isArray(entry))
entry = [entry];
let wdsAssets = [
`webpack-dev-server/client?http://localhost:${devServer.port}/`,
`webpack/hot/dev-server`
];
wpConfig.entry[entryName] = wdsAssets.concat(entry);
}
let compiler = webpack(wpConfig);
new WebpackDevServer(compiler, wpConfig.devServer)
.listen(devServer.port, devServer.host, function(err) {
if (err) throw new PluginError('webpack-dev-server', err);
2019-10-14 10:01:19 +00:00
// XXX: Keep the server alive or continue?
done();
});
}
webpackDevServer.description = `Transpiles application into memory`;
// Locale
2018-12-27 11:54:16 +00:00
let localeFiles = [
`${srcDir}/**/locale/*.yml`,
`${modulesDir}/*/front/**/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.
*
* @return {Stream} The merged gulp streams
*/
function locales() {
const mergeJson = require('gulp-merge-json');
const gulpFile = require('gulp-file');
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 fs = require('fs-extra');
2018-02-05 18:34:04 +00:00
let streams = [];
2018-12-27 11:54:16 +00:00
let localePaths = [];
let modules = fs.readdirSync(modulesDir);
for (let mod of modules)
2018-12-27 11:54:16 +00:00
localePaths[mod] = `${modulesDir}/${mod}`;
2019-01-25 22:02:29 +00:00
let baseMods = ['core', 'salix'];
2018-12-27 11:54:16 +00:00
for (let mod of baseMods)
localePaths[mod] = `${srcDir}/${mod}`;
for (let mod in localePaths) {
let path = localePaths[mod];
for (let lang of langs) {
2018-12-27 11:54:16 +00:00
let localeFiles = `${path}/**/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())
.pipe(mergeJson({fileName: `${lang}.json`}))
2017-03-01 08:55:17 +00:00
.pipe(gulp.dest(`${buildDir}/locale/${mod}`)));
}
}
for (let mod in localePaths) {
for (let lang of langs) {
let file = `${buildDir}/locale/${mod}/${lang}.json`;
if (fs.existsSync(file)) continue;
streams.push(gulpFile('en.json', '{}', {src: true})
.pipe(gulp.dest(`${buildDir}/locale/${mod}`)));
}
}
return merge(streams);
}
locales.description = `Generates client locale files`;
// Routes
2018-12-27 11:54:16 +00:00
let routeFiles = `${modulesDir}/*/front/routes.json`;
function routes() {
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));
}
routes.description = 'Merges all module routes file into one file';
// Watch
function watch(done) {
gulp.watch(routeFiles, gulp.series(routes));
gulp.watch(localeFiles, gulp.series(locales));
done();
}
watch.description = `Watches for changes in routes and locale files`;
2017-10-18 04:41:17 +00:00
2018-02-01 15:01:49 +00:00
// Docker
async function dockerStart() {
const container = new Docker('salix-db');
await container.start();
}
dockerStart.description = `Starts the salix-db container`;
async function docker() {
const container = new Docker('salix-db');
await container.run();
}
docker.description = `Runs the salix-db container`;
2018-02-01 11:52:58 +00:00
module.exports = {
default: defaultTask,
front,
back,
backOnly,
backWatch,
backTest,
launchBackTest,
e2e,
2019-01-09 14:41:15 +00:00
i,
2019-02-11 21:57:48 +00:00
install,
build,
clean,
webpack,
webpackDevServer,
routes,
2019-02-11 21:57:48 +00:00
locales,
localesRoutes,
watch,
docker,
2019-12-12 07:37:35 +00:00
backendStatus,
};