/
generate-cnw-documentation.ts
121 lines (103 loc) · 3.25 KB
/
generate-cnw-documentation.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
import * as chalk from 'chalk';
import { join } from 'path';
import { dedent } from 'tslint/lib/utils';
import {
formatDeprecated,
sortAlphabeticallyFunction,
generateMarkdownFile,
} from './utils';
const importFresh = require('import-fresh');
export async function generateCNWocumentation() {
process.env.NX_GENERATE_DOCS_PROCESS = 'true';
console.log(
`\n${chalk.blue(
'i'
)} Generating Documentation for Create Nx Workspace Command`
);
const { commandsObject } = importFresh(
'../../packages/create-nx-workspace/bin/create-nx-workspace'
);
const commandsOutputDirectory = join(
__dirname,
'../../docs/',
'generated',
'cli'
);
const command = commandsObject.getCommandInstance().getCommandHandlers()[
'$0'
];
const parsedCommand = await parseCommand(command);
const markdown = generateMarkdown(parsedCommand);
generateMarkdownFile(commandsOutputDirectory, markdown);
delete process.env.NX_GENERATE_DOCS_PROCESS;
console.log(
`${chalk.green(
'✓'
)} Generated Documentation for Create Nx Workspace Command`
);
}
interface ParsedCommandOption {
name: string;
description: string;
default: string;
deprecated: boolean | string;
}
interface ParsedCommand {
commandString: string;
description: string;
options?: Array<ParsedCommandOption>;
}
async function parseCommand(command: any): Promise<ParsedCommand> {
const builder = await command.builder(importFresh('yargs')().resetOptions());
const builderDescriptions = builder.getUsageInstance().getDescriptions();
const builderDefaultOptions = builder.getOptions().default;
const builderAutomatedOptions = builder.getOptions().defaultDescription;
const builderDeprecatedOptions = builder.getDeprecatedOptions();
return {
description: command.description,
commandString: command.original.replace('$0', 'create-nx-workspace'),
options:
Object.keys(builderDescriptions).map((key) => ({
name: key,
description: builderDescriptions[key]
? builderDescriptions[key].replace('__yargsString__:', '')
: '',
default: builderDefaultOptions[key] || builderAutomatedOptions[key],
deprecated: builderDeprecatedOptions[key],
})) || null,
};
}
function generateMarkdown(command: ParsedCommand) {
let template = dedent`
---
title: "create-nx-workspace - CLI command"
description: "${command.description}"
---
# create-nx-workspace
${command.description}
## Usage
\`\`\`bash
${command.commandString}
\`\`\`
Install \`create-nx-workspace\` globally to invoke the command directly, or use \`npx create-nx-workspace\`, \`yarn create nx-workspace\`, or \`pnpx create-nx-workspace\`.\n`;
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: 'create-nx-workspace',
template,
};
}