|
9 | 9 | import { logging } from '@angular-devkit/core'; |
10 | 10 | import yargs from 'yargs'; |
11 | 11 | import { Parser } from 'yargs/helpers'; |
12 | | -import { AddCommandModule } from '../commands/add/cli'; |
13 | | -import { AnalyticsCommandModule } from '../commands/analytics/cli'; |
14 | | -import { BuildCommandModule } from '../commands/build/cli'; |
15 | | -import { CacheCommandModule } from '../commands/cache/cli'; |
16 | | -import { CompletionCommandModule } from '../commands/completion/cli'; |
17 | | -import { ConfigCommandModule } from '../commands/config/cli'; |
18 | | -import { DeployCommandModule } from '../commands/deploy/cli'; |
19 | | -import { DocCommandModule } from '../commands/doc/cli'; |
20 | | -import { E2eCommandModule } from '../commands/e2e/cli'; |
21 | | -import { ExtractI18nCommandModule } from '../commands/extract-i18n/cli'; |
22 | | -import { GenerateCommandModule } from '../commands/generate/cli'; |
23 | | -import { LintCommandModule } from '../commands/lint/cli'; |
24 | | -import { AwesomeCommandModule } from '../commands/make-this-awesome/cli'; |
25 | | -import { NewCommandModule } from '../commands/new/cli'; |
26 | | -import { RunCommandModule } from '../commands/run/cli'; |
27 | | -import { ServeCommandModule } from '../commands/serve/cli'; |
28 | | -import { TestCommandModule } from '../commands/test/cli'; |
29 | | -import { UpdateCommandModule } from '../commands/update/cli'; |
30 | | -import { VersionCommandModule } from '../commands/version/cli'; |
| 12 | +import { |
| 13 | + CommandConfig, |
| 14 | + CommandNames, |
| 15 | + RootCommands, |
| 16 | + RootCommandsAliases, |
| 17 | +} from '../commands/command-config'; |
31 | 18 | import { colors } from '../utilities/color'; |
32 | 19 | import { AngularWorkspace, getWorkspace } from '../utilities/config'; |
33 | 20 | import { assertIsError } from '../utilities/error'; |
34 | 21 | import { PackageManagerUtils } from '../utilities/package-manager'; |
35 | 22 | import { CommandContext, CommandModuleError } from './command-module'; |
36 | | -import { addCommandModuleToYargs, demandCommandFailureMessage } from './utilities/command'; |
| 23 | +import { |
| 24 | + CommandModuleConstructor, |
| 25 | + addCommandModuleToYargs, |
| 26 | + demandCommandFailureMessage, |
| 27 | +} from './utilities/command'; |
37 | 28 | import { jsonHelpUsage } from './utilities/json-help'; |
38 | 29 | import { normalizeOptionsMiddleware } from './utilities/normalize-options-middleware'; |
39 | 30 |
|
40 | | -const COMMANDS = [ |
41 | | - VersionCommandModule, |
42 | | - DocCommandModule, |
43 | | - AwesomeCommandModule, |
44 | | - ConfigCommandModule, |
45 | | - AnalyticsCommandModule, |
46 | | - AddCommandModule, |
47 | | - GenerateCommandModule, |
48 | | - BuildCommandModule, |
49 | | - E2eCommandModule, |
50 | | - TestCommandModule, |
51 | | - ServeCommandModule, |
52 | | - ExtractI18nCommandModule, |
53 | | - DeployCommandModule, |
54 | | - LintCommandModule, |
55 | | - NewCommandModule, |
56 | | - UpdateCommandModule, |
57 | | - RunCommandModule, |
58 | | - CacheCommandModule, |
59 | | - CompletionCommandModule, |
60 | | -].sort(); // Will be sorted by class name. |
61 | | - |
62 | 31 | const yargsParser = Parser as unknown as typeof Parser.default; |
63 | 32 |
|
64 | 33 | export async function runCommand(args: string[], logger: logging.Logger): Promise<number> { |
@@ -111,7 +80,7 @@ export async function runCommand(args: string[], logger: logging.Logger): Promis |
111 | 80 | }; |
112 | 81 |
|
113 | 82 | let localYargs = yargs(args); |
114 | | - for (const CommandModule of COMMANDS) { |
| 83 | + for (const CommandModule of await getCommandsToRegister(positional[0])) { |
115 | 84 | localYargs = addCommandModuleToYargs(localYargs, CommandModule, context); |
116 | 85 | } |
117 | 86 |
|
@@ -168,3 +137,23 @@ export async function runCommand(args: string[], logger: logging.Logger): Promis |
168 | 137 |
|
169 | 138 | return process.exitCode ?? 0; |
170 | 139 | } |
| 140 | + |
| 141 | +/** |
| 142 | + * Get the commands that need to be registered. |
| 143 | + * @returns One or more command factories that needs to be registered. |
| 144 | + */ |
| 145 | +async function getCommandsToRegister( |
| 146 | + commandName: string | number, |
| 147 | +): Promise<CommandModuleConstructor[]> { |
| 148 | + const commands: CommandConfig[] = []; |
| 149 | + if (commandName in RootCommands) { |
| 150 | + commands.push(RootCommands[commandName as CommandNames]); |
| 151 | + } else if (commandName in RootCommandsAliases) { |
| 152 | + commands.push(RootCommandsAliases[commandName]); |
| 153 | + } else { |
| 154 | + // Unknown command, register every possible command. |
| 155 | + Object.values(RootCommands).forEach((c) => commands.push(c)); |
| 156 | + } |
| 157 | + |
| 158 | + return Promise.all(commands.map((command) => command.factory().then((m) => m.default))); |
| 159 | +} |
0 commit comments