|
| 1 | +// tslint:disable:no-console |
| 2 | +import * as root from 'app-root-path'; |
| 3 | +import chalk from 'chalk'; |
| 4 | +import * as cli from 'commander'; |
| 5 | +import * as fs from 'fs-extra'; |
| 6 | +import { getCommandOptions, getCommands, getCommandValues } from './metadata'; |
| 7 | +import { Command, CommandDefinition, CommandOptionDefinition, CommandValueDefinition, IocContainer } from './types'; |
| 8 | + |
| 9 | +export class Commander { |
| 10 | + private iocContainer?: IocContainer; |
| 11 | + private _version?: string; |
| 12 | + private isCommanderInitialised = false; |
| 13 | + |
| 14 | + public ioc(iocContainer: IocContainer): this { |
| 15 | + this.iocContainer = iocContainer; |
| 16 | + return this; |
| 17 | + } |
| 18 | + |
| 19 | + public version(version: string): this { |
| 20 | + this._version = version; |
| 21 | + return this; |
| 22 | + } |
| 23 | + |
| 24 | + public async execute(argv?: string[]): Promise<void> { |
| 25 | + await this.initialiseCommander(); |
| 26 | + |
| 27 | + const args = argv || process.argv; |
| 28 | + |
| 29 | + if (args.length <= 2) { |
| 30 | + cli.help(); |
| 31 | + } |
| 32 | + |
| 33 | + cli.parse(args); |
| 34 | + } |
| 35 | + |
| 36 | + private async initialiseCommander() { |
| 37 | + if (this.isCommanderInitialised) { |
| 38 | + return; |
| 39 | + } |
| 40 | + await this.setVersion(); |
| 41 | + this.registerCommands(); |
| 42 | + this.isCommanderInitialised = true; |
| 43 | + } |
| 44 | + |
| 45 | + private async getPackageVersion(): Promise<string | undefined> { |
| 46 | + const packageFileName = root.resolve('package.json'); |
| 47 | + if (!await fs.pathExists(packageFileName)) { |
| 48 | + return undefined; |
| 49 | + } |
| 50 | + |
| 51 | + const packageObject = await fs.readJSON(packageFileName); |
| 52 | + |
| 53 | + return packageObject && packageObject.version; |
| 54 | + } |
| 55 | + |
| 56 | + private async setVersion() { |
| 57 | + const version = |
| 58 | + this._version !== undefined ? |
| 59 | + this._version : |
| 60 | + await this.getPackageVersion(); |
| 61 | + |
| 62 | + if (version) { |
| 63 | + cli.version(version); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private getValuesUsage(values: CommandValueDefinition[]) { |
| 68 | + return values |
| 69 | + .map((param) => param.optional ? `[${param.name}]` : `<${param.name}>`) |
| 70 | + .join(' '); |
| 71 | + } |
| 72 | + |
| 73 | + private getOptionUsage(option: CommandOptionDefinition): string { |
| 74 | + let result = ''; |
| 75 | + |
| 76 | + if (option.shortName) { |
| 77 | + result = `-${option.shortName}`; |
| 78 | + } |
| 79 | + |
| 80 | + result += ` --${option.name.toString()}`; |
| 81 | + |
| 82 | + if (option.valueName) { |
| 83 | + result += ` <${option.valueName}>`; |
| 84 | + } |
| 85 | + |
| 86 | + return result.trim(); |
| 87 | + } |
| 88 | + |
| 89 | + private registerCommandOption(cliCommand: cli.Command, paramsClass: { new(...args: any[]): any }, option: CommandOptionDefinition) { |
| 90 | + const optionUsage = this.getOptionUsage(option); |
| 91 | + const coerceValue = !option.valueName ? undefined : ((value: string) => { |
| 92 | + if (option.valueName && option.type === Number) { |
| 93 | + return +value; |
| 94 | + } else { |
| 95 | + return value; |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + const params = new paramsClass(); |
| 100 | + const defaultValue = params[option.name]; |
| 101 | + |
| 102 | + cliCommand.option( |
| 103 | + optionUsage, |
| 104 | + option.description, |
| 105 | + coerceValue, |
| 106 | + defaultValue |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + private getParams( |
| 111 | + command: CommandDefinition<any>, |
| 112 | + args: any[] |
| 113 | + ) { |
| 114 | + const values = getCommandValues(command.paramsClass.prototype); |
| 115 | + const options = getCommandOptions(command.paramsClass.prototype); |
| 116 | + |
| 117 | + const params: { [paramName: string]: any } = new command.paramsClass(); |
| 118 | + let paramIndex = 0; |
| 119 | + |
| 120 | + for (const value of values) { |
| 121 | + params[value.name] = args[paramIndex++]; |
| 122 | + } |
| 123 | + |
| 124 | + const optionValues = args[paramIndex]; |
| 125 | + for (const option of options) { |
| 126 | + params[option.name.toString()] = optionValues[option.name]; |
| 127 | + if (option.shortName) { |
| 128 | + params[option.shortName] = optionValues[option.shortName]; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return params; |
| 133 | + } |
| 134 | + |
| 135 | + private instantiateCommand(command: CommandDefinition<any>) { |
| 136 | + const constructor = command.type as { new(...args: any[]): Command<any> }; |
| 137 | + const instance = this.iocContainer ? this.iocContainer.get(constructor) : new constructor(); |
| 138 | + return instance; |
| 139 | + } |
| 140 | + |
| 141 | + private registerCommand(command: CommandDefinition<any>) { |
| 142 | + const values = getCommandValues(command.paramsClass.prototype); |
| 143 | + const usage = `${command.name} ${this.getValuesUsage(values)}`; |
| 144 | + const cliCommand = cli.command(usage); |
| 145 | + |
| 146 | + if (command.description) { |
| 147 | + cliCommand.description(command.description); |
| 148 | + } |
| 149 | + |
| 150 | + const options = getCommandOptions(command.paramsClass.prototype); |
| 151 | + for (const option of options) { |
| 152 | + this.registerCommandOption(cliCommand, command.paramsClass, option); |
| 153 | + } |
| 154 | + |
| 155 | + cliCommand.action(async (...args: any[]) => { |
| 156 | + try { |
| 157 | + const commandInstance = this.instantiateCommand(command); |
| 158 | + const params = this.getParams(command, args); |
| 159 | + await commandInstance.execute(params); |
| 160 | + } catch (err) { |
| 161 | + console.error(); |
| 162 | + console.error(chalk.red(err.stack || err.message || err)); |
| 163 | + console.error(); |
| 164 | + process.exit(1); |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + private registerCommands() { |
| 170 | + const commands = getCommands(); |
| 171 | + |
| 172 | + for (const command of commands) { |
| 173 | + this.registerCommand(command); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments