2024-02-07 11:54:37 +00:00
|
|
|
/* eslint-disable no-console */
|
2018-02-04 18:39:56 +00:00
|
|
|
require('require-yaml');
|
2018-02-03 19:14:44 +00:00
|
|
|
const gulp = require('gulp');
|
2018-02-09 07:40:23 +00:00
|
|
|
const PluginError = require('plugin-error');
|
|
|
|
const log = require('fancy-log');
|
2024-01-25 23:09:24 +00:00
|
|
|
const Myt = require('@verdnatura/myt/myt');
|
|
|
|
const Run = require('@verdnatura/myt/myt-run');
|
|
|
|
const Start = require('@verdnatura/myt/myt-start');
|
2017-10-18 04:41:17 +00:00
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
// Configuration
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
let isWindows = /^win/.test(process.platform);
|
2018-02-07 14:33:43 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
let langs = ['es', 'en'];
|
2018-12-22 10:59:26 +00:00
|
|
|
let srcDir = './front';
|
2018-12-27 11:54:16 +00:00
|
|
|
let modulesDir = './modules';
|
2024-02-06 20:51:15 +00:00
|
|
|
let buildDir = 'front/dist';
|
2019-02-11 21:57:48 +00:00
|
|
|
|
2019-01-26 12:12:18 +00:00
|
|
|
let backSources = [
|
2019-01-28 08:07:46 +00:00
|
|
|
'!node_modules',
|
2020-10-28 13:12:57 +00:00
|
|
|
'!loopback/locale/*.json',
|
2019-01-26 12:12:18 +00:00
|
|
|
'loopback',
|
|
|
|
'modules/*/back/**',
|
2019-02-06 15:08:23 +00:00
|
|
|
'modules/*/back/*',
|
2019-01-28 08:07:46 +00:00
|
|
|
'back',
|
|
|
|
'print'
|
2019-01-26 12:12:18 +00:00
|
|
|
];
|
2019-01-26 01:30:46 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
// Development
|
2017-05-16 10:37:48 +00:00
|
|
|
|
2019-01-09 09:02:56 +00:00
|
|
|
const localesRoutes = gulp.parallel(locales, routes);
|
|
|
|
localesRoutes.description = `Builds locales and routes`;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2019-01-26 01:30:46 +00:00
|
|
|
const front = gulp.series(clean, gulp.parallel(localesRoutes, watch, webpackDevServer));
|
2019-01-09 09:02:56 +00:00
|
|
|
front.description = `Starts frontend service`;
|
2018-01-31 11:17:17 +00:00
|
|
|
|
2019-01-09 09:02:56 +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();
|
2019-01-09 09:02:56 +00:00
|
|
|
app.on('started', done);
|
|
|
|
}
|
|
|
|
backOnly.description = `Starts backend service`;
|
|
|
|
|
2019-01-26 12:12:18 +00:00
|
|
|
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');
|
2019-01-26 12:12:18 +00:00
|
|
|
|
|
|
|
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',
|
2019-01-26 12:12:18 +00:00
|
|
|
args: ['backOnly'],
|
|
|
|
watch: backSources,
|
|
|
|
done: done
|
|
|
|
});
|
|
|
|
}
|
2020-06-04 19:09:06 +00:00
|
|
|
backWatch.description = `Starts backend in watcher mode`;
|
2019-01-26 12:12:18 +00:00
|
|
|
|
|
|
|
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`;
|
2024-02-07 11:54:37 +00:00
|
|
|
async function install() {
|
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
|
|
|
|
console.log('-> Installing global packages...');
|
|
|
|
await pnpmInstall();
|
|
|
|
|
|
|
|
const modules = ['front', 'print'];
|
|
|
|
for (const module of modules) {
|
|
|
|
console.log(`-> Installing '${module}' packages...`);
|
|
|
|
await pnpmInstall(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function pnpmInstall(prefix) {
|
|
|
|
let args = ['install', '--prefer-offline'];
|
|
|
|
if (prefix) args = args.concat(['--prefix', prefix]);
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
stdio: [
|
|
|
|
process.stdin,
|
|
|
|
process.stdout,
|
|
|
|
process.stderr
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
const child = spawn('pnpm', args, options);
|
|
|
|
child.on('exit', code => {
|
|
|
|
if (code !== 0)
|
|
|
|
reject(new Error(`pnpm exit code ${code}`));
|
|
|
|
else
|
|
|
|
resolve(code);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-01-09 09:02:56 +00:00
|
|
|
}
|
|
|
|
install.description = `Installs node dependencies in all directories`;
|
2018-06-12 09:24:43 +00:00
|
|
|
|
2019-01-09 09:02:56 +00:00
|
|
|
const i = gulp.series(install);
|
|
|
|
i.description = `Alias for the 'install' task`;
|
2018-02-03 19:14:44 +00:00
|
|
|
|
2019-01-09 09:02:56 +00:00
|
|
|
// 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));
|
2019-01-09 09:02:56 +00:00
|
|
|
build.description = `Generates binaries and configuration files`;
|
2018-01-08 14:06:20 +00:00
|
|
|
|
2019-01-26 01:30:46 +00:00
|
|
|
function clean() {
|
2018-02-09 07:40:23 +00:00
|
|
|
const del = require('del');
|
|
|
|
const files = [
|
2018-12-27 15:10:55 +00:00
|
|
|
`${buildDir}/*`
|
2018-02-09 07:40:23 +00:00
|
|
|
];
|
|
|
|
return del(files, {force: true});
|
|
|
|
}
|
2019-01-26 01:30:46 +00:00
|
|
|
clean.description = `Cleans all files generated by the 'build' task`;
|
2018-02-09 07:40:23 +00:00
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
// Webpack
|
|
|
|
|
2019-01-09 09:02:56 +00:00
|
|
|
function webpack(done) {
|
|
|
|
const webpackCompile = require('webpack');
|
2018-12-20 13:37:29 +00:00
|
|
|
const merge = require('webpack-merge');
|
|
|
|
|
|
|
|
let wpConfig = require('./webpack.config.js');
|
|
|
|
wpConfig = merge(wpConfig, {});
|
2018-02-09 07:40:23 +00:00
|
|
|
|
2019-01-09 09:02:56 +00:00
|
|
|
let compiler = webpackCompile(wpConfig);
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
compiler.run(function(err, stats) {
|
2018-02-09 07:40:23 +00:00
|
|
|
if (err) throw new PluginError('webpack', err);
|
2018-12-20 13:37:29 +00:00
|
|
|
log('[webpack]', stats.toString(wpConfig.stats));
|
2019-01-09 09:02:56 +00:00
|
|
|
done();
|
2017-01-31 13:13:06 +00:00
|
|
|
});
|
2019-01-09 09:02:56 +00:00
|
|
|
}
|
|
|
|
webpack.description = `Transpiles application into files`;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2019-01-09 09:02:56 +00:00
|
|
|
function webpackDevServer(done) {
|
2018-02-09 07:40:23 +00:00
|
|
|
const webpack = require('webpack');
|
2018-12-20 13:37:29 +00:00
|
|
|
const merge = require('webpack-merge');
|
|
|
|
const WebpackDevServer = require('webpack-dev-server');
|
2018-02-09 07:40:23 +00:00
|
|
|
|
2018-12-20 13:37:29 +00:00
|
|
|
let wpConfig = require('./webpack.config.js');
|
|
|
|
wpConfig = merge(wpConfig, {});
|
|
|
|
|
|
|
|
let devServer = wpConfig.devServer;
|
|
|
|
|
|
|
|
for (let entryName in wpConfig.entry) {
|
|
|
|
let entry = wpConfig.entry[entryName];
|
2019-01-26 12:12:18 +00:00
|
|
|
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);
|
2018-12-20 13:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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?
|
2019-01-09 09:02:56 +00:00
|
|
|
done();
|
2018-12-20 13:37:29 +00:00
|
|
|
});
|
2019-01-09 09:02:56 +00:00
|
|
|
}
|
|
|
|
webpackDevServer.description = `Transpiles application into memory`;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
// Locale
|
|
|
|
|
2018-12-27 11:54:16 +00:00
|
|
|
let localeFiles = [
|
|
|
|
`${srcDir}/**/locale/*.yml`,
|
|
|
|
`${modulesDir}/*/front/**/locale/*.yml`
|
|
|
|
];
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2019-01-09 09:02:56 +00:00
|
|
|
*
|
|
|
|
* @return {Stream} The merged gulp streams
|
2018-02-13 21:40:02 +00:00
|
|
|
*/
|
2019-01-09 09:02:56 +00:00
|
|
|
function locales() {
|
|
|
|
const mergeJson = require('gulp-merge-json');
|
2019-03-26 11:05:53 +00:00
|
|
|
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');
|
2019-01-26 12:12:18 +00:00
|
|
|
const fs = require('fs-extra');
|
2018-02-05 18:34:04 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
let streams = [];
|
2018-12-27 11:54:16 +00:00
|
|
|
let localePaths = [];
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2019-01-26 01:30:46 +00:00
|
|
|
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];
|
2018-02-03 19:14:44 +00:00
|
|
|
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())
|
2019-01-09 09:02:56 +00:00
|
|
|
.pipe(mergeJson({fileName: `${lang}.json`}))
|
2017-03-01 08:55:17 +00:00
|
|
|
.pipe(gulp.dest(`${buildDir}/locale/${mod}`)));
|
|
|
|
}
|
2018-12-20 13:37:29 +00:00
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2019-03-26 11:05:53 +00:00
|
|
|
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}`)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
return merge(streams);
|
2019-01-09 09:02:56 +00:00
|
|
|
}
|
|
|
|
locales.description = `Generates client locale files`;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
// Routes
|
|
|
|
|
2018-12-27 11:54:16 +00:00
|
|
|
let routeFiles = `${modulesDir}/*/front/routes.json`;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2019-01-09 09:02:56 +00:00
|
|
|
function routes() {
|
2018-02-05 18:34:04 +00:00
|
|
|
const concat = require('gulp-concat');
|
|
|
|
const wrap = require('gulp-wrap');
|
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
return gulp.src(routeFiles)
|
2017-10-24 06:56:18 +00:00
|
|
|
.pipe(concat('routes.js', {newLine: ','}))
|
|
|
|
.pipe(wrap('var routes = [<%=contents%>\n];'))
|
|
|
|
.pipe(gulp.dest(buildDir));
|
2019-01-09 09:02:56 +00:00
|
|
|
}
|
|
|
|
routes.description = 'Merges all module routes file into one file';
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
// Watch
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2019-01-09 09:02:56 +00:00
|
|
|
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
|
2019-01-09 09:02:56 +00:00
|
|
|
async function dockerStart() {
|
2024-01-15 11:35:23 +00:00
|
|
|
const myt = new Myt();
|
|
|
|
await myt.init({workspace: __dirname});
|
|
|
|
await myt.run(Start);
|
|
|
|
await myt.deinit();
|
2019-01-09 09:02:56 +00:00
|
|
|
}
|
2024-01-29 12:48:02 +00:00
|
|
|
dockerStart.description = `Starts the DB container`;
|
2019-01-09 09:02:56 +00:00
|
|
|
|
2020-06-04 19:09:06 +00:00
|
|
|
async function docker() {
|
2024-01-15 11:35:23 +00:00
|
|
|
const myt = new Myt();
|
|
|
|
await myt.init({workspace: __dirname});
|
|
|
|
await myt.run(Run);
|
|
|
|
await myt.deinit();
|
2018-02-09 07:40:23 +00:00
|
|
|
}
|
2024-01-29 12:48:02 +00:00
|
|
|
docker.description = `Builds and starts the DB container`;
|
2018-02-01 11:52:58 +00:00
|
|
|
|
2019-01-09 09:02:56 +00:00
|
|
|
module.exports = {
|
|
|
|
default: defaultTask,
|
|
|
|
front,
|
|
|
|
back,
|
|
|
|
backOnly,
|
2019-01-26 12:12:18 +00:00
|
|
|
backWatch,
|
2019-01-09 14:41:15 +00:00
|
|
|
i,
|
2019-02-11 21:57:48 +00:00
|
|
|
install,
|
2019-01-09 09:02:56 +00:00
|
|
|
build,
|
2019-01-26 01:30:46 +00:00
|
|
|
clean,
|
2019-01-09 09:02:56 +00:00
|
|
|
webpack,
|
|
|
|
webpackDevServer,
|
|
|
|
routes,
|
2019-02-11 21:57:48 +00:00
|
|
|
locales,
|
2019-01-09 09:02:56 +00:00
|
|
|
localesRoutes,
|
|
|
|
watch,
|
2024-01-15 11:35:23 +00:00
|
|
|
dockerStart,
|
2022-05-13 15:09:41 +00:00
|
|
|
docker
|
2019-01-09 09:02:56 +00:00
|
|
|
};
|