UNPKG

fedapay-cli

Version:
66 lines (65 loc) 1.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const fs = tslib_1.__importStar(require("fs")); const path = tslib_1.__importStar(require("path")); class UserConfig { /** * * @param {string} dir The config directory */ constructor(dir) { this.defaultConfig = { mq_url: 'amqp://cli:cli@es.fedapay.com:5672' }; !fs.existsSync(dir) && fs.mkdirSync(dir); this.configPath = path.join(dir, 'config.json'); } /** * * @param {any} config The content to write in file */ write(config) { const oldConfig = this.readAll(); const newConfig = Object.assign(Object.assign({}, oldConfig), config); fs.writeFileSync(this.configPath, JSON.stringify(newConfig, null, 4)); } /** * * @param {any} config The content to write in file */ writeAll(config) { fs.writeFileSync(this.configPath, JSON.stringify(config, null, 4)); } /** * clear method to overwrite the file */ clear() { fs.writeFileSync(this.configPath, ('')); } /** *The read method * @param {string} key The specified key that you want to get * @param {any} firstValue The default value * @return {any} Config value */ read(key, firstValue = null) { const config = this.readAll(); const configValue = Object.prototype.hasOwnProperty.call(config, key) ? config[key] : null; return firstValue || configValue; } /** * ReadAll method * @return {any} */ readAll() { try { const config = JSON.parse(fs.readFileSync(this.configPath, 'utf-8')); return Object.assign(Object.assign({}, this.defaultConfig), config); } catch (error) { return {}; } } } exports.default = UserConfig;