Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
126 lines (105 loc) · 3.92 KB
/
Copy pathcli.ts
File metadata and controls
126 lines (105 loc) · 3.92 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { Target } from '@angular-devkit/architect';
import { Argv } from 'yargs';
import { ArchitectBaseCommandModule } from '../../command-builder/architect-base-command-module';
import {
CommandModuleError,
CommandModuleImplementation,
CommandScope,
Options,
OtherOptions,
} from '../../command-builder/command-module';
import longDescription from './long-description.md';
export interface RunCommandArgs {
target: string;
}
export default class RunCommandModule
extends ArchitectBaseCommandModule<RunCommandArgs>
implements CommandModuleImplementation<RunCommandArgs>
{
override scope = CommandScope.In;
command = 'run <target>';
describe =
'Runs an Architect target with an optional custom builder configuration defined in your project.';
override longDescription = longDescription;
async builder(argv: Argv): Promise<Argv<RunCommandArgs>> {
const { jsonHelp, getYargsCompletions, help } = this.context.args.options;
const localYargs: Argv<RunCommandArgs> = argv
.positional('target', {
describe:
'The Architect target to run provided in the following format `project:target[:configuration]`.',
type: 'string',
demandOption: true,
// Show only in when using --help and auto completion because otherwise comma seperated configuration values will be invalid.
// Also, hide choices from JSON help so that we don't display them in AIO.
choices: (getYargsCompletions || help) && !jsonHelp ? this.getTargetChoices() : undefined,
})
.middleware((args) => {
// TODO: remove in version 15.
const { configuration, target } = args;
if (typeof configuration === 'string' && target) {
const targetWithConfig = target.split(':', 2);
targetWithConfig.push(configuration);
throw new CommandModuleError(
'Unknown argument: configuration.\n' +
`Provide the configuration as part of the target 'ng run ${targetWithConfig.join(
':',
)}'.`,
);
}
}, true)
.strict();
const target = this.makeTargetSpecifier();
if (!target) {
return localYargs;
}
const schemaOptions = await this.getArchitectTargetOptions(target);
return this.addSchemaOptionsToCommand(localYargs, schemaOptions);
}
async run(options: Options<RunCommandArgs> & OtherOptions): Promise<number> {
const target = this.makeTargetSpecifier(options);
const { target: _target, ...extraOptions } = options;
if (!target) {
throw new CommandModuleError('Cannot determine project or target.');
}
return this.runSingleTarget(target, extraOptions);
}
protected makeTargetSpecifier(options?: Options<RunCommandArgs>): Target | undefined {
const architectTarget = options?.target ?? this.context.args.positional[1];
if (!architectTarget) {
return undefined;
}
const [project = '', target = '', configuration] = architectTarget.split(':');
return {
project,
target,
configuration,
};
}
/** @returns a sorted list of target specifiers to be used for auto completion. */
private getTargetChoices(): string[] | undefined {
if (!this.context.workspace) {
return;
}
const targets = [];
for (const [projectName, project] of this.context.workspace.projects) {
for (const [targetName, target] of project.targets) {
const currentTarget = `${projectName}:${targetName}`;
targets.push(currentTarget);
if (!target.configurations) {
continue;
}
for (const configName of Object.keys(target.configurations)) {
targets.push(`${currentTarget}:${configName}`);
}
}
}
return targets.sort();
}
}