reg-suit-core
Version:
See https://github.com/Quramy/reg-suit .
123 lines • 4.15 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigManager = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const reg_suit_util_1 = require("reg-suit-util");
const DEFAULT_CONFIG_FILE_NAME = "regconfig.json";
const ESCAPE_REGEXP = /\$(\$[^\$]+)/g;
const PLACEHOLDER_REGEXP = /\$([^\$]+)/g;
const PLACEHOLDER_REGEXP_BRACE = /\$\{([^\$\{\}]+)\}/g;
function expandPlaceholders(x) {
if (typeof x === "object") {
Object.keys(x).forEach(k => {
x[k] = expandPlaceholders(x[k]);
});
return x;
}
else if (Array.isArray(x)) {
return x.map(() => expandPlaceholders(x));
}
else if (typeof x === "string") {
if (ESCAPE_REGEXP.test(x)) {
return x.replace(ESCAPE_REGEXP, (_, g1) => g1);
}
else if (PLACEHOLDER_REGEXP_BRACE.test(x)) {
return x.replace(PLACEHOLDER_REGEXP_BRACE, (_, g1) => process.env[g1]);
}
else if (PLACEHOLDER_REGEXP.test(x)) {
return x.replace(PLACEHOLDER_REGEXP, (_, g1) => process.env[g1]);
}
else {
return x;
}
}
else {
return x;
}
}
class ConfigManager {
constructor(opt) {
this._configFileName = opt.configFileName;
this._logger = opt.logger;
}
get defaultConfigFileName() {
return DEFAULT_CONFIG_FILE_NAME;
}
replaceEnvValue() {
const rawConfig = this.config;
if (!!rawConfig["__replaced__"])
return rawConfig;
expandPlaceholders(rawConfig);
rawConfig["__replaced__"] = true;
return rawConfig;
}
get config() {
if (!this._loadedConfig) {
this._loadedConfig = this.readConfig().config;
}
return this._loadedConfig;
}
readConfig() {
const defaultCoreConfig = {
workingDir: ".reg",
actualDir: "directory_contains_actual_images",
thresholdRate: 0,
};
let readResult, readJsonObj;
const configFilePath = this._getConfigPath();
try {
readResult = fs_1.default.readFileSync(configFilePath, "utf8");
}
catch (e) {
this._logger.verbose(`Failed to load config file: ${this._logger.colors.magenta(configFilePath)}`);
}
if (readResult) {
try {
readJsonObj = JSON.parse(readResult);
}
catch (e) {
const msg = `Failed to read ${this._configFileName} because it's invalid JSON file.`;
this._logger.error(msg);
this._logger.error(readResult);
throw new Error(msg);
}
}
if (readJsonObj) {
return {
readUserConfig: true,
config: {
core: Object.assign(Object.assign({}, defaultCoreConfig), readJsonObj.core),
plugins: readJsonObj.plugins,
},
};
}
else {
return {
readUserConfig: false,
config: {
core: defaultCoreConfig,
},
};
}
}
writeConfig(config) {
this._loadedConfig = config;
fs_1.default.writeFileSync(this._getConfigPath(), JSON.stringify(config, null, 2), "utf8");
}
_getConfigPath() {
if (this._configFileName) {
this._logger.verbose(`config file: ${this._configFileName}`);
return path_1.default.resolve(reg_suit_util_1.fsUtil.prjRootDir(), this._configFileName);
}
else {
this._logger.verbose(`config file not specified, load from ${DEFAULT_CONFIG_FILE_NAME}.`);
return path_1.default.resolve(reg_suit_util_1.fsUtil.prjRootDir(), DEFAULT_CONFIG_FILE_NAME);
}
}
}
exports.ConfigManager = ConfigManager;
//# sourceMappingURL=config-manager.js.map
;