1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-14 11:23:17 +02:00
pigallery2/backend/config/ConfigLoader.ts

97 lines
3.0 KiB
TypeScript
Raw Normal View History

2016-05-09 17:04:56 +02:00
import * as fs from "fs";
import * as optimist from "optimist";
2016-04-10 22:53:46 +02:00
export class ConfigLoader {
static init(configObject:any, configFilePath?:string, envAlias:Array<Array<string>> = []) {
2016-04-10 22:53:46 +02:00
this.processConfigFile(configFilePath, configObject);
this.processArguments(configObject);
this.processEnvVariables(configObject, envAlias);
2016-04-10 22:53:46 +02: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-10 22:53:46 +02:00
this.loadObject(configObject, process.env);
};
private static processArguments(configObject:any) {
let argv = optimist.argv;
delete(argv._);
2016-05-09 17:04:56 +02:00
delete(argv.$0);
this.processHierarchyVar(configObject, argv);
};
private static processHierarchyVar(configObject:any, vars:any) {
2016-04-10 22:53:46 +02:00
let config = {};
Object.keys(vars).forEach((key)=> {
2016-04-10 22:53:46 +02:00
let keyArray = key.split("-");
let value = vars[key];
2016-04-10 22:53:46 +02:00
2016-05-09 17:04:56 +02:00
let setObject = (object, keyArray, value) => {
2016-04-10 22:53:46 +02:00
let key = keyArray.shift();
object[key] = {};
2016-05-09 17:04:56 +02:00
if (keyArray.length == 0) {
2016-04-10 22:53:46 +02:00
object[key] = value;
return;
}
2016-05-09 17:04:56 +02:00
return setObject(object[key], keyArray, value);
2016-04-10 22:53:46 +02:00
};
2016-05-09 17:04:56 +02:00
setObject(config, keyArray, value);
2016-04-10 22:53:46 +02:00
2016-05-09 17:04:56 +02:00
});
2016-04-10 22:53:46 +02:00
this.loadObject(configObject, config);
}
2016-04-10 22:53:46 +02: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 17:04:56 +02:00
private static loadConfigFile(configFilePath, configObject):boolean {
if (fs.existsSync(configFilePath) === false) {
2016-04-10 22:53:46 +02:00
return false;
}
try {
let config = JSON.parse(fs.readFileSync(configFilePath, 'utf8'));
2016-05-09 17:04:56 +02:00
this.loadObject(configObject, config);
2016-04-10 22:53:46 +02:00
return true;
2016-05-09 17:04:56 +02:00
} catch (err) {
2016-04-10 22:53:46 +02:00
}
return false;
}
2016-05-09 17:04:56 +02:00
private static saveConfigFile(configFilePath, configObject) {
2016-04-10 22:53:46 +02:00
try {
fs.writeFileSync(configFilePath, JSON.stringify(configObject, null, 4));
2016-05-09 17:04:56 +02:00
} catch (err) {
2016-04-10 22:53:46 +02:00
}
}
2016-05-09 17:04:56 +02:00
private static loadObject(targetObject, sourceObject) {
2016-04-10 22:53:46 +02:00
Object.keys(sourceObject).forEach((key)=> {
2016-05-09 17:04:56 +02:00
if (typeof targetObject[key] === "undefined") {
2016-04-10 22:53:46 +02:00
return;
2016-05-09 17:04:56 +02:00
}
if (typeof targetObject[key] === "object") {
this.loadObject(targetObject[key], sourceObject[key]);
} else {
2016-04-10 22:53:46 +02:00
targetObject[key] = sourceObject[key];
}
});
}
}