-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathreadOptions.ts
222 lines (199 loc) · 6.04 KB
/
readOptions.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
import { parseArgs } from "node:util";
import { titleCase } from "title-case";
import { z } from "zod";
import { withSpinner } from "../cli/spinners.js";
import { Mode, PromptedOptions } from "../types.js";
import { Options, OptionsLogo } from "../types.js";
import { allArgOptions } from "./args.js";
import { augmentOptionsWithExcludes } from "./augmentOptionsWithExcludes.js";
import { createOptionDefaults } from "./createOptionDefaults/index.js";
import { detectEmailRedundancy } from "./detectEmailRedundancy.js";
import { ensureRepositoryExists } from "./ensureRepositoryExists.js";
import { GitHub, getGitHub } from "./getGitHub.js";
import { getPrefillOrPromptedOption } from "./getPrefillOrPromptedOption.js";
import { optionsSchema } from "./optionsSchema.js";
export interface GitHubAndOptions {
github: GitHub | undefined;
options: Options;
}
export interface OptionsParseCancelled {
cancelled: true;
error?: string | z.ZodError<object>;
options: object;
}
export interface OptionsParseSuccess extends GitHubAndOptions {
cancelled: false;
}
export type OptionsParseResult = OptionsParseCancelled | OptionsParseSuccess;
export async function readOptions(
args: string[],
mode: Mode,
promptedOptions: PromptedOptions = {},
): Promise<OptionsParseResult> {
const defaults = createOptionDefaults(promptedOptions);
const { values } = parseArgs({
args,
options: allArgOptions,
strict: false,
tokens: true,
});
const mappedOptions = {
access: values.access,
author: values.author,
base: values.base,
description: values.description,
directory: values.directory,
email:
values.email ?? values["email-github"] ?? values["email-npm"]
? {
github: values.email ?? values["email-github"],
npm: values.email ?? values["email-npm"],
}
: undefined,
excludeAllContributors: values["exclude-all-contributors"],
excludeCompliance: values["exclude-compliance"],
excludeLintDeprecation: values["exclude-lint-deprecation"],
excludeLintESLint: values["exclude-lint-eslint"],
excludeLintJSDoc: values["exclude-lint-jsdoc"],
excludeLintJson: values["exclude-lint-json"],
excludeLintKnip: values["exclude-lint-knip"],
excludeLintMd: values["exclude-lint-md"],
excludeLintPackageJson: values["exclude-lint-package-json"],
excludeLintPackages: values["exclude-lint-packages"],
excludeLintPerfectionist: values["exclude-lint-perfectionist"],
excludeLintRegex: values["exclude-lint-regex"],
excludeLintSpelling: values["exclude-lint-spelling"],
excludeLintStrict: values["exclude-lint-strict"],
excludeLintYml: values["exclude-lint-yml"],
excludeReleases: values["exclude-releases"],
excludeRenovate: values["exclude-renovate"],
excludeTests: values["unit-tests"],
funding: values.funding,
offline: values.offline,
owner: values.owner,
preserveGeneratedFrom:
values["preserve-generated-from"] ?? values.owner === "JoshuaKGoldberg",
repository: values.repository,
skipAllContributorsApi:
values["skip-all-contributors-api"] ?? values.offline,
skipGitHubApi: values["skip-github-api"] ?? values.offline,
skipInstall: values["skip-install"],
skipRemoval: values["skip-removal"],
skipRestore: values["skip-restore"],
skipUninstall: values["skip-uninstall"],
title: values.title,
};
const emailError = detectEmailRedundancy(values);
if (emailError) {
return {
cancelled: true,
error: emailError,
options: mappedOptions,
};
}
const optionsParseResult = optionsSchema.safeParse(mappedOptions);
if (!optionsParseResult.success) {
return {
cancelled: true,
error: optionsParseResult.error,
options: mappedOptions,
};
}
const options = optionsParseResult.data;
options.owner ??= await getPrefillOrPromptedOption(
"What organization or user will the repository be under?",
defaults.owner,
);
if (!options.owner) {
return {
cancelled: true,
options,
};
}
options.repository ??= await getPrefillOrPromptedOption(
"What will the kebab-case name of the repository be?",
defaults.repository,
);
if (!options.repository) {
return {
cancelled: true,
options,
};
}
const { github, repository } = await ensureRepositoryExists(
options.skipGitHubApi
? undefined
: await withSpinner("Checking GitHub authentication", getGitHub),
{
mode,
owner: options.owner,
repository: options.repository,
},
);
if (!repository) {
return { cancelled: true, options };
}
options.description ??= await getPrefillOrPromptedOption(
"How would you describe the new package?",
async () =>
(await defaults.description()) ?? "A very lovely package. Hooray!",
);
if (!options.description) {
return { cancelled: true, options };
}
options.title ??= await getPrefillOrPromptedOption(
"What will the Title Case title of the repository be?",
async () =>
(await defaults.title()) ?? titleCase(repository).replaceAll("-", " "),
);
if (!options.title) {
return { cancelled: true, options };
}
let logo: OptionsLogo | undefined;
if (options.logo) {
const alt =
options.logoAlt ??
(await getPrefillOrPromptedOption(
"What is the alt text (non-visual description) of the logo?",
));
if (!alt) {
return { cancelled: true, options };
}
logo = { alt, src: options.logo };
}
const email =
options.email ??
(await defaults.email()) ??
(await getPrefillOrPromptedOption(
"What email should be used in package.json and .md files?",
));
if (!email) {
return { cancelled: true, options };
}
const augmentedOptions = await augmentOptionsWithExcludes({
...options,
access: options.access ?? "public",
author: options.author ?? (await defaults.owner()),
description: options.description,
directory:
options.directory ?? promptedOptions.directory ?? options.repository,
email: typeof email === "string" ? { github: email, npm: email } : email,
funding: options.funding ?? (await defaults.funding()),
logo,
mode,
owner: options.owner,
repository,
title: options.title,
});
if (!augmentedOptions) {
return {
cancelled: true,
options,
};
}
return {
cancelled: false,
github,
options: augmentedOptions,
};
}