UNPKG

alia

Version:
136 lines (135 loc) 4.34 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 = {}; flag = { key: 'set', short: 's', desc: 'set an alias', run: (args) => this.setAlias(args) }; 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) } ]; 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(args) { const separator = this.confService.separator; const separatorIndex = args.findIndex((a) => a === separator); if (separatorIndex === -1) { logger.info(`invalid input, missing separator: ${separator}`); return false; } const key = args[separatorIndex - 1]; let command = args.slice(separatorIndex + 1); if (!key || command.length === 0) { logger.info(`invalid arguments passed: '${key || ''}' ${separator} '${command.join(' ')}'`); return false; } if (command.length === 1) { command = command[0].split(' '); } const alias = this.confService.getAlias(key); if (alias) { logger.info(`unset alias: ${key} ${separator} ${alias.command.join(' ')}`); } this.confService.setAlias(key, { options: this.options, command }); logger.info(`set alias: ${key} ${separator} ${command.join(' ')}`); return true; } }