will-util
Version:
Utility functional
59 lines (58 loc) • 1.91 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Configure = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
class Configure {
static initConfig() {
if (!Configure.configurations) {
Configure.configurations = Configure.readConfig();
}
}
static readConfig(filepath = path_1.default.join(process.cwd(), "config")) {
try {
let filename = path_1.default.resolve(filepath, 'default.json');
let filedata = fs_1.default.readFileSync(filename, 'utf-8');
return JSON.parse(filedata);
}
catch (ex) {
console.error(ex);
}
return {};
}
static getConfig(key) {
Configure.initConfig();
return Configure.configurations[key];
}
static hasConfig(key) {
Configure.initConfig();
let value = Configure.configurations ? Configure.configurations[key] : null;
if (value === undefined || value === null)
return false;
return true;
}
static reloadConfig() {
Configure.configurations = Configure.readConfig();
}
static getEnv(key, defaultValue) {
let result = process.env[key];
if (result === undefined || result === null) {
result = this.getConfig(key);
}
return result || defaultValue;
}
get(key) {
return Configure.getConfig(key);
}
has(key) {
return Configure.hasConfig(key);
}
env(key, defaultValue) {
return Configure.getEnv(key, defaultValue);
}
}
exports.Configure = Configure;
exports.default = new Configure();
;