forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.ts
More file actions
64 lines (57 loc) · 2.05 KB
/
command.ts
File metadata and controls
64 lines (57 loc) · 2.05 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
/**
* @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 { Argv } from 'yargs';
import {
CommandContext,
CommandModule,
CommandModuleError,
CommandModuleImplementation,
CommandScope,
} from '../command-module';
export const demandCommandFailureMessage = `You need to specify a command before moving on. Use '--help' to view the available commands.`;
export type CommandModuleConstructor = Partial<CommandModuleImplementation> & {
new (context: CommandContext): Partial<CommandModuleImplementation> & CommandModule;
};
export function addCommandModuleToYargs<U extends CommandModuleConstructor>(
commandModule: U,
context: CommandContext,
): void {
const cmd = new commandModule(context);
const {
args: {
options: { jsonHelp },
},
workspace,
} = context;
const describe = jsonHelp ? cmd.fullDescribe : cmd.describe;
context.yargsInstance.command({
command: cmd.command,
aliases: cmd.aliases,
describe:
// We cannot add custom fields in help, such as long command description which is used in AIO.
// Therefore, we get around this by adding a complex object as a string which we later parse when generating the help files.
typeof describe === 'object' ? JSON.stringify(describe) : describe,
deprecated: cmd.deprecated,
builder: (argv) => {
// Skip scope validation when running with '--json-help' since it's easier to generate the output for all commands this way.
const isInvalidScope =
!jsonHelp &&
((cmd.scope === CommandScope.In && !workspace) ||
(cmd.scope === CommandScope.Out && workspace));
if (isInvalidScope) {
throw new CommandModuleError(
`This command is not available when running the Angular CLI ${
workspace ? 'inside' : 'outside'
} a workspace.`,
);
}
return cmd.builder(argv);
},
handler: (args) => cmd.handler(args),
});
}