-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-cli-data.ts
212 lines (187 loc) · 5.75 KB
/
generate-cli-data.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import * as chalk from 'chalk';
import { readFileSync } from 'fs';
import { readJsonSync, removeSync } from 'fs-extra';
import { join } from 'path';
import { dedent } from 'tslint/lib/utils';
import {
formatDeprecated,
generateMarkdownFile,
sortAlphabeticallyFunction,
} from './utils';
import { register as registerTsConfigPaths } from 'tsconfig-paths';
import { examples } from '../../packages/nx/src/command-line/examples';
const importFresh = require('import-fresh');
const sharedCommands = [
'build',
'e2e',
'generate',
'lint',
'run',
'serve',
'test',
];
interface ParsedCommandOption {
name: string;
description: string;
default: string;
deprecated: boolean | string;
}
interface ParsedCommand {
name: string;
commandString: string;
description: string;
options?: Array<ParsedCommandOption>;
}
export async function generateCLIDocumentation() {
/**
* For certain commands, they will output dynamic data at runtime in a real workspace,
* so we leverage an envrionment variable to inform the logic of the context that we
* are just statically generating documentation for the current execution.
*/
process.env.NX_GENERATE_DOCS_PROCESS = 'true';
const config = readJsonSync(
join(__dirname, '../../tsconfig.base.json')
).compilerOptions;
registerTsConfigPaths(config);
console.log(`\n${chalk.blue('i')} Generating Documentation for Nx Commands`);
const { commandsObject } = importFresh(
'../../packages/nx/src/command-line/nx-commands'
);
const commandsOutputDirectory = join(
__dirname,
'../../docs/',
'generated',
'cli'
);
removeSync(commandsOutputDirectory);
function getCommands(command) {
return command.getCommandInstance().getCommandHandlers();
}
async function parseCommandInstance(
name: string,
command: any
): Promise<ParsedCommand> {
// It is not a function return a strip down version of the command
if (
!(
command.builder &&
command.builder.constructor &&
command.builder.call &&
command.builder.apply
)
) {
return {
name,
commandString: command.original,
description: command.description,
};
}
// Show all the options we can get from yargs
const builder = await command.builder(
importFresh('yargs')().resetOptions()
);
const builderDescriptions = builder.getUsageInstance().getDescriptions();
const builderDefaultOptions = builder.getOptions().default;
const builderDeprecatedOptions = builder.getDeprecatedOptions();
return {
name,
description: command.description,
commandString: command.original,
options:
Object.keys(builderDescriptions).map((key) => ({
name: key,
description: builderDescriptions[key]
? builderDescriptions[key].replace('__yargsString__:', '')
: '',
default: builderDefaultOptions[key],
deprecated: builderDeprecatedOptions[key],
})) || null,
};
}
function generateMarkdown(command: ParsedCommand) {
let template = dedent`
---
title: "${command.name} - CLI command"
description: "${command.description}"
---
# ${command.name}
${command.description}
## Usage
\`\`\`bash
nx ${command.commandString}
\`\`\`
[Install \`nx\` globally](/getting-started/nx-setup#install-nx) to invoke the command directly using \`nx\`, or use \`npx nx\`, \`yarn nx\`, or \`pnpx nx\`.\n`;
if (examples[command.name] && examples[command.name].length > 0) {
template += `\n### Examples`;
examples[command.name].forEach((example) => {
template += dedent`
${example.description}:
\`\`\`bash
nx ${example.command}
\`\`\`
`;
});
}
if (Array.isArray(command.options) && !!command.options.length) {
template += '\n## Options';
command.options
.sort((a, b) => sortAlphabeticallyFunction(a.name, b.name))
.forEach((option) => {
template += dedent`
### ${option.deprecated ? `~~${option.name}~~` : option.name}
${
option.default === undefined || option.default === ''
? ''
: `Default: \`${option.default}\`\n`
}
`;
template += dedent`
${formatDeprecated(option.description, option.deprecated)}
`;
});
}
return {
name: command.name
.replace(':', '-')
.replace(' ', '-')
.replace(/[\]\[.]+/gm, ''),
template,
};
}
// TODO: Try to add option's type, examples, and group?
const nxCommands = getCommands(commandsObject);
await Promise.all(
Object.keys(nxCommands)
.filter((name) => !sharedCommands.includes(name))
.filter((name) => nxCommands[name].description)
.map((name) => parseCommandInstance(name, nxCommands[name]))
.map(async (command) => generateMarkdown(await command))
.map(async (templateObject) =>
generateMarkdownFile(commandsOutputDirectory, await templateObject)
)
);
await Promise.all(
sharedCommands.map((command) => {
const sharedCommandsDirectory = join(__dirname, '../../docs/shared/cli');
const sharedCommandsOutputDirectory = join(
__dirname,
'../../docs/',
'generated',
'cli'
);
const templateObject = {
name: command,
template: readFileSync(
join(sharedCommandsDirectory, `${command}.md`),
'utf-8'
),
};
return generateMarkdownFile(
sharedCommandsOutputDirectory,
templateObject
);
})
);
delete process.env.NX_GENERATE_DOCS_PROCESS;
console.log(`${chalk.green('✓')} Generated Documentation for Nx Commands`);
}