UNPKG

bailiff

Version:
64 lines (63 loc) 2.34 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const _ = __importStar(require("lodash")); const fs = __importStar(require("fs")); const path_1 = __importDefault(require("path")); const app_root_path_1 = __importDefault(require("app-root-path")); const errors_1 = __importDefault(require("../errors")); class CustomStore { static add(configs, dirName) { if (typeof configs == "string") { configs = this.__readConfigsFromJsonFile(String(configs), dirName); } if (typeof configs == "object") _.extend(this.__configStore, configs); } static get(name) { return this.__configStore[name]; } static __readConfigsFromJsonFile(relativePath, dirName) { const absolutePath = this.__absoluteValidPath(relativePath, dirName); if (!absolutePath) return {}; return this.__readFromJsonFile(absolutePath); } static __absoluteValidPath(relativePath, dirName) { let absolutePath = path_1.default.join(app_root_path_1.default.toString(), relativePath); if (fs.existsSync(absolutePath)) return absolutePath; absolutePath = path_1.default.join(dirName, relativePath); if (fs.existsSync(absolutePath)) return absolutePath; console.error(errors_1.default["002"]); return false; } static __readFromJsonFile(filePath) { let jsonData = {}; try { const parsedJson = JSON.parse(fs.readFileSync(filePath, 'utf-8')); if (Array.isArray(parsedJson)) { console.error(errors_1.default["003"]); } else { jsonData = parsedJson; } } catch (e) { console.error(errors_1.default["004"]); } return jsonData; } } exports.default = CustomStore; CustomStore.__configStore = {};