1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/config/ConfigLoader.ts

107 lines
3.3 KiB
TypeScript
Raw Normal View History

2016-05-09 23:04:56 +08:00
import * as fs from "fs";
import * as optimist from "optimist";
2016-04-11 04:53:46 +08:00
export class ConfigLoader {
static init(configObject:any, configFilePath?:string, envAlias:Array<Array<string>> = []) {
2016-04-11 04:53:46 +08:00
this.processConfigFile(configFilePath, configObject);
this.processArguments(configObject);
this.processEnvVariables(configObject, envAlias);
2016-04-11 04:53:46 +08:00
}
private static processEnvVariables(configObject:any, envAlias:Array<Array<string>>) {
let varAliases = {};
envAlias.forEach((alias)=> {
if (process.env[alias[0]]) {
varAliases[alias[1]] = process.env[alias[0]];
}
});
this.processHierarchyVar(configObject, varAliases);
2016-04-11 04:53:46 +08:00
this.loadObject(configObject, process.env);
};
private static processArguments(configObject:any) {
let argv = optimist.argv;
delete(argv._);
2016-05-09 23:04:56 +08:00
delete(argv.$0);
this.processHierarchyVar(configObject, argv);
};
private static processHierarchyVar(configObject:any, vars:any) {
2016-04-11 04:53:46 +08:00
let config = {};
Object.keys(vars).forEach((key)=> {
2016-04-11 04:53:46 +08:00
let keyArray = key.split("-");
let value = vars[key];
2016-04-11 04:53:46 +08:00
2016-07-08 04:16:04 +08:00
//recursive settings
2016-05-09 23:04:56 +08:00
let setObject = (object, keyArray, value) => {
2016-04-11 04:53:46 +08:00
let key = keyArray.shift();
object[key] = {};
2016-07-08 04:16:04 +08:00
2016-05-09 23:04:56 +08:00
if (keyArray.length == 0) {
2016-07-08 04:16:04 +08:00
//convert to boolean
if (value.toLowerCase && value.toLowerCase() === "false") {
value = false;
}
if (value.toLowerCase && value.toLowerCase() === "true") {
value = true;
}
2016-04-11 04:53:46 +08:00
object[key] = value;
return;
}
2016-07-08 04:16:04 +08:00
2016-05-09 23:04:56 +08:00
return setObject(object[key], keyArray, value);
2016-04-11 04:53:46 +08:00
};
2016-05-09 23:04:56 +08:00
setObject(config, keyArray, value);
2016-04-11 04:53:46 +08:00
2016-05-09 23:04:56 +08:00
});
2016-04-11 04:53:46 +08:00
this.loadObject(configObject, config);
}
2016-04-11 04:53:46 +08:00
private static processConfigFile(configFilePath:string, configObject:any) {
if (typeof configFilePath !== 'undefined') {
if (ConfigLoader.loadConfigFile(configFilePath, configObject) === false) {
ConfigLoader.saveConfigFile(configFilePath, configObject);
}
}
};
2016-05-09 23:04:56 +08:00
private static loadConfigFile(configFilePath, configObject):boolean {
if (fs.existsSync(configFilePath) === false) {
2016-04-11 04:53:46 +08:00
return false;
}
try {
let config = JSON.parse(fs.readFileSync(configFilePath, 'utf8'));
2016-05-09 23:04:56 +08:00
this.loadObject(configObject, config);
2016-04-11 04:53:46 +08:00
return true;
2016-05-09 23:04:56 +08:00
} catch (err) {
2016-04-11 04:53:46 +08:00
}
return false;
}
2016-05-09 23:04:56 +08:00
private static saveConfigFile(configFilePath, configObject) {
2016-04-11 04:53:46 +08:00
try {
fs.writeFileSync(configFilePath, JSON.stringify(configObject, null, 4));
2016-05-09 23:04:56 +08:00
} catch (err) {
2016-04-11 04:53:46 +08:00
}
}
2016-05-09 23:04:56 +08:00
private static loadObject(targetObject, sourceObject) {
2016-04-11 04:53:46 +08:00
Object.keys(sourceObject).forEach((key)=> {
2016-05-09 23:04:56 +08:00
if (typeof targetObject[key] === "undefined") {
2016-04-11 04:53:46 +08:00
return;
2016-05-09 23:04:56 +08:00
}
if (typeof targetObject[key] === "object") {
this.loadObject(targetObject[key], sourceObject[key]);
} else {
2016-04-11 04:53:46 +08:00
targetObject[key] = sourceObject[key];
}
});
}
}