UNPKG

@semo/cli

Version:

A command line tools dispatcher

77 lines 3.48 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.aliases = 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 = 'delete <configKey>'; exports.desc = 'Delete configs by key'; exports.aliases = 'del'; // export const middleware = (argv) => {} const builder = function (yargs) { // yargs.option('option', { default, describe, alias }) // yargs.commandDir('delete') }; exports.builder = builder; const handler = function (argv) { return __awaiter(this, void 0, void 0, function* () { 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 (!configPath || !fs_extra_1.default.existsSync(configPath)) { core_1.Utils.error('Config file not found.'); return; } const rcFile = fs_extra_1.default.readFileSync(configPath, 'utf8'); const config = yaml_1.default.parseDocument(rcFile); const tmpConfigObject = core_1.Utils._.set({}, argv.configKey, 1); // Recursively find and change walk(config.contents, tmpConfigObject); fs_extra_1.default.writeFileSync(configPath, config.toString()); console.log(core_1.Utils.color.green(`${configPath} updated!`)); }); }; exports.handler = handler; const walk = (map, configKey) => { const currentKey = Object.keys(configKey)[0]; if (map.items) { for (let pairKey in map.items) { const pair = map.items[pairKey]; if (pair.key.value === currentKey) { if (!core_1.Utils._.isObject(configKey[pair.key.value])) { map.items.splice(pairKey, 1); } else if (pair.value.items) { walk(pair.value, configKey[pair.key.value]); } } } } return; }; //# sourceMappingURL=delete.js.map