UNPKG

alia

Version:
153 lines (152 loc) 4.59 kB
import path from 'path'; import logger from '../utils/logger.js'; import { Flag } from './flag.js'; import { toBool } from '../utils/to-bool.js'; export class SetFlag extends Flag { #options = {}; #command; #key; flag = { key: 'set', short: 's', desc: 'set an alias', run: (_) => this.#setAlias() }; mods = [ { key: 'shell', short: 'sh', desc: 'enable shell mode (default: true)', run: (args) => this.#setShell(args) }, { key: 'env', short: 'e', desc: 'add environment variables', run: (args) => this.#setEnv(args) }, { key: 'env-file', short: 'ef', desc: 'add environment variables file', run: (args) => this.#setEnvFile(args) }, { key: 'quote', short: 'q', desc: 'add quotes to command (default: true)', run: (args) => this.#setQuote(args) }, { key: 'work-dir', short: 'wd', desc: 'run command in a specific directory', run: (args) => this.#setWorkDir(args) }, { key: 'key', short: 'k', desc: 'the alias key to set', required: true, run: (args) => this.#setKey(args) }, { key: 'command', short: 'c', desc: 'the command to set as alias (group multiple args with quotes)', required: true, run: (args) => this.#setCommand(args) } ]; #setKey(data) { if (!data[0]) { logger.info(`no value provided for key`); return false; } this.#key = data[0]; return true; } #setCommand(data) { if (data.length === 0) { logger.info(`no value provided for command`); return false; } this.#command = data[0].split(' '); return true; } #setQuote(data) { const quote = toBool(data); if (quote === undefined) { logger.info(`invalid value for quote: ${data[0]}`); return false; } this.#options.quote = quote; logger.set('quote', this.#options.quote); return true; } #setShell(data) { const shell = toBool(data); if (shell === undefined) { logger.info(`invalid value for shell flag: ${data[0]}`); return false; } this.#options.shell = shell; logger.set('shell', this.#options.shell); return true; } #setEnv(data) { if (!data[0]) { logger.info(`invalid value for env flag: ${data[0]}`); return false; } const invalid = data.find((val) => { const [key, value, ...rest] = val.split('='); return !key || !value || rest.length > 0; }); if (invalid) { logger.info(`invalid value for env flag: ${invalid}`); return false; } const envData = data.reduce((acc, val) => { const [key, value] = val.split('='); acc[key] = value; return acc; }, {}); this.#options.env = { ...this.#options.env, ...envData }; logger.info('ENV variables', ''); Object.entries(this.#options.env).forEach(([key, val]) => { logger.info('\t', `${key}=${val}`); }); return true; } #setEnvFile(data) { if (!data[0]) { logger.info(`invalid value for env-file flag: ${data[0]}`); return false; } this.#options.envFile = path.resolve(data[0]); logger.set('env File', this.#options.envFile); return true; } #setWorkDir(args) { if (!args[0]) { logger.info(`invalid value for work-dir flag: ${args[0]}`); return false; } this.#options.workDir = path.resolve(args[0]); logger.set('work dir', this.#options.workDir); return true; } #setAlias() { const alias = this.confService.getAlias(this.#key); if (alias) { logger.info(`unset alias: ${this.#key} => ${alias.command.join(' ')}`); } this.confService.setAlias(this.#key, { options: this.#options, command: this.#command }); logger.info(`set alias: ${this.#key} => ${this.#command.join(' ')}`); return true; } }