YAML configuration, client module register simplified

This commit is contained in:
Juan Ferrer Toribio 2018-02-04 19:39:56 +01:00
parent 3e2e2f3c3a
commit 89c745244a
22 changed files with 171 additions and 189 deletions

View File

@ -1,59 +1,59 @@
import {module} from '../module'; import {module} from '../module';
import splitingRegister from './splitingRegister'; import splitingRegister from './splitingRegister';
factory.$inject = ['$translatePartialLoader', '$http', '$window', '$ocLazyLoad', '$q', '$translate']; factory.$inject = ['$http', '$window', '$ocLazyLoad', '$translatePartialLoader', '$translate'];
export function factory($translatePartialLoader, $http, $window, $ocLazyLoad, $q, $translate) { export function factory($http, $window, $ocLazyLoad, $translatePartialLoader, $translate) {
class ModuleLoader { class ModuleLoader {
constructor() { constructor() {
this._loadedModules = {}; this._loaded = {};
} }
load(moduleName, validations) { load(moduleName, validations) {
if (this._loadedModules[moduleName]) let loaded = this._loaded;
return;
this._loadedModules[moduleName] = true; if (loaded[moduleName])
return loaded[moduleName];
loaded[moduleName] = Promise.resolve(true);
let deps = splitingRegister.getDependencies(moduleName); let deps = splitingRegister.getDependencies(moduleName);
let modules = splitingRegister.modules; let depPromises = [];
if (deps)
for (let dep of deps)
depPromises.push(this.load(dep, validations));
loaded[moduleName] = new Promise((resolve, reject) => {
Promise.all(depPromises)
.then(() => {
let promises = []; let promises = [];
for (let dep of deps) { // FIXME: https://github.com/angular-translate/angular-translate/pull/1674
this._loadedModules[dep] = true; $translatePartialLoader.addPart(moduleName);
promises.push(modules[dep]()); promises.push($translate.refresh());
if (validations) if (validations)
promises.push(new Promise(resolve => { promises.push(new Promise(resolve => {
$http.get(`/${dep}/validations`).then( $http.get(`/${moduleName}/validations`).then(
json => this.onValidationsReady(json, resolve), json => this.onValidationsReady(json, resolve),
json => resolve() json => resolve()
); );
})); }));
$translatePartialLoader.addPart(dep); promises.push(new Promise(resolve => {
// FIXME: https://github.com/angular-translate/angular-translate/pull/1674 splitingRegister.modules[moduleName](resolve);
// promises.push($translate.refresh()); }));
setTimeout(() => $translate.refresh(), 500);
}
let ocDeps = deps.map(item => { Promise.all(promises)
return {name: item}; .then(() => {
this._loaded[moduleName] = true;
resolve($ocLazyLoad.load({name: moduleName}));
})
.catch(reject);
})
.catch(reject);
}); });
return new Promise(resolve => { return loaded[moduleName];
Promise.all(promises).then(
() => resolve($ocLazyLoad.load(ocDeps))
);
});
}
parseValidation(val) {
switch (val.validation) {
case 'custom':
// TODO: Reemplazar eval
val.bindedFunction = eval(`(${val.bindedFunction})`);
break;
case 'format':
val.with = new RegExp(val.with);
break;
}
} }
onValidationsReady(json, resolve) { onValidationsReady(json, resolve) {
let entities = json.data; let entities = json.data;
@ -69,6 +69,17 @@ export function factory($translatePartialLoader, $http, $window, $ocLazyLoad, $q
Object.assign($window.validations, json.data); Object.assign($window.validations, json.data);
resolve(); resolve();
} }
parseValidation(val) {
switch (val.validation) {
case 'custom':
// TODO: Replace eval
val.bindedFunction = eval(`(${val.bindedFunction})`);
break;
case 'format':
val.with = new RegExp(val.with);
break;
}
}
} }
return new ModuleLoader(); return new ModuleLoader();

View File

@ -1,29 +1,11 @@
class SplitingRegister { class SplitingRegister {
constructor() { constructor() {
this._graph = null; this.graph = null;
this._modules = {}; this.modules = {};
} }
get modules() { getDependencies(moduleName) {
return this._modules; return this.graph[moduleName];
}
getDependencies(dependency) {
var array = [];
array.push(dependency);
var first = this._graph[dependency];
if (first)
while (first.length > 0) {
dependency = first.shift();
array = array.concat(this.getDependencies(dependency));
}
return array;
}
registerGraph(graph) {
this._graph = graph;
}
register(moduleName, loader) {
this._modules[moduleName] = loader;
} }
} }

View File

@ -1,10 +0,0 @@
{
"salix": [],
"auth": [],
"core": [],
"client": [],
"production": [],
"route": [],
"locator": [],
"item": []
}

8
client/modules.yml Normal file
View File

@ -0,0 +1,8 @@
salix: []
auth: []
core: []
client: []
production: []
route: []
locator: []
item: []

View File

@ -1,5 +1,4 @@
import './module'; import './module';
import './spliting';
import './configroutes'; import './configroutes';
import './config'; import './config';
import './run'; import './run';

View File

@ -1,6 +1,6 @@
import './spliting';
import deps from 'client/modules.json';
import ngModule from './module'; import ngModule from './module';
import deps from 'client/modules.yml';
import modules from 'client/spliting';
import {splitingRegister} from 'core'; import {splitingRegister} from 'core';
function loader(moduleName, validations) { function loader(moduleName, validations) {
@ -13,7 +13,9 @@ function loader(moduleName, validations) {
config.$inject = ['$stateProvider', '$urlRouterProvider', 'aclServiceProvider', 'modulesFactoryProvider']; config.$inject = ['$stateProvider', '$urlRouterProvider', 'aclServiceProvider', 'modulesFactoryProvider'];
function config($stateProvider, $urlRouterProvider, aclServiceProvider, modulesFactory) { function config($stateProvider, $urlRouterProvider, aclServiceProvider, modulesFactory) {
splitingRegister.registerGraph(deps); splitingRegister.graph = deps;
splitingRegister.modules = modules;
let aclService = aclServiceProvider.$get(); let aclService = aclServiceProvider.$get();
function getParams(route) { function getParams(route) {

View File

@ -1,56 +0,0 @@
import * as core from 'core';
export const client = () => {
return new Promise(resolve => {
require.ensure([], () => {
require('client');
resolve('client');
}, 'client');
});
};
core.splitingRegister.register('client', client);
export const production = () => {
return new Promise(resolve => {
require.ensure([], () => {
require('production');
resolve('production');
}, 'production');
});
};
core.splitingRegister.register('production', production);
export const route = () => {
return new Promise(resolve => {
require.ensure([], () => {
require('route');
resolve('route');
}, 'route');
});
};
core.splitingRegister.register('route', route);
export const locator = () => {
return new Promise(resolve => {
require.ensure([], () => {
require('locator');
resolve('locator');
}, 'locator');
});
};
core.splitingRegister.register('locator', locator);
export const item = () => {
return new Promise(resolve => {
require.ensure([], () => {
require('item');
resolve('item');
}, 'item');
});
};
core.splitingRegister.register('item', item);

12
client/spliting.js Normal file
View File

@ -0,0 +1,12 @@
export default {
client:
cb => require.ensure([], () => cb(require('client'))),
production:
cb => require.ensure([], () => cb(require('production'))),
route:
cb => require.ensure([], () => cb(require('route'))),
locator:
cb => require.ensure([], () => cb(require('locator'))),
item:
cb => require.ensure([], () => cb(require('item')))
};

View File

@ -1,3 +1,4 @@
require('require-yaml');
const gulp = require('gulp'); const gulp = require('gulp');
const gutil = require('gulp-util'); const gutil = require('gulp-util');
const wrap = require('gulp-wrap'); const wrap = require('gulp-wrap');
@ -22,11 +23,11 @@ const servicesDir = './services';
const nginxDir = `${servicesDir}/nginx`; const nginxDir = `${servicesDir}/nginx`;
const buildDir = `${nginxDir}/static`; const buildDir = `${nginxDir}/static`;
const modules = require('./client/modules.json'); const modules = require('./client/modules.yml');
const webpackConfig = require('./webpack.config.js'); const webpackConfig = require('./webpack.config.js');
let proxyConf = require(`${nginxDir}/config.json`); let proxyConf = require(`${nginxDir}/config.yml`);
let proxyEnvFile = `${nginxDir}/config.${env}.json`; let proxyEnvFile = `${nginxDir}/config.${env}.yml`;
if (fs.existsSync(proxyEnvFile)) if (fs.existsSync(proxyEnvFile))
Object.assign(proxyConf, require(proxyEnvFile)); Object.assign(proxyConf, require(proxyEnvFile));
@ -80,7 +81,7 @@ gulp.task('e2e-run', () => {
gulp.task('clean', () => { gulp.task('clean', () => {
const del = require('del'); const del = require('del');
return del([`${buildDir}/*`, `${nginxDir}/temp/*`, `!${buildDir}/templates`, `!${buildDir}/images`], {force: true}); return del([`${buildDir}/*`, `!${buildDir}/templates`, `!${buildDir}/images`], {force: true});
}); });
gulp.task('install', () => { gulp.task('install', () => {
@ -144,7 +145,7 @@ gulp.task('nginx', ['nginx-conf'], callback => {
let command = isWindows ? 'start.cmd' : 'start.sh'; let command = isWindows ? 'start.cmd' : 'start.sh';
command = path.join(`${nginxDir}/${command}`); command = path.join(`${nginxDir}/${command}`);
exec(command, (err, stdout, stderr) => { exec(command, (err, stdout, stderr) => {
if (stderr) console.log(stderr); // if (stderr) console.log(stderr);
callback(err); callback(err);
}); });
}); });
@ -172,6 +173,11 @@ gulp.task('nginx-conf', async () => {
await fs.writeFile(`${nginxDir}/temp/nginx.conf`, nginxConf); await fs.writeFile(`${nginxDir}/temp/nginx.conf`, nginxConf);
}); });
gulp.task('nginx-clean', () => {
const del = require('del');
return del([`${nginxDir}/temp/*`], {force: true});
});
let services; let services;
async function getServices() { async function getServices() {

67
package-lock.json generated
View File

@ -132,19 +132,19 @@
"integrity": "sha1-cC9XTW0UBpADXqxkOV/jEfeYf7s=" "integrity": "sha1-cC9XTW0UBpADXqxkOV/jEfeYf7s="
}, },
"angular-translate": { "angular-translate": {
"version": "2.13.1", "version": "2.17.0",
"resolved": "https://registry.npmjs.org/angular-translate/-/angular-translate-2.13.1.tgz", "resolved": "https://registry.npmjs.org/angular-translate/-/angular-translate-2.17.0.tgz",
"integrity": "sha1-BWc+s3GYb8Ee3BUfWQKZ1+DEYig=", "integrity": "sha512-SudfI0R0Hhtvngc0X3wFChXQGmw90o95i+QPZ11LhJJryneTq8LR3+3E4E7jgHA4fu6TcswgcfZ9+cp5ckbUHw==",
"requires": { "requires": {
"angular": "1.6.8" "angular": "1.6.8"
} }
}, },
"angular-translate-loader-partial": { "angular-translate-loader-partial": {
"version": "2.13.1", "version": "2.17.0",
"resolved": "https://registry.npmjs.org/angular-translate-loader-partial/-/angular-translate-loader-partial-2.13.1.tgz", "resolved": "https://registry.npmjs.org/angular-translate-loader-partial/-/angular-translate-loader-partial-2.17.0.tgz",
"integrity": "sha1-U3g7SK7OUJFS1NwfZMIxaKaTadM=", "integrity": "sha512-pyRJcRc93iwiUnRnh9ZfehbQE/yxO5T6jmEqIvLEVz8gKLjDqDLKcaQFgPef9wCIN2n3e531YbStkkbSH3LYmQ==",
"requires": { "requires": {
"angular-translate": "2.13.1" "angular-translate": "2.17.0"
} }
}, },
"ansi-align": { "ansi-align": {
@ -267,7 +267,6 @@
"version": "1.0.9", "version": "1.0.9",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.9.tgz", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.9.tgz",
"integrity": "sha1-c9g7wmP4bpf4zE9rrhsOkKfSLIY=", "integrity": "sha1-c9g7wmP4bpf4zE9rrhsOkKfSLIY=",
"dev": true,
"requires": { "requires": {
"sprintf-js": "1.0.3" "sprintf-js": "1.0.3"
} }
@ -5329,7 +5328,7 @@
"fsevents": { "fsevents": {
"version": "1.1.3", "version": "1.1.3",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.1.3.tgz", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.1.3.tgz",
"integrity": "sha1-EfgjGPX+e7LNIpZaEI6TBiCCFtg=", "integrity": "sha512-WIr7iDkdmdbxu/Gh6eKEZJL6KPE74/5MEsf2whTOFNxbIoIixogroLdKYqB6FDav4Wavh/lZdzzd3b2KxIXC5Q==",
"dev": true, "dev": true,
"optional": true, "optional": true,
"requires": { "requires": {
@ -9980,9 +9979,9 @@
"dev": true "dev": true
}, },
"json-loader": { "json-loader": {
"version": "0.5.4", "version": "0.5.7",
"resolved": "https://registry.npmjs.org/json-loader/-/json-loader-0.5.4.tgz", "resolved": "https://registry.npmjs.org/json-loader/-/json-loader-0.5.7.tgz",
"integrity": "sha1-i6oTZaYy9Yo8RtIBdfxgAsluN94=", "integrity": "sha512-QLPs8Dj7lnf3e3QYS1zkCo+4ZwqOiF9d/nZnYozTISxXWCfNs9yuky5rJw4/W34s7POaNlbZmQGaB5NiXCbP4w==",
"dev": true "dev": true
}, },
"json-schema": { "json-schema": {
@ -12665,6 +12664,30 @@
"resolve-from": "1.0.1" "resolve-from": "1.0.1"
} }
}, },
"require-yaml": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/require-yaml/-/require-yaml-0.0.1.tgz",
"integrity": "sha1-LhsY2RPDuqcqWk03O28Tjd0sMr0=",
"requires": {
"js-yaml": "3.10.0"
},
"dependencies": {
"esprima": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.0.tgz",
"integrity": "sha512-oftTcaMu/EGrEIu904mWteKIv8vMuOgGYo7EhVJJN00R/EED9DCua/xxHRdYnKtcECzVg7xOWhflvJMnqcFZjw=="
},
"js-yaml": {
"version": "3.10.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.10.0.tgz",
"integrity": "sha512-O2v52ffjLa9VeM43J4XocZE//WT9N0IiwDa3KSHH7Tu8CtH+1qM8SIZvnsTh6v+4yFy5KUY3BHUVwjpfAWsjIA==",
"requires": {
"argparse": "1.0.9",
"esprima": "4.0.0"
}
}
}
},
"requires-port": { "requires-port": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz", "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
@ -12820,9 +12843,9 @@
} }
}, },
"sass-loader": { "sass-loader": {
"version": "4.0.2", "version": "4.1.1",
"resolved": "https://registry.npmjs.org/sass-loader/-/sass-loader-4.0.2.tgz", "resolved": "https://registry.npmjs.org/sass-loader/-/sass-loader-4.1.1.tgz",
"integrity": "sha1-phbrdwNmVD5k9UfIYw85xNp18V0=", "integrity": "sha1-ee+UaM8L9kbClSnh8sumvW5Rx7w=",
"dev": true, "dev": true,
"requires": { "requires": {
"async": "2.1.2", "async": "2.1.2",
@ -13535,8 +13558,7 @@
"sprintf-js": { "sprintf-js": {
"version": "1.0.3", "version": "1.0.3",
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw="
"dev": true
}, },
"sqlstring": { "sqlstring": {
"version": "2.3.0", "version": "2.3.0",
@ -14879,7 +14901,7 @@
"async": "2.1.2", "async": "2.1.2",
"enhanced-resolve": "3.0.3", "enhanced-resolve": "3.0.3",
"interpret": "1.0.1", "interpret": "1.0.1",
"json-loader": "0.5.4", "json-loader": "0.5.7",
"loader-runner": "2.3.0", "loader-runner": "2.3.0",
"loader-utils": "0.2.16", "loader-utils": "0.2.16",
"memory-fs": "0.4.1", "memory-fs": "0.4.1",
@ -15264,6 +15286,15 @@
"integrity": "sha1-MGxUODXwnuGkyyO3vOmrNByRzdQ=", "integrity": "sha1-MGxUODXwnuGkyyO3vOmrNByRzdQ=",
"dev": true "dev": true
}, },
"yaml-loader": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/yaml-loader/-/yaml-loader-0.5.0.tgz",
"integrity": "sha512-p9QIzcFSNm4mCw/m5NdyMfN4RE4aFZJWRRb01ERVNGCym8VNbKtw3OYZXnvUIkim6U/EjqE/2yIh9F/msShH9A==",
"dev": true,
"requires": {
"js-yaml": "3.10.0"
}
},
"yargs": { "yargs": {
"version": "7.1.0", "version": "7.1.0",
"resolved": "https://registry.npmjs.org/yargs/-/yargs-7.1.0.tgz", "resolved": "https://registry.npmjs.org/yargs/-/yargs-7.1.0.tgz",

View File

@ -13,13 +13,14 @@
"angular": "^1.6.8", "angular": "^1.6.8",
"angular-cookies": "^1.6.4", "angular-cookies": "^1.6.4",
"angular-paging": "^2.2.2", "angular-paging": "^2.2.2",
"angular-translate": "^2.13.1", "angular-translate": "^2.17.0",
"angular-translate-loader-partial": "^2.13.1", "angular-translate-loader-partial": "^2.17.0",
"flatpickr": "^2.6.3", "flatpickr": "^2.6.3",
"fs-extra": "^5.0.0", "fs-extra": "^5.0.0",
"material-design-lite": "^1.3.0", "material-design-lite": "^1.3.0",
"mg-crud": "^1.1.2", "mg-crud": "^1.1.2",
"oclazyload": "^0.6.3", "oclazyload": "^0.6.3",
"require-yaml": "0.0.1",
"validator": "^6.2.1" "validator": "^6.2.1"
}, },
"devDependencies": { "devDependencies": {
@ -53,6 +54,7 @@
"jasmine": "^2.9.0", "jasmine": "^2.9.0",
"jasmine-spec-reporter": "^4.2.1", "jasmine-spec-reporter": "^4.2.1",
"js-yaml": "^3.10.0", "js-yaml": "^3.10.0",
"json-loader": "^0.5.7",
"karma": "^1.7.1", "karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0", "karma-chrome-launcher": "^2.2.0",
"karma-firefox-launcher": "^1.1.0", "karma-firefox-launcher": "^1.1.0",
@ -68,10 +70,11 @@
"nodemon": "^1.12.1", "nodemon": "^1.12.1",
"raw-loader": "*", "raw-loader": "*",
"run-sequence": "^2.2.0", "run-sequence": "^2.2.0",
"sass-loader": "^4.0.2", "sass-loader": "^4.1.1",
"style-loader": "^0.13.1", "style-loader": "^0.13.1",
"webpack": "^2.2.0", "webpack": "^2.2.0",
"webpack-dev-server": "^2.2.0" "webpack-dev-server": "^2.2.0",
"yaml-loader": "^0.5.0"
}, },
"scripts": { "scripts": {
"test": "nodemon -q services_tests.js -w services", "test": "nodemon -q services_tests.js -w services",

View File

@ -1,3 +1,4 @@
require('require-yaml');
var fs = require('fs-extra'); var fs = require('fs-extra');
let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development'; let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
@ -8,8 +9,8 @@ let configEnvFile = `${__dirname}/config/datasources.${env}.json`;
if (fs.existsSync(configEnvFile)) if (fs.existsSync(configEnvFile))
Object.assign(config, require(configEnvFile)); Object.assign(config, require(configEnvFile));
let proxyConf = require(`../../nginx/config.json`); let proxyConf = require(`../../nginx/config.yml`);
let proxyEnvFile = `../../nginx/config.${env}.json`; let proxyEnvFile = `../../nginx/config.${env}.yml`;
if (fs.existsSync(proxyEnvFile)) if (fs.existsSync(proxyEnvFile))
Object.assign(proxyConf, require(proxyEnvFile)); Object.assign(proxyConf, require(proxyEnvFile));

View File

@ -1,4 +1,3 @@
logs/*
static/* static/*
temp/* temp/*
!static/templates !static/templates

View File

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

View File

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

View File

@ -0,0 +1,2 @@
host: salix
port: 80

View File

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

View File

@ -0,0 +1,2 @@
host: test-salix
port: 80

View File

@ -0,0 +1,5 @@
host: localhost
port: 5000
main: salix
defaultPort: 3000
devServerPort: 8081

View File

@ -2,7 +2,7 @@
set nginxDir=%~dp0 set nginxDir=%~dp0
set nginxTemp=%nginxDir%\temp set nginxTemp=%nginxDir%\temp
set nginxConf=%nginxTemp%\nginx.conf set nginxConf=temp\nginx.conf
if "%1"=="" goto caseStart if "%1"=="" goto caseStart
if "%1"=="start" goto caseStart if "%1"=="start" goto caseStart

View File

@ -1,3 +1,4 @@
require('require-yaml');
var fs = require('fs-extra'); var fs = require('fs-extra');
let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development'; let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
@ -8,8 +9,8 @@ let configEnvFile = `${__dirname}/config/datasources.${env}.json`;
if (fs.existsSync(configEnvFile)) if (fs.existsSync(configEnvFile))
Object.assign(config, require(configEnvFile)); Object.assign(config, require(configEnvFile));
let proxyConf = require(`../../nginx/config.json`); let proxyConf = require(`../../nginx/config.yml`);
let proxyEnvFile = `../../nginx/config.${env}.json`; let proxyEnvFile = `../../nginx/config.${env}.yml`;
if (fs.existsSync(proxyEnvFile)) if (fs.existsSync(proxyEnvFile))
Object.assign(proxyConf, require(proxyEnvFile)); Object.assign(proxyConf, require(proxyEnvFile));

View File

@ -25,20 +25,19 @@ var config = {
query: { query: {
presets: ['es2015'] presets: ['es2015']
} }
}, }, {
{ test: /\.yml$/,
loader: 'json-loader!yaml-loader'
}, {
test: /\.html$/, test: /\.html$/,
loader: 'html-loader' loader: 'html-loader'
}, }, {
{
test: /\.css$/, test: /\.css$/,
loader: 'style-loader!css-loader' loader: 'style-loader!css-loader'
}, }, {
{
test: /\.scss$/, test: /\.scss$/,
loader: 'style-loader!css-loader!sass-loader' loader: 'style-loader!css-loader!sass-loader'
}, }, {
{
test: /\.(svg|png|ttf|woff|woff2)$/, test: /\.(svg|png|ttf|woff|woff2)$/,
loader: 'file-loader' loader: 'file-loader'
} }