import {ArgTypeEnum, CliCommandInterface} from "./interfaces/cli-command.interface"; import {newCommand} from "./cli-commands/new-command/new-command"; import {validateConfigCommand} from "./cli-commands/validate-config-command/validate-config-command"; import {Logger} from "./shared/logging/Logger"; import {emojify} from "node-emoji"; import {buildCommand} from "./cli-commands/build-command/build-command"; import _ = require("lodash"); import Table = require('cli-table'); export const commands: CliCommandInterface[] = [ newCommand, validateConfigCommand, buildCommand ]; export function dispatch(cmd: string, args?: string[]): boolean { if (!cmd) { Logger.error('No command found.'); return dispatch('help'); } if (cmd === 'help') { printHelp(); return true; } if (_.some(commands, {name: cmd})) { const command: CliCommandInterface = _.find(commands, {name: cmd}); if (args[0] === 'help') { printHelp(command); return true; } return command._call(); } else { console.error(`Command not found: ${cmd}`); return false; } } function printHelp(cmd?: CliCommandInterface): void { Logger.info('Help'); if (!!cmd) { const table = new Table({ head: ['shortArg', 'longArg', 'arg required', 'type', 'value required', 'description'], // colWidths: [10, 20, 14, 8, 16], colAligns: ['middle', "middle", "middle", "middle", "middle", "left"] }); if (cmd.args) { for (let arg of cmd.args) { table.push([ arg.shortArg && `-${arg.shortArg}` || '', `-${arg.longArg}`, arg.argRequired ? emojify(':white_check_mark:') : emojify(':x:'), arg.type, arg.type !== ArgTypeEnum.void && arg.valueRequired ? emojify(':white_check_mark:') : emojify(':x:'), arg.description || '' ]); } console.log(table.toString()); } else { console.log('No arguments required'); } } else { console.log('Usage: pdf-bunny [args]'); const table = new Table({ head: ['command', 'description'] }); for (let command of commands) { table.push([command.name, command.description]); } console.log(table.toString()); } }