import path from "path"; import dotenv, {DotenvConfigOutput} from "dotenv"; import StringUtil from '../utils/StringUtil'; import * as process from "process"; import {__approot, __businesspath, __ENV, __resourcepath} from "./ApplicationEnvironment"; export default class ApplicationConfig { private configration:DotenvConfigOutput = {}; private env:any = {}; private defaultBusinessServicePath:string = __businesspath||"../business/service"; public static defaultConfig:ApplicationConfig; constructor(filePath:string) { let configPath = `${__resourcepath}/${__ENV}.env`; if (filePath != undefined && filePath !== null && filePath !== "") { configPath = filePath; } filePath = path.normalize(String(configPath)); const conf = dotenv.config({ path: filePath, }); if (conf.error) { throw conf.error; } this.configration = conf; this.env = process.env; } public get(key:string, defaultValue?:string):string{ let val: string = StringUtil.null2str(process.env, key); if(val === "" && defaultValue !== undefined){ val = defaultValue; } return val; } public getInt(key:string, defaultValue?:number):number{ let val: number = StringUtil.null2int(process.env, key, defaultValue); return val; } public getBusinessPath():string{ let val: string = StringUtil.null2str(process.env, "BUSINESS.SERVICE.PATH"); if(val === ""){ val = this.defaultBusinessServicePath; } return val; } public getArray(key:string):Array{ let values:string[] = this.get(key).split(","); return values; } public static load(path:any):ApplicationConfig{ const config = new ApplicationConfig(path); return config; } public static loadDefault():ApplicationConfig{ if(this.defaultConfig){ return this.defaultConfig; } this.defaultConfig = new this(""); return this.defaultConfig; } }