|
| 1 | +import {Command} from './command'; |
| 2 | +import {CLI} from 'command-line-commands'; |
| 3 | +import * as commandLineArgs from 'command-line-args'; |
| 4 | + |
| 5 | +export class HelpCommand implements Command { |
| 6 | + name = 'help'; |
| 7 | + |
| 8 | + description = 'Shows this help message, or help for a specific command'; |
| 9 | + |
| 10 | + args = [{ |
| 11 | + name: 'command', |
| 12 | + description: 'The command to display help for', |
| 13 | + defaultOption: true, |
| 14 | + }]; |
| 15 | + |
| 16 | + commands : Map<String, Command> = new Map(); |
| 17 | + |
| 18 | + constructor(commands : Map<String, Command>) { |
| 19 | + this.commands = commands; |
| 20 | + } |
| 21 | + |
| 22 | + run(options): Promise<any> { |
| 23 | + return new Promise<any>((resolve, _) => { |
| 24 | + if (options.command) { |
| 25 | + let command = this.commands.get(options.command); |
| 26 | + if (!command) { |
| 27 | + console.log(`unknown command ${command}`); |
| 28 | + } else { |
| 29 | + let argsCli = commandLineArgs(command.args); |
| 30 | + console.log(argsCli.getUsage({ |
| 31 | + title: `polytool ${command.name}`, |
| 32 | + description: command.description, |
| 33 | + })); |
| 34 | + } |
| 35 | + } else { |
| 36 | + console.log(`polytool - The Polymer command-line tool\n`); |
| 37 | + console.log(`usage: polytool <command> [<args>]\n`); |
| 38 | + console.log(`polytool supports the following commands:`); |
| 39 | + for (let command of this.commands.values()) { |
| 40 | + console.log(` ${command.name}\t\t${command.description}`); |
| 41 | + } |
| 42 | + console.log(`\nRun \`polytool help <command>\` for help on a specific command.\n`); |
| 43 | + } |
| 44 | + resolve(null); |
| 45 | + }); |
| 46 | + } |
| 47 | +} |
0 commit comments