Skip to content

Commit c0fe062

Browse files
alan-agius4clydin
authored andcommitted
build: handle default sub command in json help output
This change is needed to handle default subcommands which are used in `ng generate` default subcommands are prefixed with `$0` or `*`. More info: https://github.com/yargs/yargs/blob/main/docs/advanced.md#default-commands
1 parent d560e23 commit c0fe062

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

packages/angular/cli/src/command-builder/utilities/json-help.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export interface JsonHelp extends JsonHelpDescription {
4040
subcommands?: JsonHelpSubcommand[];
4141
}
4242

43+
const yargsDefaultCommandRegExp = /^\$0|\*/;
44+
4345
export function jsonHelpUsage(): string {
4446
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4547
const localYargs = yargs as any;
@@ -106,23 +108,26 @@ export function jsonHelpUsage(): string {
106108
deprecated: string | boolean,
107109
][]
108110
)
109-
.map(([name, rawDescription, _, aliases, deprecated]) => ({
110-
name: name.split(' ', 1)[0],
111-
command: name,
111+
.map(([name, rawDescription, isDefault, aliases, deprecated]) => ({
112+
name: name.split(' ', 1)[0].replace(yargsDefaultCommandRegExp, ''),
113+
command: name.replace(yargsDefaultCommandRegExp, ''),
114+
default: isDefault || undefined,
112115
...parseDescription(rawDescription),
113116
aliases,
114117
deprecated,
115118
}))
116119
.sort((a, b) => a.name.localeCompare(b.name));
117120

118121
const [command, rawDescription] = usageInstance.getUsage()[0] ?? [];
122+
const defaultSubCommand = subcommands.find((x) => x.default)?.command ?? '';
123+
const otherSubcommands = subcommands.filter((s) => !s.default);
119124

120125
const output: JsonHelp = {
121126
name: [...context.commands].pop(),
122-
command: command?.replace('$0', localYargs['$0']),
127+
command: `${command?.replace(yargsDefaultCommandRegExp, localYargs['$0'])}${defaultSubCommand}`,
123128
...parseDescription(rawDescription),
124129
options: normalizeOptions.sort((a, b) => a.name.localeCompare(b.name)),
125-
subcommands: subcommands.length ? subcommands : undefined,
130+
subcommands: otherSubcommands.length ? otherSubcommands : undefined,
126131
};
127132

128133
return JSON.stringify(output, undefined, 2);

scripts/json-help.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { logging } from '@angular-devkit/core';
10-
import { spawn } from 'child_process';
10+
import { spawn, spawnSync } from 'child_process';
1111
import { promises as fs } from 'fs';
1212
import * as os from 'os';
1313
import { JsonHelp } from 'packages/angular/cli/src/command-builder/utilities/json-help';
@@ -38,32 +38,18 @@ export default async function ({ temporaryProjectRoot }: JsonHelpOptions, logger
3838

3939
await fs.mkdir(helpOutputRoot);
4040

41-
const runNgCommandJsonHelp = async (args: string[]) => {
42-
const process = spawn(ngPath, [...args, '--json-help', '--help'], {
41+
const runNgCommandJsonHelp = (args: string[]): Promise<JsonHelp> => {
42+
const { stdout, status } = spawnSync(ngPath, [...args, '--json-help', '--help'], {
4343
cwd: newProjectRoot,
44+
maxBuffer: 200_0000,
4445
stdio: ['ignore', 'pipe', 'inherit'],
4546
});
4647

47-
let result = '';
48-
process.stdout.on('data', (data) => {
49-
result += data.toString();
50-
});
51-
52-
return new Promise<JsonHelp>((resolve, reject) => {
53-
process
54-
.on('close', (code) => {
55-
if (code === 0) {
56-
resolve(JSON.parse(result.trim()));
57-
} else {
58-
reject(
59-
new Error(
60-
`Command failed: ${ngPath} ${args.map((x) => JSON.stringify(x)).join(', ')}`,
61-
),
62-
);
63-
}
64-
})
65-
.on('error', (err) => reject(err));
66-
});
48+
if (status === 0) {
49+
return Promise.resolve(JSON.parse(stdout.toString().trim()));
50+
} else {
51+
throw new Error(`Command failed: ${ngPath} ${args.map((x) => JSON.stringify(x)).join(', ')}`);
52+
}
6753
};
6854

6955
const { subcommands: commands = [] } = await runNgCommandJsonHelp([]);

0 commit comments

Comments
 (0)