- Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathjson-help.mts
99 lines (84 loc) · 3.27 KB
/
json-help.mts
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
/**
* @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{spawnSync}from'node:child_process';
import{promisesasfs}from'node:fs';
import*asosfrom'node:os';
import*aspathfrom'node:path';
import{fileURLToPath}from'node:url';
importcreatefrom'./create.mjs';
const__dirname=path.dirname(fileURLToPath(import.meta.url));
exportasyncfunctioncreateTemporaryProject(): Promise<string>{
console.info('Creating temporary project...');
constnewProjectTempRoot=awaitfs.mkdtemp(path.join(os.tmpdir(),'angular-cli-create-'));
constnewProjectName='help-project';
constnewProjectRoot=path.join(newProjectTempRoot,newProjectName);
awaitcreate({_: [newProjectName]},newProjectTempRoot);
returnnewProjectRoot;
}
exportinterfaceJsonHelpOptions{
temporaryProjectRoot?: string;
}
exportdefaultasyncfunction({ temporaryProjectRoot }: JsonHelpOptions){
console.info('Gathering JSON Help...');
constnewProjectRoot=temporaryProjectRoot??(awaitcreateTemporaryProject());
constngPath=path.join(newProjectRoot,'node_modules/.bin/ng');
consthelpOutputRoot=path.join(__dirname,'../dist/@angular/cli/help');
awaitfs.mkdir(helpOutputRoot);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
construnNgCommandJsonHelp=async(args: string[]): Promise<any>=>{
const{ stdout, status }=spawnSync(ngPath,[...args,'--json-help','--help'],{
cwd: newProjectRoot,
maxBuffer: 200_0000,
stdio: ['ignore','pipe','inherit'],
});
if(status===0){
try{
returnJSON.parse(stdout.toString().trim());
}catch(e){
console.error(`${e}`);
}
}
thrownewError(`Command failed: ${ngPath}${args.map((x)=>JSON.stringify(x)).join(', ')}.`);
};
const{subcommands: commands=[]}=awaitrunNgCommandJsonHelp([]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constcommandsHelp=commands.map((command: any)=>
runNgCommandJsonHelp([command.name]).then((c)=>({
...command,
...c,
})),
);
forawait(constcommandofcommandsHelp){
constcommandName=command.name;
constcommandOptionNames=newSet([
...command.options.map(({ name }: {name: string})=>name),
]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constsubCommandsHelp=command.subcommands?.map((subcommand: any)=>
runNgCommandJsonHelp([command.name,subcommand.name]).then((s)=>({
...s,
...subcommand,
// Filter options which are inherited from the parent command.
// Ex: `interactive` in `ng generate lib`.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options: s.options.filter((o: any)=>!commandOptionNames.has(o.name)),
})),
);
constjsonOutput=JSON.stringify(
{
...command,
subcommands: subCommandsHelp ? awaitPromise.all(subCommandsHelp) : undefined,
},
undefined,
2,
);
constfilePath=path.join(helpOutputRoot,commandName+'.json');
awaitfs.writeFile(filePath,jsonOutput);
console.info(filePath);
}
}