UNPKG

@semo/cli

Version:

A command line tools dispatcher

136 lines 5.78 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.handler = exports.builder = exports.desc = exports.command = exports.plugin = exports.disabled = void 0; const core_1 = require("@semo/core"); const path_1 = __importDefault(require("path")); const fs_extra_1 = __importDefault(require("fs-extra")); const yaml_1 = __importDefault(require("yaml")); exports.disabled = false; // Set to true to disable this command temporarily exports.plugin = 'semo'; exports.command = 'set <configKey> <configValue> [configComment] [configType]'; exports.desc = 'Set config by key'; // export const aliases = '' // export const middleware = (argv) => {} const builder = function (yargs) { yargs.positional('configType', { default: 'string', choices: ['string', 'number', 'int', 'integer', 'boolean', 'bool'], }); // yargs.option('option', { default, describe, alias }) // yargs.commandDir('get') }; exports.builder = builder; const handler = function (argv) { return __awaiter(this, void 0, void 0, function* () { switch (argv.configType) { case 'bool': case 'boolean': argv.configValue = Boolean(argv.configValue); break; case 'int': case 'integer': case 'number': argv.configValue = Number(argv.configValue); } if (core_1.Utils._.isString(argv.configKey)) { argv.configKey = argv.configKey.split('.'); } const scriptName = argv.scriptName || 'semo'; let configPath; if (argv.global) { configPath = process.env.HOME ? path_1.default.resolve(process.env.HOME, '.' + scriptName, '.' + scriptName + 'rc.yml') : ''; } else { configPath = path_1.default.resolve(process.cwd(), '.' + scriptName + 'rc.yml'); } if (!argv.global && !fs_extra_1.default.existsSync(configPath)) { core_1.Utils.error('Config file not found. you need to create config file manually to prove you know what you are doing.'); return; } if (argv.global && !configPath) { core_1.Utils.error('Global config file path not recognized.'); return; } if (argv.global && configPath && !fs_extra_1.default.existsSync(path_1.default.dirname(configPath))) { fs_extra_1.default.ensureDirSync(path_1.default.dirname(configPath)); } let config; if (fs_extra_1.default.existsSync(configPath)) { const rcFile = fs_extra_1.default.readFileSync(configPath, 'utf8'); config = yaml_1.default.parseDocument(rcFile); } else { config = yaml_1.default.parseDocument(''); config.commentBefore = ` THIS IS SEMO(@semo/cli)'s RC FILE. YOU CAN EDIT THIS FILE MANUALLY OR USE semo config COMMAND. RUN semo config help TO SEE RELATED COMMANDS. `; config.contents = config.createNode({}, undefined); } const tmpConfigObject = core_1.Utils._.set({}, argv.configKey, argv.configValue); // Recursively find and change walk(config.contents, tmpConfigObject, config, argv.configComment); fs_extra_1.default.writeFileSync(configPath, config.toString()); console.log(core_1.Utils.color.green(`${configPath} updated!`)); }); }; exports.handler = handler; const walk = (map, configKey, config, comment) => { const currentKey = Object.keys(configKey)[0]; let found = false; if (map && map.items && map.items.length > 0) { for (let pair of map.items) { if (pair.key.value === currentKey) { found = true; if (!core_1.Utils._.isObject(configKey[pair.key.value])) { pair.value = configKey[pair.key.value]; pair.comment = comment; } else if (pair.value.items) { walk(pair.value, configKey[pair.key.value], config, comment); } } } } if (!found) { const pair = config.createPair(currentKey, configKey[currentKey]); walkComment(pair.value, configKey[currentKey], comment); if (map && core_1.Utils._.isArray(map.items)) { map.items.push(pair); } else { config.contents = pair; } } return; }; const walkComment = (map, configKey, comment) => { if (core_1.Utils._.isString(configKey)) { map.comment = comment; } else { const nextKey = Object.keys(configKey)[0]; if (map && map.items && map.items.length > 0) { for (let pair of map.items) { if (pair.key.value === nextKey) { walkComment(pair.value, configKey[pair.key.value], comment); } } } } }; //# sourceMappingURL=set.js.map