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