mirror of
https://github.com/xuthus83/pigallery2.git
synced 2024-11-03 21:04:03 +08:00
adding config loader
This commit is contained in:
parent
8ad13e3e78
commit
ca77700e2e
@ -1,6 +1,16 @@
|
||||
|
||||
export class Config{
|
||||
public static thumbnailSizes = [200];
|
||||
public static imagesFolder = "/demo/images";
|
||||
public static thumbnailFolder = "/demo/TEMP";
|
||||
}
|
||||
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();
|
88
backend/config/ConfigLoader.ts
Normal file
88
backend/config/ConfigLoader.ts
Normal file
@ -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];
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -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: [
|
||||
|
@ -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",
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user