2018-02-04 18:39:56 +00:00
|
|
|
require('require-yaml');
|
2018-02-03 19:14:44 +00:00
|
|
|
const gulp = require('gulp');
|
|
|
|
const runSequence = require('run-sequence');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const exec = require('child_process').exec;
|
2018-02-09 07:40:23 +00:00
|
|
|
const PluginError = require('plugin-error');
|
|
|
|
const argv = require('minimist')(process.argv.slice(2));
|
|
|
|
const log = require('fancy-log');
|
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-09 07:40:23 +00:00
|
|
|
if (argv.NODE_ENV)
|
|
|
|
process.env.NODE_ENV = argv.NODE_ENV;
|
2018-02-07 14:33:43 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
|
2017-01-31 13:13:06 +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';
|
2018-02-13 21:40:02 +00:00
|
|
|
let servicesDir = './services';
|
2018-12-27 11:54:16 +00:00
|
|
|
let services = require('./modules.yml');
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
let wpConfig = require('./webpack.config.yml');
|
|
|
|
let buildDir = wpConfig.buildDir;
|
|
|
|
let devServerPort = wpConfig.devServerPort;
|
|
|
|
|
|
|
|
let nginxDir = `${servicesDir}/nginx`;
|
2018-02-04 18:39:56 +00:00
|
|
|
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));
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
let defaultService = proxyConf.main;
|
|
|
|
let defaultPort = proxyConf.defaultPort;
|
2018-02-03 21:53:02 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
// Development
|
2017-05-16 10:37:48 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
gulp.task('default', () => {
|
2018-12-21 10:36:44 +00:00
|
|
|
return gulp.start('client', 'services');
|
2018-03-13 10:15:39 +00:00
|
|
|
});
|
|
|
|
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('client', ['build-clean'], async() => {
|
2018-02-20 09:00:19 +00:00
|
|
|
await runSequenceP(['routes', 'locales'], 'watch', 'webpack-dev-server');
|
2017-05-16 10:37:48 +00:00
|
|
|
});
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Starts all backend services, including the nginx proxy and the database.
|
|
|
|
*/
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('services', async() => {
|
2018-11-02 09:48:15 +00:00
|
|
|
await runSequenceP('docker-start', 'services-only', 'nginx');
|
2018-01-31 13:56:55 +00:00
|
|
|
});
|
2018-01-31 11:17:17 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
2018-02-14 09:03:00 +00:00
|
|
|
* Starts backend services.
|
2018-02-13 21:40:02 +00:00
|
|
|
*/
|
2018-12-21 10:36:44 +00:00
|
|
|
gulp.task('services-only', callback => {
|
2018-12-27 11:54:16 +00:00
|
|
|
let app = require(`./loopback/server/server`);
|
2018-12-21 10:36:44 +00:00
|
|
|
app.start(defaultPort);
|
|
|
|
app.on('started', callback);
|
2017-06-02 13:23:19 +00:00
|
|
|
});
|
2018-01-11 07:12:21 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
2018-02-13 21:45:30 +00:00
|
|
|
* Runs the e2e tests, restoring the fixtures first.
|
2018-02-13 21:40:02 +00:00
|
|
|
*/
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('e2e', ['docker'], async() => {
|
2018-02-09 07:40:23 +00:00
|
|
|
await runSequenceP('e2e-only');
|
2018-02-01 07:48:54 +00:00
|
|
|
});
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Runs the e2e tests.
|
|
|
|
*/
|
2018-02-09 07:40:23 +00:00
|
|
|
gulp.task('e2e-only', () => {
|
2018-02-03 19:14:44 +00:00
|
|
|
const jasmine = require('gulp-jasmine');
|
2018-09-05 11:01:21 +00:00
|
|
|
|
|
|
|
if (argv.show || argv.s)
|
|
|
|
process.env.E2E_SHOW = true;
|
|
|
|
|
2018-02-09 07:40:23 +00:00
|
|
|
return gulp.src('./e2e_tests.js')
|
2018-11-02 09:48:15 +00:00
|
|
|
.pipe(jasmine({reporter: 'none'}));
|
2018-02-01 07:48:54 +00:00
|
|
|
});
|
|
|
|
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('smokes', ['docker'], async() => {
|
2018-12-20 13:37:29 +00:00
|
|
|
await runSequenceP('smokes-only');
|
|
|
|
});
|
|
|
|
|
2018-03-09 19:04:03 +00:00
|
|
|
gulp.task('smokes-only', () => {
|
|
|
|
const jasmine = require('gulp-jasmine');
|
|
|
|
return gulp.src('./smokes_tests.js')
|
2018-11-02 09:48:15 +00:00
|
|
|
.pipe(jasmine({reporter: 'none'}));
|
2018-03-09 19:04:03 +00:00
|
|
|
});
|
|
|
|
|
2018-02-14 13:52:35 +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');
|
2018-12-27 11:54:16 +00:00
|
|
|
// gulp.src('./loopback/common/**/*[sS]pec.js')
|
2018-02-14 13:52:35 +00:00
|
|
|
// .pipe(jasmine(
|
|
|
|
// require('./services-test.config')
|
|
|
|
// ));
|
|
|
|
// });
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Cleans all generated project files.
|
|
|
|
*/
|
2018-02-09 07:40:23 +00:00
|
|
|
gulp.task('clean', ['build-clean', 'nginx-clean']);
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
2018-02-13 21:45:30 +00:00
|
|
|
* Alias for the 'install' task.
|
2018-02-13 21:40:02 +00:00
|
|
|
*/
|
2018-02-07 12:24:46 +00:00
|
|
|
gulp.task('i', ['install']);
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Installs node dependencies in all project directories.
|
|
|
|
*/
|
2018-01-29 11:37:54 +00:00
|
|
|
gulp.task('install', () => {
|
2018-02-03 19:14:44 +00:00
|
|
|
const install = require('gulp-install');
|
2018-02-09 07:40:23 +00:00
|
|
|
const print = require('gulp-print');
|
|
|
|
|
|
|
|
let packageFiles = [];
|
2018-02-03 21:53:02 +00:00
|
|
|
let services = fs.readdirSync(servicesDir);
|
2018-01-29 11:37:54 +00:00
|
|
|
services.forEach(service => {
|
2018-02-09 07:40:23 +00:00
|
|
|
packageFiles.push(`${servicesDir}/${service}/package.json`);
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2018-02-09 07:40:23 +00:00
|
|
|
return gulp.src(packageFiles)
|
2018-01-29 11:37:54 +00:00
|
|
|
.pipe(print(filepath => {
|
|
|
|
return `Installing packages in ${filepath}`;
|
|
|
|
}))
|
|
|
|
.pipe(install({
|
|
|
|
npm: ['--no-package-lock']
|
|
|
|
}));
|
2018-01-11 07:12:21 +00:00
|
|
|
});
|
2017-06-02 13:23:19 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
// Deployment
|
|
|
|
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('build', ['clean'], async() => {
|
2018-12-17 10:28:39 +00:00
|
|
|
await runSequenceP(['routes', 'locales', 'webpack', 'docker-compose', 'nginx-conf']);
|
2018-02-03 19:14:44 +00:00
|
|
|
});
|
|
|
|
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('docker-compose', async() => {
|
2018-02-03 19:14:44 +00:00
|
|
|
const yaml = require('js-yaml');
|
2018-02-09 07:40:23 +00:00
|
|
|
|
2018-02-05 18:34:04 +00:00
|
|
|
let compose = await fs.readFile('./docker-compose.tpl.yml', 'utf8');
|
2018-02-03 19:14:44 +00:00
|
|
|
let composeYml = yaml.safeLoad(compose);
|
|
|
|
|
2018-06-11 18:19:18 +00:00
|
|
|
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}-`;
|
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
for (let service of services) {
|
2018-06-11 10:31:11 +00:00
|
|
|
let dockerFile = `Dockerfile`;
|
2018-12-21 10:36:44 +00:00
|
|
|
let localDockerFile = `${__dirname}/services/${service}/Dockerfile`;
|
2018-06-11 10:31:11 +00:00
|
|
|
|
|
|
|
if (await fs.exists(localDockerFile))
|
|
|
|
dockerFile = localDockerFile;
|
2018-02-03 19:14:44 +00:00
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
composeYml.services[service] = {
|
2018-06-11 18:19:18 +00:00
|
|
|
build: {
|
|
|
|
context: `./services`,
|
|
|
|
dockerfile: dockerFile
|
|
|
|
},
|
|
|
|
ports: [`${service.port}:${defaultPort}`],
|
2018-06-12 09:24:43 +00:00
|
|
|
environment: {
|
2018-12-17 10:28:39 +00:00
|
|
|
NODE_ENV: '${NODE_ENV}'
|
2018-06-12 09:24:43 +00:00
|
|
|
}
|
2018-02-03 19:14:44 +00:00
|
|
|
};
|
2018-06-12 09:24:43 +00:00
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
composeYml.services.nginx.links.push(
|
2018-12-21 10:36:44 +00:00
|
|
|
`${service}:${namePrefix}${service}`
|
2018-02-03 19:14:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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}`,
|
2018-08-26 12:36:51 +00:00
|
|
|
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 09:24:43 +00:00
|
|
|
}
|
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
let ymlString = yaml.safeDump(composeYml);
|
|
|
|
await fs.writeFile('./docker-compose.yml', ymlString);
|
2018-01-08 14:06:20 +00:00
|
|
|
});
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Cleans all files generated by the 'build' task.
|
|
|
|
*/
|
2018-02-09 07:40:23 +00:00
|
|
|
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
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-05 18:22:38 +00:00
|
|
|
let nginxConf = 'temp/nginx.conf';
|
|
|
|
let nginxTemp = `${nginxDir}/temp`;
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Starts the nginx process, if it is started, restarts it.
|
|
|
|
*/
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('nginx', async() => {
|
2018-12-21 10:36:44 +00:00
|
|
|
await runSequenceP('nginx-start');
|
2018-02-09 07:40:23 +00:00
|
|
|
});
|
2018-02-05 18:22:38 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Starts the nginx process, generating it's configuration file first.
|
|
|
|
*/
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('nginx-start', ['nginx-conf'], async() => {
|
2018-02-05 18:22:38 +00:00
|
|
|
let nginxBin = await nginxGetBin();
|
|
|
|
|
|
|
|
if (isWindows)
|
|
|
|
nginxBin = `start /B ${nginxBin}`;
|
|
|
|
|
2018-12-21 10:36:44 +00:00
|
|
|
log(`Application available at http://${proxyConf.host}:${proxyConf.port}/`);
|
2018-02-09 07:40:23 +00:00
|
|
|
await execP(`${nginxBin} -c "${nginxConf}" -p "${nginxDir}"`);
|
2017-09-26 14:59:24 +00:00
|
|
|
});
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Stops the nginx process.
|
|
|
|
*/
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('nginx-stop', async() => {
|
2018-02-05 18:22:38 +00:00
|
|
|
try {
|
|
|
|
let nginxBin = await nginxGetBin();
|
|
|
|
await fs.stat(`${nginxTemp}/nginx.pid`);
|
2018-02-09 07:40:23 +00:00
|
|
|
await execP(`${nginxBin} -c "${nginxConf}" -p "${nginxDir}" -s stop`);
|
2018-02-05 18:22:38 +00:00
|
|
|
} catch (e) {}
|
|
|
|
});
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('nginx-conf', ['nginx-stop'], async() => {
|
2018-02-03 19:14:44 +00:00
|
|
|
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
|
|
|
|
2018-02-03 19:14:44 +00:00
|
|
|
let params = {
|
2018-12-21 10:36:44 +00:00
|
|
|
services: services,
|
2018-02-03 19:14:44 +00:00
|
|
|
defaultService: defaultService,
|
|
|
|
defaultPort: defaultPort,
|
2018-02-03 21:53:02 +00:00
|
|
|
devServerPort: devServerPort,
|
|
|
|
port: proxyConf.port,
|
|
|
|
host: proxyConf.host
|
2018-02-03 19:14:44 +00:00
|
|
|
};
|
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');
|
2018-02-03 19:14:44 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Cleans all files generated by nginx.
|
|
|
|
*/
|
2018-02-09 07:40:23 +00:00
|
|
|
gulp.task('nginx-clean', ['nginx-stop'], () => {
|
2018-02-04 18:39:56 +00:00
|
|
|
const del = require('del');
|
2018-02-07 08:51:51 +00:00
|
|
|
return del([`${nginxTemp}/*`], {force: true});
|
2018-02-04 18:39:56 +00:00
|
|
|
});
|
|
|
|
|
2018-02-09 07:40:23 +00:00
|
|
|
async function nginxGetBin() {
|
|
|
|
if (isWindows)
|
|
|
|
return 'nginx';
|
|
|
|
try {
|
|
|
|
let nginxBin = '/usr/sbin/nginx';
|
|
|
|
await fs.stat(nginxBin);
|
|
|
|
return nginxBin;
|
|
|
|
} catch (e) {
|
|
|
|
return 'nginx';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
// Webpack
|
|
|
|
|
2018-02-09 07:40:23 +00:00
|
|
|
gulp.task('webpack', function(callback) {
|
|
|
|
const webpack = 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
|
|
|
|
2018-12-20 13:37:29 +00:00
|
|
|
let compiler = webpack(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));
|
2018-02-09 07:40:23 +00:00
|
|
|
callback();
|
2017-01-31 13:13:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-12-20 13:37:29 +00:00
|
|
|
gulp.task('webpack-dev-server', function(callback) {
|
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];
|
|
|
|
let wdsAssets = [`webpack-dev-server/client?http://127.0.0.1:${devServer.port}/`];
|
|
|
|
if (Array.isArray(entry))
|
|
|
|
wdsAssets = wdsAssets.concat(entry);
|
|
|
|
else
|
|
|
|
wdsAssets.push(entry);
|
|
|
|
wpConfig.entry[entryName] = wdsAssets;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
// TODO: Keep the server alive or continue?
|
|
|
|
callback();
|
|
|
|
});
|
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.
|
|
|
|
*/
|
2017-01-31 13:13:06 +00:00
|
|
|
gulp.task('locales', function() {
|
2018-02-03 19:14:44 +00:00
|
|
|
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');
|
|
|
|
|
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
|
|
|
|
2018-12-27 11:54:16 +00:00
|
|
|
for (let mod of services)
|
|
|
|
localePaths[mod] = `${modulesDir}/${mod}`;
|
|
|
|
|
|
|
|
let baseMods = ['core', 'auth', 'salix'];
|
|
|
|
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())
|
2017-03-01 08:55:17 +00:00
|
|
|
.pipe(extend(`${lang}.json`))
|
|
|
|
.pipe(gulp.dest(`${buildDir}/locale/${mod}`)));
|
|
|
|
}
|
2018-12-20 13:37:29 +00:00
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
return merge(streams);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
|
2018-12-27 11:54:16 +00:00
|
|
|
let routeFiles = `${modulesDir}/*/front/routes.json`;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
gulp.task('routes', function() {
|
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));
|
2017-01-31 13:13:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Watch
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
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-01-29 11:37:54 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
2018-04-26 13:13:56 +00:00
|
|
|
* Rebuilds the docker, if already exists, destroys and
|
|
|
|
* rebuild it.
|
2018-02-13 21:40:02 +00:00
|
|
|
*/
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('docker', async() => {
|
2018-02-09 07:40:23 +00:00
|
|
|
try {
|
2018-05-09 06:33:44 +00:00
|
|
|
await execP('docker rm -fv dblocal');
|
2018-02-09 07:40:23 +00:00
|
|
|
} catch (e) {}
|
2018-04-26 13:13:56 +00:00
|
|
|
|
|
|
|
await runSequenceP('docker-run');
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2018-04-27 05:27:29 +00:00
|
|
|
* Rebuilds the docker image, if already exists, destroys and
|
2018-08-01 07:45:08 +00:00
|
|
|
* rebuild it. calls upon docker task afterwards.
|
2018-04-26 13:13:56 +00:00
|
|
|
*/
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('docker-build', async() => {
|
2018-04-26 13:13:56 +00:00
|
|
|
try {
|
2018-05-09 06:33:44 +00:00
|
|
|
await execP('docker rm -fv dblocal');
|
2018-04-26 13:13:56 +00:00
|
|
|
} catch (e) {}
|
2018-02-09 07:40:23 +00:00
|
|
|
try {
|
|
|
|
await execP('docker rmi dblocal:latest');
|
|
|
|
} catch (e) {}
|
2018-05-08 09:34:50 +00:00
|
|
|
try {
|
|
|
|
await execP('docker volume rm data');
|
|
|
|
} catch (e) {}
|
2018-02-09 07:40:23 +00:00
|
|
|
|
2018-04-26 13:13:56 +00:00
|
|
|
log('Building image...');
|
2018-05-08 09:34:50 +00:00
|
|
|
await execP('docker build -t dblocal:latest ./services/db');
|
2018-08-01 07:45:08 +00:00
|
|
|
|
|
|
|
await runSequenceP('docker');
|
2018-02-09 07:40:23 +00:00
|
|
|
});
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2018-02-13 21:40:02 +00:00
|
|
|
* you do not rebuild the docker you may be using an outdated version of it.
|
2018-03-12 08:19:50 +00:00
|
|
|
* See the 'docker' task for more info.
|
2018-02-13 21:40:02 +00:00
|
|
|
*/
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('docker-start', async() => {
|
2018-02-13 21:40:02 +00:00
|
|
|
let state;
|
2018-02-09 07:40:23 +00:00
|
|
|
try {
|
2018-02-13 21:40:02 +00:00
|
|
|
let result = await execP('docker container inspect -f "{{json .State}}" dblocal');
|
|
|
|
state = JSON.parse(result.stdout);
|
2018-02-09 07:40:23 +00:00
|
|
|
} catch (err) {
|
|
|
|
return await runSequenceP('docker-run');
|
|
|
|
}
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
switch (state.Status) {
|
2018-02-09 07:40:23 +00:00
|
|
|
case 'running':
|
|
|
|
return;
|
|
|
|
case 'exited':
|
|
|
|
return await execP('docker start dblocal');
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown docker status: ${status}`);
|
|
|
|
}
|
2017-10-24 06:56:18 +00:00
|
|
|
});
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Runs the docker, if the container or it's image doesn't exists builds them.
|
|
|
|
*/
|
2018-12-22 10:59:26 +00:00
|
|
|
gulp.task('docker-run', async() => {
|
2018-02-09 07:40:23 +00:00
|
|
|
try {
|
|
|
|
await execP('docker image inspect -f "{{json .Id}}" dblocal');
|
2018-05-08 09:34:50 +00:00
|
|
|
await execP('docker run -d --name dblocal --volume data:/data -p 3306:3306 dblocal');
|
2018-04-26 13:40:31 +00:00
|
|
|
await runSequenceP('docker-wait');
|
2018-02-09 07:40:23 +00:00
|
|
|
} catch (err) {
|
2018-04-30 06:46:41 +00:00
|
|
|
await runSequenceP('docker-build');
|
2018-02-09 07:40:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Waits until MySQL docker is started and ready to serve connections.
|
|
|
|
*/
|
2018-02-09 07:40:23 +00:00
|
|
|
gulp.task('docker-wait', callback => {
|
2018-02-13 21:40:02 +00:00
|
|
|
const mysql = require('mysql2');
|
|
|
|
|
|
|
|
let interval = 1;
|
|
|
|
let elapsedTime = 0;
|
2018-05-08 09:34:50 +00:00
|
|
|
let maxInterval = 30 * 60;
|
2018-02-13 21:40:02 +00:00
|
|
|
|
2018-02-09 07:40:23 +00:00
|
|
|
log('Waiting for MySQL init process...');
|
2018-02-13 21:40:02 +00:00
|
|
|
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
|
|
|
}
|
2018-02-13 21:40:02 +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'
|
2018-02-13 21:40:02 +00:00
|
|
|
});
|
|
|
|
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
|
|
|
|
2018-02-09 07:40:23 +00:00
|
|
|
// Helpers
|
2018-02-01 11:52:58 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Promisified version of exec().
|
|
|
|
*
|
|
|
|
* @param {String} command The exec command
|
|
|
|
* @return {Promise} The promise
|
|
|
|
*/
|
2018-02-09 07:40:23 +00:00
|
|
|
function execP(command) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
exec(command, (err, stdout, stderr) => {
|
|
|
|
if (err)
|
|
|
|
reject(err);
|
2018-12-20 13:37:29 +00:00
|
|
|
else {
|
2018-02-09 07:40:23 +00:00
|
|
|
resolve({
|
|
|
|
stdout: stdout,
|
|
|
|
stderr: stderr
|
|
|
|
});
|
2018-12-20 13:37:29 +00:00
|
|
|
}
|
2018-02-09 07:40:23 +00:00
|
|
|
});
|
2018-02-01 11:52:58 +00:00
|
|
|
});
|
2018-02-09 07:40:23 +00:00
|
|
|
}
|
2018-02-01 11:52:58 +00:00
|
|
|
|
2018-02-13 21:40:02 +00:00
|
|
|
/**
|
|
|
|
* Promisified version of runSequence().
|
|
|
|
*
|
|
|
|
* @param {String} args The list of gulp task names
|
|
|
|
* @return {Promise} The promise
|
|
|
|
*/
|
2018-12-21 10:36:44 +00:00
|
|
|
function runSequenceP(...args) {
|
2018-02-09 07:40:23 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-12-21 10:36:44 +00:00
|
|
|
args = Array.prototype.slice.call(args);
|
2018-02-09 07:40:23 +00:00
|
|
|
args.push(err => {
|
|
|
|
if (err)
|
|
|
|
reject(err);
|
|
|
|
else
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
runSequence(...args);
|
2018-02-01 11:52:58 +00:00
|
|
|
});
|
2018-02-09 07:40:23 +00:00
|
|
|
}
|