UNPKG

imq-cli

Version:

Command Line Interface for IMQ

90 lines 2.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /*! * IMQ-CLI library: config * * Copyright (c) 2018, Mykhailo Stadnyk <mikhus@gmail.com> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ const fs_1 = require("fs"); const fs_2 = require("./fs"); const constants_1 = require("./constants"); /** * Loads config from file and returns config object * * @return {IMQCLIConfig} */ function loadConfig() { if (!fs_1.existsSync(constants_1.CONFIG_PATH)) { return {}; } const configText = fs_1.readFileSync(constants_1.CONFIG_PATH, { encoding: 'utf8' }); if (!configText) { return {}; } return JSON.parse(configText); } exports.loadConfig = loadConfig; /** * Saves given config object to config file * * @param {IMQCLIConfig} config */ function saveConfig(config) { const configText = JSON.stringify(config, null, 2) + '\n'; if (!fs_1.existsSync(constants_1.CONFIG_PATH)) { return fs_2.touch(constants_1.CONFIG_PATH, configText); } return fs_1.writeFileSync(constants_1.CONFIG_PATH, configText); } exports.saveConfig = saveConfig; /** * Checks if current config file contains empty config * * @return {boolean} */ function configEmpty() { if (!fs_1.existsSync(constants_1.CONFIG_PATH)) { return true; } const config = loadConfig(); return !(config && Object.keys(config).length); } exports.configEmpty = configEmpty; /** * Prepares config value. * Actually it is used when the set command is called to cast * a given string value to a proper JSON-compatible object * * @param {any} value * @return {any} */ function prepareConfigValue(value) { if (typeof value === 'string') { switch (value) { case 'true': return true; case 'false': return false; case 'null': return null; case 'undefined': return undefined; } // istanbul ignore else // noinspection RegExpSingleCharAlternation,RegExpRedundantEscape if (/^\s*(\[|\{)/.test(value)) { return JSON.parse(value); } } return value; } exports.prepareConfigValue = prepareConfigValue; //# sourceMappingURL=config.js.map