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 splitingRegister from './splitingRegister';
factory.$inject = ['$translatePartialLoader', '$http', '$window', '$ocLazyLoad', '$q', '$translate'];
export function factory($translatePartialLoader, $http, $window, $ocLazyLoad, $q, $translate) {
factory.$inject = ['$http', '$window', '$ocLazyLoad', '$translatePartialLoader', '$translate'];
export function factory($http, $window, $ocLazyLoad, $translatePartialLoader, $translate) {
class ModuleLoader {
constructor() {
this._loadedModules = {};
this._loaded = {};
}
load(moduleName, validations) {
if (this._loadedModules[moduleName])
return;
let loaded = this._loaded;
this._loadedModules[moduleName] = true;
if (loaded[moduleName])
return loaded[moduleName];
loaded[moduleName] = Promise.resolve(true);
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 = [];
for (let dep of deps) {
this._loadedModules[dep] = true;
promises.push(modules[dep]());
// FIXME: https://github.com/angular-translate/angular-translate/pull/1674
$translatePartialLoader.addPart(moduleName);
promises.push($translate.refresh());
if (validations)
promises.push(new Promise(resolve => {
$http.get(`/${dep}/validations`).then(
$http.get(`/${moduleName}/validations`).then(
json => this.onValidationsReady(json, resolve),
json => resolve()
);
}));
$translatePartialLoader.addPart(dep);
// FIXME: https://github.com/angular-translate/angular-translate/pull/1674
// promises.push($translate.refresh());
setTimeout(() => $translate.refresh(), 500);
}
promises.push(new Promise(resolve => {
splitingRegister.modules[moduleName](resolve);
}));
let ocDeps = deps.map(item => {
return {name: item};
Promise.all(promises)
.then(() => {
this._loaded[moduleName] = true;
resolve($ocLazyLoad.load({name: moduleName}));
})
.catch(reject);
})
.catch(reject);
});
return new Promise(resolve => {
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;
}
return loaded[moduleName];
}
onValidationsReady(json, resolve) {
let entities = json.data;
@ -69,6 +69,17 @@ export function factory($translatePartialLoader, $http, $window, $ocLazyLoad, $q
Object.assign($window.validations, json.data);
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();

View File

@ -1,29 +1,11 @@
class SplitingRegister {
constructor() {
this._graph = null;
this._modules = {};
this.graph = null;
this.modules = {};
}
get modules() {
return this._modules;
}
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;
getDependencies(moduleName) {
return this.graph[moduleName];
}
}

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 './spliting';
import './configroutes';
import './config';
import './run';

View File

@ -1,6 +1,6 @@
import './spliting';
import deps from 'client/modules.json';
import ngModule from './module';
import deps from 'client/modules.yml';
import modules from 'client/spliting';
import {splitingRegister} from 'core';
function loader(moduleName, validations) {
@ -13,7 +13,9 @@ function loader(moduleName, validations) {
config.$inject = ['$stateProvider', '$urlRouterProvider', 'aclServiceProvider', 'modulesFactoryProvider'];
function config($stateProvider, $urlRouterProvider, aclServiceProvider, modulesFactory) {
splitingRegister.registerGraph(deps);
splitingRegister.graph = deps;
splitingRegister.modules = modules;
let aclService = aclServiceProvider.$get();
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 gutil = require('gulp-util');
const wrap = require('gulp-wrap');
@ -22,11 +23,11 @@ const servicesDir = './services';
const nginxDir = `${servicesDir}/nginx`;
const buildDir = `${nginxDir}/static`;
const modules = require('./client/modules.json');
const modules = require('./client/modules.yml');
const webpackConfig = require('./webpack.config.js');
let proxyConf = require(`${nginxDir}/config.json`);
let proxyEnvFile = `${nginxDir}/config.${env}.json`;
let proxyConf = require(`${nginxDir}/config.yml`);
let proxyEnvFile = `${nginxDir}/config.${env}.yml`;
if (fs.existsSync(proxyEnvFile))
Object.assign(proxyConf, require(proxyEnvFile));
@ -80,7 +81,7 @@ gulp.task('e2e-run', () => {
gulp.task('clean', () => {
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', () => {
@ -144,7 +145,7 @@ gulp.task('nginx', ['nginx-conf'], callback => {
let command = isWindows ? 'start.cmd' : 'start.sh';
command = path.join(`${nginxDir}/${command}`);
exec(command, (err, stdout, stderr) => {
if (stderr) console.log(stderr);
// if (stderr) console.log(stderr);
callback(err);
});
});
@ -172,6 +173,11 @@ gulp.task('nginx-conf', async () => {
await fs.writeFile(`${nginxDir}/temp/nginx.conf`, nginxConf);
});
gulp.task('nginx-clean', () => {
const del = require('del');
return del([`${nginxDir}/temp/*`], {force: true});
});
let services;
async function getServices() {

67
package-lock.json generated
View File

@ -132,19 +132,19 @@
"integrity": "sha1-cC9XTW0UBpADXqxkOV/jEfeYf7s="
},
"angular-translate": {
"version": "2.13.1",
"resolved": "https://registry.npmjs.org/angular-translate/-/angular-translate-2.13.1.tgz",
"integrity": "sha1-BWc+s3GYb8Ee3BUfWQKZ1+DEYig=",
"version": "2.17.0",
"resolved": "https://registry.npmjs.org/angular-translate/-/angular-translate-2.17.0.tgz",
"integrity": "sha512-SudfI0R0Hhtvngc0X3wFChXQGmw90o95i+QPZ11LhJJryneTq8LR3+3E4E7jgHA4fu6TcswgcfZ9+cp5ckbUHw==",
"requires": {
"angular": "1.6.8"
}
},
"angular-translate-loader-partial": {
"version": "2.13.1",
"resolved": "https://registry.npmjs.org/angular-translate-loader-partial/-/angular-translate-loader-partial-2.13.1.tgz",
"integrity": "sha1-U3g7SK7OUJFS1NwfZMIxaKaTadM=",
"version": "2.17.0",
"resolved": "https://registry.npmjs.org/angular-translate-loader-partial/-/angular-translate-loader-partial-2.17.0.tgz",
"integrity": "sha512-pyRJcRc93iwiUnRnh9ZfehbQE/yxO5T6jmEqIvLEVz8gKLjDqDLKcaQFgPef9wCIN2n3e531YbStkkbSH3LYmQ==",
"requires": {
"angular-translate": "2.13.1"
"angular-translate": "2.17.0"
}
},
"ansi-align": {
@ -267,7 +267,6 @@
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.9.tgz",
"integrity": "sha1-c9g7wmP4bpf4zE9rrhsOkKfSLIY=",
"dev": true,
"requires": {
"sprintf-js": "1.0.3"
}
@ -5329,7 +5328,7 @@
"fsevents": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.1.3.tgz",
"integrity": "sha1-EfgjGPX+e7LNIpZaEI6TBiCCFtg=",
"integrity": "sha512-WIr7iDkdmdbxu/Gh6eKEZJL6KPE74/5MEsf2whTOFNxbIoIixogroLdKYqB6FDav4Wavh/lZdzzd3b2KxIXC5Q==",
"dev": true,
"optional": true,
"requires": {
@ -9980,9 +9979,9 @@
"dev": true
},
"json-loader": {
"version": "0.5.4",
"resolved": "https://registry.npmjs.org/json-loader/-/json-loader-0.5.4.tgz",
"integrity": "sha1-i6oTZaYy9Yo8RtIBdfxgAsluN94=",
"version": "0.5.7",
"resolved": "https://registry.npmjs.org/json-loader/-/json-loader-0.5.7.tgz",
"integrity": "sha512-QLPs8Dj7lnf3e3QYS1zkCo+4ZwqOiF9d/nZnYozTISxXWCfNs9yuky5rJw4/W34s7POaNlbZmQGaB5NiXCbP4w==",
"dev": true
},
"json-schema": {
@ -12665,6 +12664,30 @@
"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": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
@ -12820,9 +12843,9 @@
}
},
"sass-loader": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/sass-loader/-/sass-loader-4.0.2.tgz",
"integrity": "sha1-phbrdwNmVD5k9UfIYw85xNp18V0=",
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/sass-loader/-/sass-loader-4.1.1.tgz",
"integrity": "sha1-ee+UaM8L9kbClSnh8sumvW5Rx7w=",
"dev": true,
"requires": {
"async": "2.1.2",
@ -13535,8 +13558,7 @@
"sprintf-js": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=",
"dev": true
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw="
},
"sqlstring": {
"version": "2.3.0",
@ -14879,7 +14901,7 @@
"async": "2.1.2",
"enhanced-resolve": "3.0.3",
"interpret": "1.0.1",
"json-loader": "0.5.4",
"json-loader": "0.5.7",
"loader-runner": "2.3.0",
"loader-utils": "0.2.16",
"memory-fs": "0.4.1",
@ -15264,6 +15286,15 @@
"integrity": "sha1-MGxUODXwnuGkyyO3vOmrNByRzdQ=",
"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": {
"version": "7.1.0",
"resolved": "https://registry.npmjs.org/yargs/-/yargs-7.1.0.tgz",

View File

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

View File

@ -1,3 +1,4 @@
require('require-yaml');
var fs = require('fs-extra');
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))
Object.assign(config, require(configEnvFile));
let proxyConf = require(`../../nginx/config.json`);
let proxyEnvFile = `../../nginx/config.${env}.json`;
let proxyConf = require(`../../nginx/config.yml`);
let proxyEnvFile = `../../nginx/config.${env}.yml`;
if (fs.existsSync(proxyEnvFile))
Object.assign(proxyConf, require(proxyEnvFile));

View File

@ -1,4 +1,3 @@
logs/*
static/*
temp/*
!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 nginxTemp=%nginxDir%\temp
set nginxConf=%nginxTemp%\nginx.conf
set nginxConf=temp\nginx.conf
if "%1"=="" goto caseStart
if "%1"=="start" goto caseStart

View File

@ -1,3 +1,4 @@
require('require-yaml');
var fs = require('fs-extra');
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))
Object.assign(config, require(configEnvFile));
let proxyConf = require(`../../nginx/config.json`);
let proxyEnvFile = `../../nginx/config.${env}.json`;
let proxyConf = require(`../../nginx/config.yml`);
let proxyEnvFile = `../../nginx/config.${env}.yml`;
if (fs.existsSync(proxyEnvFile))
Object.assign(proxyConf, require(proxyEnvFile));

View File

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