/
utils.ts
206 lines (189 loc) · 5.32 KB
/
utils.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
import { outputFileSync, readJsonSync } from 'fs-extra';
import { join } from 'path';
import { format, resolveConfig } from 'prettier';
import { dedent } from 'tslint/lib/utils';
const stripAnsi = require('strip-ansi');
const importFresh = require('import-fresh');
export function sortAlphabeticallyFunction(a: string, b: string): number {
const nameA = a.toUpperCase(); // ignore upper and lowercase
const nameB = b.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
}
export function sortByBooleanFunction(a: boolean, b: boolean): number {
if (a && !b) {
return -1;
}
if (!a && b) {
return 1;
}
return 0;
}
export async function generateMarkdownFile(
outputDirectory: string,
templateObject: { name: string; template: string }
): Promise<void> {
const filePath = join(outputDirectory, `${templateObject.name}.md`);
outputFileSync(
filePath,
await formatWithPrettier(filePath, stripAnsi(templateObject.template))
);
}
export async function generateJsonFile(
filePath: string,
json: unknown
): Promise<void> {
outputFileSync(
filePath,
await formatWithPrettier(filePath, JSON.stringify(json)),
{ encoding: 'utf8' }
);
}
export async function formatWithPrettier(filePath: string, content: string) {
let options: any = {
filepath: filePath,
};
const resolvedOptions = await resolveConfig(filePath);
if (resolvedOptions) {
options = {
...options,
...resolvedOptions,
};
}
return format(content, options);
}
export function getNxPackageDependencies(packageJsonPath: string): {
name: string;
dependencies: string[];
peerDependencies: string[];
} {
const packageJson = readJsonSync(packageJsonPath);
if (!packageJson) {
console.log(`No package.json found at: ${packageJsonPath}`);
return null;
}
return {
name: packageJson.name,
dependencies: packageJson.dependencies
? Object.keys(packageJson.dependencies).filter((item) =>
item.includes('@nrwl')
)
: [],
peerDependencies: packageJson.peerDependencies
? Object.keys(packageJson.peerDependencies).filter((item) =>
item.includes('@nrwl')
)
: [],
};
}
export function formatDeprecated(
description: string,
deprecated: boolean | string
) {
if (!deprecated) {
return description;
}
return deprecated === true
? `**Deprecated:** ${description}`
: `
**Deprecated:** ${deprecated}
${description}
`;
}
export function getCommands(command) {
return command.getInternalMethods().getCommandInstance().getCommandHandlers();
}
export interface ParsedCommandOption {
name: string;
description: string;
default: string;
deprecated: boolean | string;
}
export interface ParsedCommand {
name: string;
commandString: string;
description: string;
options?: Array<ParsedCommandOption>;
}
export async function parseCommand(
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')().getInternalMethods().reset()
);
const builderDescriptions = builder
.getInternalMethods()
.getUsageInstance()
.getDescriptions();
const builderDefaultOptions = builder.getOptions().default;
const builderAutomatedOptions = builder.getOptions().defaultDescription;
const builderDeprecatedOptions = builder.getDeprecatedOptions();
const builderOptionsChoices = builder.getOptions().choices;
return {
name,
description: command.description,
commandString: command.original.replace('$0', name),
options:
Object.keys(builderDescriptions).map((key) => ({
name: key,
description: builderDescriptions[key]
? builderDescriptions[key].replace('__yargsString__:', '')
: '',
default: builderDefaultOptions[key] ?? builderAutomatedOptions[key],
choices: builderOptionsChoices[key],
deprecated: builderDeprecatedOptions[key],
})) || null,
};
}
export function generateOptionsMarkdown(command): string {
let response = '';
if (Array.isArray(command.options) && !!command.options.length) {
response += '\n## Options';
command.options
.sort((a, b) => sortAlphabeticallyFunction(a.name, b.name))
.forEach((option) => {
response += dedent`
### ${option.deprecated ? `~~${option.name}~~` : option.name}
${
option.default === undefined || option.default === ''
? ''
: `Default: \`${option.default}\`\n`
}`;
response += dedent`
${
option.choices === undefined
? ''
: `Choices: \`[${option.choices
.map((c) => `"${c}"`)
.join(', ')}]\`\n`
}`;
response += dedent`
${formatDeprecated(option.description, option.deprecated)}
`;
});
}
return response;
}