|
| 1 | +import { run, ExecutionOptions } from '@nativescript/schematics-executor'; |
| 2 | + |
| 3 | +export class GenerateCommand implements ICommand { |
| 4 | + public allowedParameters: ICommandParameter[] = []; |
| 5 | + private executionOptions: ExecutionOptions; |
| 6 | + |
| 7 | + constructor(private $logger: ILogger, |
| 8 | + private $options: IOptions, |
| 9 | + private $errors: IErrors) { } |
| 10 | + |
| 11 | + public async execute(_rawArgs: string[]): Promise<void> { |
| 12 | + try { |
| 13 | + await run(this.executionOptions); |
| 14 | + } catch (error) { |
| 15 | + this.$errors.failWithoutHelp(error.message); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + public async canExecute(rawArgs: string[]): Promise<boolean> { |
| 20 | + this.setExecutionOptions(rawArgs); |
| 21 | + this.validateExecutionOptions(); |
| 22 | + |
| 23 | + return true; |
| 24 | + } |
| 25 | + |
| 26 | + private validateExecutionOptions() { |
| 27 | + if (!this.executionOptions.schematic) { |
| 28 | + this.$errors.fail(`The generate command requires a schematic name to be specified.`); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private setExecutionOptions(rawArgs: string[]) { |
| 33 | + const options = this.parseRawArgs(rawArgs); |
| 34 | + |
| 35 | + this.executionOptions = { |
| 36 | + ...options, |
| 37 | + logger: this.$logger, |
| 38 | + directory: process.cwd(), |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + private parseRawArgs(rawArgs: string[]) { |
| 43 | + const collection = this.$options.collection; |
| 44 | + const schematic = rawArgs.shift(); |
| 45 | + const { options, args } = parseSchematicSettings(rawArgs); |
| 46 | + |
| 47 | + return { |
| 48 | + collection, |
| 49 | + schematic, |
| 50 | + schematicOptions: options, |
| 51 | + schematicArgs: args, |
| 52 | + }; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Converts an array of command line arguments to options for the executed schematic. |
| 58 | + * @param rawArgs The command line arguments. They should be in the format 'key=value' for strings or 'key' for booleans. |
| 59 | + */ |
| 60 | +function parseSchematicSettings(rawArgs: string[]) { |
| 61 | + const [optionStrings, args] = partition<string>(rawArgs, item => item.includes('=')); |
| 62 | + const options = optionStrings |
| 63 | + .map(o => o.split("=")) // split to key and value pairs |
| 64 | + .map(([key, ...value]) => [key, value.join("=")]) // concat the value arrays if there are multiple = signs |
| 65 | + .reduce((obj, [key, value]) => { |
| 66 | + return { ...obj, [key]: value }; |
| 67 | + }, {}); |
| 68 | + |
| 69 | + return { options, args }; |
| 70 | +} |
| 71 | +/** |
| 72 | + * Splits an array into two groups based on a predicate. |
| 73 | + * @param array The array to split. |
| 74 | + * @param predicate The condition to be used for splitting. |
| 75 | + */ |
| 76 | +function partition<T>(array: T[], predicate: (item: T) => boolean): T[][] { |
| 77 | + return array.reduce(([pass, fail], item) => { |
| 78 | + return predicate(item) ? |
| 79 | + [[...pass, item], fail] : |
| 80 | + [pass, [...fail, item]]; |
| 81 | + }, [[], []]); |
| 82 | +} |
| 83 | + |
| 84 | +$injector.registerCommand("generate", GenerateCommand); |
0 commit comments