From ca77700e2e1d808848612cd86a5671cec67a209c Mon Sep 17 00:00:00 2001 From: Braun Patrik Date: Sun, 10 Apr 2016 22:53:46 +0200 Subject: [PATCH] adding config loader --- backend/config/Config.ts | 20 ++++++-- backend/config/ConfigLoader.ts | 88 ++++++++++++++++++++++++++++++++++ frontend/webpack.config.js | 3 -- package.json | 1 + typings.json | 1 + 5 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 backend/config/ConfigLoader.ts diff --git a/backend/config/Config.ts b/backend/config/Config.ts index e97050c4..f66a9222 100644 --- a/backend/config/Config.ts +++ b/backend/config/Config.ts @@ -1,6 +1,16 @@ -export class Config{ - public static thumbnailSizes = [200]; - public static imagesFolder = "/demo/images"; - public static thumbnailFolder = "/demo/TEMP"; -} \ No newline at end of file +import {ConfigLoader} from "./ConfigLoader"; + +export class ConfigClass{ + + constructor(){ + ConfigLoader.init(this,__dirname+'./../../../config.json'); + } + + public thumbnailSizes = [200]; + public imagesFolder = "/demo/images"; + public thumbnailFolder = "/demo/TEMP"; +} + + +export var Config = new ConfigClass(); \ No newline at end of file diff --git a/backend/config/ConfigLoader.ts b/backend/config/ConfigLoader.ts new file mode 100644 index 00000000..ec81a92c --- /dev/null +++ b/backend/config/ConfigLoader.ts @@ -0,0 +1,88 @@ +import * as fs from 'fs'; +import * as optimist from 'optimist'; + +export class ConfigLoader { + + + static init(configObject:any, configFilePath?:string){ + this.processConfigFile(configFilePath, configObject); + this.processArguments(configObject); + this.processEnvVariables(configObject); + + } + + private static processEnvVariables(configObject:any) { + this.loadObject(configObject, process.env); + }; + + private static processArguments(configObject:any) { + let argv = optimist.argv; + delete(argv._); + delete(argv.$0); + let config = {}; + + Object.keys(argv).forEach((key)=> { + let keyArray = key.split("-"); + let value = argv[key]; + + let setObject = (object,keyArray,value) => { + let key = keyArray.shift(); + object[key] = {}; + if(keyArray.length == 0){ + object[key] = value; + return; + } + return setObject(object[key],keyArray,value); + }; + setObject(config,keyArray,value); + + }); + this.loadObject(configObject, config); + }; + + private static processConfigFile(configFilePath:string, configObject:any) { + if (typeof configFilePath !== 'undefined') { + if (ConfigLoader.loadConfigFile(configFilePath, configObject) === false) { + ConfigLoader.saveConfigFile(configFilePath, configObject); + } + } + }; + + private static loadConfigFile(configFilePath,configObject):boolean{ + if(fs.existsSync(configFilePath) === false){ + return false; + } + try { + let config = JSON.parse(fs.readFileSync(configFilePath, 'utf8')); + + this.loadObject(configObject,config); + return true; + }catch(err){ + + } + return false; + } + + + + private static saveConfigFile(configFilePath,configObject){ + try { + fs.writeFileSync(configFilePath, JSON.stringify(configObject, null, 4)); + }catch(err){ + + } + } + + private static loadObject(targetObject,sourceObject){ + Object.keys(sourceObject).forEach((key)=> { + if(typeof targetObject[key] === "undefined"){ + return; + } + if(typeof targetObject[key] === "object"){ + this.loadObject(targetObject[key],sourceObject[key] ); + }else { + targetObject[key] = sourceObject[key]; + } + }); + } +} \ No newline at end of file diff --git a/frontend/webpack.config.js b/frontend/webpack.config.js index 4854346e..f6ed6255 100644 --- a/frontend/webpack.config.js +++ b/frontend/webpack.config.js @@ -20,9 +20,6 @@ module.exports = { }, // Add minification plugins: [ - // new CommonsChunkPlugin("dist/admin-commons-bundle.js",['demo','missionControl']), - // new CommonsChunkPlugin("dist/commons-bundle.js") - // ,new webpack.optimize.UglifyJsPlugin() ], module: { loaders: [ diff --git a/package.json b/package.json index 0df98e13..d4d90f7c 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "mime": "^1.3.4", "morgan": "^1.7.0", "ng2-cookies": "^0.1.5", + "optimist": "^0.6.1", "rxjs": "5.0.0-beta.2", "style-loader": "^0.13.1", "ts-loader": "^0.8.1", diff --git a/typings.json b/typings.json index 32f9ab7a..7b7f3870 100644 --- a/typings.json +++ b/typings.json @@ -10,6 +10,7 @@ "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#d22516f9f089de107d7e7d5938566377370631f6", "mime": "github:DefinitelyTyped/DefinitelyTyped/mime/mime.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e", "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e", + "optimist": "registry:dt/optimist#0.0.0+20160316171810", "serve-static": "github:DefinitelyTyped/DefinitelyTyped/serve-static/serve-static.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e" } }