Refactor gulp and config

This commit is contained in:
Juan Ferrer Toribio 2018-02-03 22:53:02 +01:00
parent 2415619a13
commit e7d3ff9272
14 changed files with 112 additions and 99 deletions

View File

@ -13,9 +13,8 @@ const path = require('path');
// Configuration
const defaultService = 'salix';
const defaultPort = 3000;
const devServerPort = 8081;
const isWindows = /^win/.test(process.platform);
const env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
const langs = ['es', 'en'];
const srcDir = './client';
@ -26,6 +25,16 @@ const buildDir = `${nginxDir}/static`;
const modules = require('./client/modules.json');
const webpackConfig = require('./webpack.config.js');
let proxyConf = require(`${nginxDir}/config.json`);
let proxyEnvFile = `${nginxDir}/config.${env}.json`;
if (fs.existsSync(proxyEnvFile))
Object.assign(proxyConf, require(proxyEnvFile));
const defaultService = proxyConf.main;
const defaultPort = proxyConf.defaultPort;
const devServerPort = proxyConf.devServerPort;
// Development
gulp.task('default', () => {
@ -37,26 +46,25 @@ gulp.task('client', ['clean'], () => {
});
gulp.task('services', callback => {
let isWindows = /^win/.test(process.platform);
let command = isWindows ? 'docker inspect dblocal | findstr Status' : 'docker inspect dblocal | grep Status';
let command = isWindows
? '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) {
if (isNotRunning)
runSequence('docker-wait', 'services-run', callback);
} else {
else
runSequence('services-run', callback);
}
});
});
gulp.task('services-run', async () => {
const services = await getServices();
for (let service of services)
require(service.index).start(service.port);
await renderNginxConf(services, 'development');
return gulp.start('nginx');
});
@ -72,13 +80,13 @@ gulp.task('e2e-run', () => {
gulp.task('clean', () => {
const del = require('del');
return del([`${buildDir}/*`, `!${buildDir}/templates`, `!${buildDir}/images`], {force: true});
return del([`${buildDir}/*`, `${nginxDir}/temp/*`, `!${buildDir}/templates`, `!${buildDir}/images`], {force: true});
});
gulp.task('install', () => {
const install = require('gulp-install');
const jsonFile = [];
const services = fs.readdirSync(servicesDir);
let jsonFile = [];
let services = fs.readdirSync(servicesDir);
services.push('..');
services.forEach(service => {
jsonFile.push(`${servicesDir}/${service}/package.json`);
@ -98,7 +106,7 @@ gulp.task('build', ['clean'], () => {
return gulp.start('routes', 'locales', 'webpack', 'build-conf');
});
gulp.task('build-conf', async callback => {
gulp.task('build-conf', async () => {
let compose = await fs.readFile('./docker-compose.tpl.yml', 'utf8');
const yaml = require('js-yaml');
@ -129,58 +137,73 @@ gulp.task('build-conf', async callback => {
let ymlString = yaml.safeDump(composeYml);
await fs.writeFile('./docker-compose.yml', ymlString);
await renderNginxConf(services, 'production');
await buildNginxConf();
});
// Services
async function getServices() {
let startPort = defaultPort + 1;
let servicesData = [];
let services;
const services = fs.readdirSync(servicesDir);
async function getServices() {
if (services) return services;
let startPort = defaultPort + 1;
services = [];
const serviceDirs = await fs.readdir(servicesDir);
const exclude = ['loopback'];
for (let service of services) {
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++;
servicesData.push({
services.push({
name: service,
index: index,
port: port
});
}
return servicesData;
return services;
}
// Nginx
gulp.task('nginx', callback => {
let isWindows = /^win/.test(process.platform);
gulp.task('nginx', async () => {
await buildNginxConf();
let command = isWindows ? 'start.cmd' : 'start.sh';
command = path.join(`${nginxDir}/${command}`);
exec(command, (err, stdout, stderr) => {
if (stderr) console.log(stderr);
callback(err);
return new Promise((resolve, reject) => {
exec(command, (err, stdout, stderr) => {
if (stderr) console.log(stderr);
if (err) return reject(err);
resolve();
});
});
});
async function renderNginxConf(services, env) {
async function buildNginxConf() {
const mustache = require('mustache');
let params = {
services: services,
services: await getServices(),
defaultService: defaultService,
defaultPort: defaultPort,
devServerPort: devServerPort
devServerPort: devServerPort,
port: proxyConf.port,
host: proxyConf.host
};
let template = await fs.readFile(`${nginxDir}/nginx.${env}.mst`, 'utf8');
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);
await fs.writeFile(`${nginxDir}/temp/nginx.${env}.conf`, nginxConf);
await fs.writeFile(`${nginxDir}/temp/nginx.conf`, nginxConf);
}
// Webpack

View File

@ -1,5 +1,2 @@
mysqldump --defaults-extra-file=connect.ini --default-character-set=utf8 --no-data --triggers --routines --events --databases account util vn2008 vn edi bs bi pbx cache salix vncontrol hedera > 01-structure.sql

View File

@ -38,7 +38,7 @@ module.exports = function(Self) {
if (changes.isActive == false && finalState.isDefaultAddress)
throw new UserError('The default consignee can not be unchecked');
if (changes.isDefaultAddress == true) {
if (changes.isDefaultAddress == true && finalState.isActive != false) {
let filter = {
clientFk: finalState.clientFk,
isDefaultAddress: {neq: false}

View File

@ -32,15 +32,13 @@ module.exports = {
return this.response.status(401).send({message: 'Token expired'});
// Set proxy host
let host = this.request.headers.host.split(':')[0];
let proxy;
let proxy = config.proxy;
if (host == '127.0.0.1')
proxy = config.proxy.localhost;
else if (process.env.NODE_ENV == 'production')
proxy = config.proxy.salix;
else if (process.env.NODE_ENV == 'development')
proxy = config.proxy.testSalix;
if (!proxy)
proxy = {
host: 'localhost',
port: 80
};
this.request.proxyHost = `http://${proxy.host}:${proxy.port}`;
this.request.user = {

View File

@ -1,23 +1,20 @@
var path = require('path');
let defaultFile = 'datasources.json';
var fs = require('fs-extra');
function getFile(fileName) {
return require(path.join(__dirname, `/config/${fileName}`));
}
let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
try {
let envFile = 'datasources.development.json';
let config = require(`${__dirname}/config/datasources.json`);
let configEnvFile = `${__dirname}/config/datasources.${env}.json`;
if (process.env.NODE_ENV === 'test')
envFile = 'datasources.test.json';
if (fs.existsSync(configEnvFile))
Object.assign(config, require(configEnvFile));
config = getFile(envFile);
} catch (e) {
if (e.code == 'MODULE_NOT_FOUND')
config = getFile(defaultFile);
}
let proxyConf = require(`../../nginx/config.json`);
let proxyEnvFile = `../../nginx/config.${env}.json`;
config.proxy = require('../../nginx/config.json');
if (fs.existsSync(proxyEnvFile))
Object.assign(proxyConf, require(proxyEnvFile));
config.proxy = proxyConf;
config.package = require('../package.json');
module.exports = config;

View File

@ -3,7 +3,7 @@ FROM nginx
RUN rm /etc/nginx/nginx.conf
RUN rm /etc/nginx/conf.d/default.conf
COPY tmp/nginx.production.conf /etc/nginx/nginx.conf
COPY tmp/nginx.conf /etc/nginx/nginx.conf
COPY static /usr/share/nginx/html
RUN apt-get update && apt-get -y install vim dnsmasq dnsutils

View File

@ -1,14 +1,7 @@
{
"localhost": {
"host": "localhost",
"port": 5000
},
"testSalix": {
"host": "test-salix",
"port": 80
},
"salix": {
"host": "salix",
"port": 80
}
"main": "salix",
"defaultPort": 3000,
"devServerPort": 8081,
"host": "localhost",
"port": 5000
}

View File

@ -0,0 +1,4 @@
{
"host": "salix",
"port": 80
}

View File

@ -0,0 +1,4 @@
{
"host": "test-salix",
"port": 80
}

View File

@ -21,8 +21,8 @@ http {
scgi_temp_path temp/scgi;
server {
listen 5000;
server_name localhost;
listen {{port}};
server_name {{host}};
autoindex off;
{{#services}}

View File

@ -6,15 +6,15 @@ events {
}
http {
sendfile on;
gzip on;
default_type application/octet-stream;
resolver 127.0.0.1;
include /etc/nginx/mime.types;
sendfile on;
gzip on;
default_type application/octet-stream;
resolver 127.0.0.1;
include /etc/nginx/mime.types;
server {
listen 80;
server_name localhost;
listen {{port}};
server_name {{host}};
autoindex off;
root /usr/share/nginx/html;

View File

@ -2,7 +2,7 @@
set currentDir=%cd%
set nginxPrefix=%currentDir%\services\nginx
set nginxConf=%nginxPrefix%\temp\nginx.development.conf
set nginxConf=%nginxPrefix%\temp\nginx.conf
if "%1"=="" goto caseStart
if "%1"=="start" goto caseStart

View File

@ -2,7 +2,7 @@
nginxBin="/usr/sbin/nginx"
nginxPrefix="${PWD}/services/nginx"
nginxConf="$nginxPrefix/temp/nginx.development.conf"
nginxConf="$nginxPrefix/temp/nginx.conf"
if [ ! -f $nginxBin ]; then
nginxBin="nginx"

View File

@ -1,23 +1,20 @@
var path = require('path');
let defaultFile = 'datasources.json';
var fs = require('fs-extra');
function getFile(fileName) {
return require(path.join(__dirname, `/config/${fileName}`));
}
let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
try {
let envFile = 'datasources.development.json';
let config = require(`${__dirname}/config/datasources.json`);
let configEnvFile = `${__dirname}/config/datasources.${env}.json`;
if (process.env.NODE_ENV === 'test')
envFile = 'datasources.test.json';
if (fs.existsSync(configEnvFile))
Object.assign(config, require(configEnvFile));
config = getFile(envFile);
} catch (e) {
if (e.code == 'MODULE_NOT_FOUND')
config = getFile(defaultFile);
}
let proxyConf = require(`../../nginx/config.json`);
let proxyEnvFile = `../../nginx/config.${env}.json`;
config.proxy = require('../../nginx/config.json');
if (fs.existsSync(proxyEnvFile))
Object.assign(proxyConf, require(proxyEnvFile));
config.proxy = proxyConf;
config.package = require('../package.json');
module.exports = config;
module.exports = config;