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 {
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
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);
|
2016-05-11 03:33:58 +08:00
|
|
|
this.processEnvVariables(configObject, envAlias);
|
2016-04-11 04:53:46 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
private static processEnvVariables(configObject: any, envAlias: Array<Array<string>>) {
|
2016-05-11 03:33:58 +08:00
|
|
|
let varAliases = {};
|
2016-12-27 06:36:38 +08:00
|
|
|
envAlias.forEach((alias) => {
|
2016-05-11 03:33:58 +08:00
|
|
|
if (process.env[alias[0]]) {
|
|
|
|
varAliases[alias[1]] = process.env[alias[0]];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.processHierarchyVar(configObject, varAliases);
|
2017-01-25 03:37:16 +08:00
|
|
|
this.processHierarchyVar(configObject, process.env);
|
2016-04-11 04:53:46 +08:00
|
|
|
};
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
private static processArguments(configObject: any) {
|
2016-04-11 04:53:46 +08:00
|
|
|
let argv = optimist.argv;
|
|
|
|
delete(argv._);
|
2016-05-09 23:04:56 +08:00
|
|
|
delete(argv.$0);
|
2016-05-11 03:33:58 +08:00
|
|
|
this.processHierarchyVar(configObject, argv);
|
|
|
|
};
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
|
|
|
|
private static processHierarchyVar(configObject: any, vars: any) {
|
2016-04-11 04:53:46 +08:00
|
|
|
let config = {};
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
Object.keys(vars).forEach((key) => {
|
2016-04-11 04:53:46 +08:00
|
|
|
let keyArray = key.split("-");
|
2016-05-11 03:33:58 +08:00
|
|
|
let value = vars[key];
|
2016-04-11 04:53:46 +08:00
|
|
|
|
2016-07-08 04:16:04 +08:00
|
|
|
//recursive settings
|
2016-12-27 06:36:38 +08:00
|
|
|
let setObject = (object: any, keyArray: Array<string>, value: any): void => {
|
2016-04-11 04:53:46 +08:00
|
|
|
let key = keyArray.shift();
|
2016-12-27 06:36:38 +08:00
|
|
|
object[key] = 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-12-27 06:36:38 +08:00
|
|
|
|
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-12-27 06:36:38 +08:00
|
|
|
|
2016-04-11 04:53:46 +08:00
|
|
|
this.loadObject(configObject, config);
|
2016-05-11 03:33:58 +08:00
|
|
|
}
|
2016-04-11 04:53:46 +08:00
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
private static processConfigFile(configFilePath: string, configObject: any) {
|
2016-04-11 04:53:46 +08:00
|
|
|
if (typeof configFilePath !== 'undefined') {
|
|
|
|
if (ConfigLoader.loadConfigFile(configFilePath, configObject) === false) {
|
|
|
|
ConfigLoader.saveConfigFile(configFilePath, configObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-27 06:36:38 +08:00
|
|
|
private static loadConfigFile(configFilePath: string, configObject: any): boolean {
|
2016-05-09 23:04:56 +08:00
|
|
|
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-12-27 06:36:38 +08:00
|
|
|
private static saveConfigFile(configFilePath: string, configObject: any) {
|
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
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 03:37:16 +08:00
|
|
|
|
|
|
|
private static loadObject(targetObject, sourceObject) {
|
2016-12-27 06:36:38 +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
|
|
|
}
|
2017-01-25 03:37:16 +08:00
|
|
|
|
|
|
|
if (Array.isArray(targetObject[key])) {
|
|
|
|
return targetObject[key] = sourceObject[key];
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
if (typeof targetObject[key] === "object") {
|
2017-01-25 03:37:16 +08:00
|
|
|
return this.loadObject(targetObject[key], sourceObject[key]);
|
2016-04-11 04:53:46 +08:00
|
|
|
}
|
2017-01-25 03:37:16 +08:00
|
|
|
|
|
|
|
targetObject[key] = sourceObject[key];
|
2016-04-11 04:53:46 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|