@imqueue/cli
Version:
Command Line Interface for IMQ
96 lines • 3.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadConfig = loadConfig;
exports.saveConfig = saveConfig;
exports.configEmpty = configEmpty;
exports.prepareConfigValue = prepareConfigValue;
/*!
* IMQ-CLI library: config
*
* I'm Queue Software Project
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* If you want to use this code in a closed source (commercial) project, you can
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
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 (!(0, fs_1.existsSync)(constants_1.CONFIG_PATH)) {
return {};
}
const configText = (0, fs_1.readFileSync)(constants_1.CONFIG_PATH, { encoding: 'utf8' });
if (!configText) {
return {};
}
return JSON.parse(configText);
}
/**
* Saves given config object to config file
*
* @param {IMQCLIConfig} config
*/
function saveConfig(config) {
const configText = JSON.stringify(config, null, 2) + '\n';
if (!(0, fs_1.existsSync)(constants_1.CONFIG_PATH)) {
return (0, fs_2.touch)(constants_1.CONFIG_PATH, configText);
}
return (0, fs_1.writeFileSync)(constants_1.CONFIG_PATH, configText);
}
/**
* Checks if current config file contains empty config
*
* @return {boolean}
*/
function configEmpty() {
if (!(0, fs_1.existsSync)(constants_1.CONFIG_PATH)) {
return true;
}
const config = loadConfig();
return !(config && Object.keys(config).length);
}
/**
* 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;
}
//# sourceMappingURL=config.js.map