forked from JoshuaKGoldberg/create-typescript-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaugmentOptionsWithExcludes.ts
242 lines (231 loc) · 6.14 KB
/
augmentOptionsWithExcludes.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import * as prompts from "@clack/prompts";
import chalk from "chalk";
import { filterPromptCancel } from "../prompts.js";
import { Options, OptionsBase } from "../types.js";
interface ExclusionDescription {
hint: string;
label: string;
uncommon?: true;
}
export type ExclusionKey = keyof Options & `exclude${string}`;
const exclusionDescriptions: Record<ExclusionKey, ExclusionDescription> = {
excludeAllContributors: {
hint: "--exclude-all-contributors",
label:
"Add all-contributors to track contributions and display them in a README.md table.",
},
excludeCompliance: {
hint: "--exclude-compliance",
label:
"Add a GitHub Actions workflow to verify that PRs match an expected format.",
uncommon: true,
},
excludeLintDeprecation: {
hint: "--exclude-lint-deprecation",
label:
"Include an eslint-plugin-deprecation to reports on usage of code marked as @deprecated.",
uncommon: true,
},
excludeLintESLint: {
hint: "--exclude-lint-eslint",
label:
"Include eslint-plugin-eslint-comment to enforce good practices around ESLint comment directives.",
uncommon: true,
},
excludeLintJSDoc: {
hint: "--exclude-lint-jsdoc",
label:
"Include eslint-plugin-jsdoc to enforce good practices around JSDoc comments.",
uncommon: true,
},
excludeLintJson: {
hint: "--exclude-lint-json",
label: "Apply linting and sorting to *.json and *.jsonc files.",
uncommon: true,
},
excludeLintKnip: {
hint: "--exclude-lint-knip",
label: "Add Knip to detect unused files, dependencies, and code exports.",
},
excludeLintMd: {
hint: "--exclude-lint-md",
label: "Apply linting to *.md files.",
uncommon: true,
},
excludeLintPackageJson: {
hint: "--exclude-lint-package-json",
label: "Add npm-package-json-lint to lint for package.json correctness.",
uncommon: true,
},
excludeLintPackages: {
hint: "--exclude-lint-packages",
label:
"Add a pnpm dedupe workflow to ensure packages aren't duplicated unnecessarily.",
uncommon: true,
},
excludeLintPerfectionist: {
hint: "--exclude-lint-perfectionist",
label:
"Apply eslint-plugin-perfectionist to ensure imports, keys, and so on are in sorted order.",
uncommon: true,
},
excludeLintRegex: {
hint: "--exclude-lint-regex",
label:
"Include eslint-plugin-regex to enforce good practices around regular expressions.",
uncommon: true,
},
excludeLintSpelling: {
hint: "--exclude-lint-spelling",
label: "Add cspell to spell check against dictionaries of known words.",
uncommon: true,
},
excludeLintStrict: {
hint: "--exclude-lint-strict",
label:
"Include strict logical lint rules such as typescript-eslint's strict config. ",
uncommon: true,
},
excludeLintStylistic: {
hint: "--exclude-lint-stylistic",
label:
"Include stylistic lint rules such as typescript-eslint's stylistic config.",
uncommon: true,
},
excludeLintYml: {
hint: "--exclude-lint-yml",
label: "Apply linting and sorting to *.yaml and *.yml files.",
uncommon: true,
},
excludeReleases: {
hint: "--exclude-releases",
label:
"Add release-it to generate changelogs, package bumps, and publishes based on conventional commits.",
},
excludeRenovate: {
hint: "--exclude-renovate",
label: "Add a Renovate config to keep dependencies up-to-date with PRs.",
},
excludeTests: {
hint: "--exclude-tests",
label:
"Add Vitest tooling for fast unit tests, configured with coverage tracking.",
},
};
const exclusionKeys = Object.keys(exclusionDescriptions) as ExclusionKey[];
export function getExclusions(
options: Partial<Options>,
base?: OptionsBase,
): Partial<Options> {
switch (base) {
case "common":
return {
...Object.fromEntries(
exclusionKeys
.filter((exclusion) => exclusionDescriptions[exclusion].uncommon)
.map((exclusion) => [exclusion, options[exclusion] ?? true]),
),
};
case "minimum":
return {
...Object.fromEntries(
exclusionKeys.map((exclusion) => [
exclusion,
options[exclusion] ?? true,
]),
),
};
// We only really care about exclusions on the common and minimum bases
default:
return {};
}
}
export async function augmentOptionsWithExcludes(
options: Options,
): Promise<Options | undefined> {
if (
Object.keys(options).some(
(key) =>
key in exclusionDescriptions &&
options[key as keyof typeof options] !== undefined,
)
) {
return options;
}
const base =
options.base ??
filterPromptCancel<OptionsBase | symbol>(
await prompts.select({
initialValue: "common" as OptionsBase,
message: `How much tooling would you like the template to set up for you?`,
options: [
{
label: makeLabel(
"everything",
"The most comprehensive tooling imaginable: sorting, spellchecking, and more!",
),
value: "everything",
},
{
hint: "recommended",
label: makeLabel(
"common",
"Bare starters plus testing and automation for all-contributors and releases.",
),
value: "common",
},
{
label: makeLabel(
"minimum",
"Just bare starter tooling: building, formatting, linting, and type checking.",
),
value: "minimum",
},
{
label: makeLabel("prompt", "(allow me to customize)"),
value: "prompt",
},
],
}),
);
switch (base) {
case undefined:
return undefined;
case "common":
case "minimum":
case "everything":
return {
...options,
...getExclusions(options, base),
};
case "prompt":
const exclusionsNotEnabled = new Set(
filterPromptCancel(
await prompts.multiselect({
initialValues: exclusionKeys,
message:
"Select the tooling portions you'd like to remove. All are enabled by default.",
options: Object.entries(exclusionDescriptions).map(
([value, { hint, label }]) => ({
hint,
label,
value: value as ExclusionKey,
}),
),
}),
),
);
return {
...options,
...Object.fromEntries(
exclusionKeys.map(
(exclusionKey) =>
[exclusionKey, !exclusionsNotEnabled.has(exclusionKey)] as const,
),
),
};
}
}
function makeLabel(label: string, message: string) {
return [chalk.bold(label), message].join("\t ");
}