-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathbase.ts
265 lines (241 loc) · 7.51 KB
/
base.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { BaseOptionsFor, createBase } from "bingo-stratum";
import { execaCommand } from "execa";
import gitRemoteOriginUrl from "git-remote-origin-url";
import gitUrlParse from "git-url-parse";
import { inputFromFile } from "input-from-file";
import { inputFromFileJSON } from "input-from-file-json";
import { inputFromScript } from "input-from-script";
import lazyValue from "lazy-value";
import npmUser from "npm-user";
import { z } from "zod";
import { inputFromOctokit } from "./inputs/inputFromOctokit.js";
import { parsePackageAuthor } from "./options/parsePackageAuthor.js";
import { readDefaultsFromReadme } from "./options/readDefaultsFromReadme.js";
import { readDescription } from "./options/readDescription.js";
import { readDocumentation } from "./options/readDocumentation.js";
import { readEmails } from "./options/readEmails.js";
import { readFileSafe } from "./options/readFileSafe.js";
import { readFunding } from "./options/readFunding.js";
import { readGuide } from "./options/readGuide.js";
import { readPackageData } from "./options/readPackageData.js";
import { AllContributorsData } from "./types.js";
import { swallowError } from "./utils/swallowError.js";
import { tryCatchLazyValueAsync } from "./utils/tryCatchLazyValueAsync.js";
const zContributor = z.object({
avatar_url: z.string(),
contributions: z.array(z.string()),
login: z.string(),
name: z.string(),
profile: z.string(),
});
export type Contributor = z.infer<typeof zContributor>;
export const base = createBase({
options: {
access: z
.union([z.literal("public"), z.literal("restricted")])
.describe("which `npm publish --access` to release npm packages with"),
author: z
.string()
.optional()
.describe("username on npm to publish packages under"),
bin: z
.string()
.optional()
.describe('value to set in `package.json`\'s `"bin"` property'),
contributors: z
.array(zContributor)
.optional()
.describe("AllContributors contributors to store in .all-contributorsrc"),
description: z
.string()
.default("A very lovely package. Hooray! 💖")
.describe("'Sentence case.' description of the repository"),
directory: z.string().describe("Directory to create the repository in"),
documentation: z
.string()
.optional()
.describe("any additional docs to add to .github/DEVELOPMENT.md"),
email: z
.union([
z.string(),
z.object({
github: z.string(),
npm: z.string(),
}),
])
.transform((email) =>
typeof email === "string" ? { github: email, npm: email } : email,
)
.describe(
"email address to be listed as the point of contact in docs and packages",
),
explainer: z
.array(z.string())
.optional()
.describe("additional README.md sentence(s) describing the package"),
funding: z
.string()
.optional()
.describe("GitHub organization or username to mention in `funding.yml`"),
guide: z
.object({
href: z.string(),
title: z.string(),
})
.optional()
.describe(
"link to a contribution guide to place at the top of development docs",
),
keywords: z
.array(z.string())
.optional()
.describe("any number of keywords to include in `package.json`"),
logo: z
.object({
alt: z.string(),
height: z.number().optional(),
src: z.string(),
width: z.number().optional(),
})
.optional()
.describe(
"local image file in the repository to display near the top of the README.md",
),
node: z
.object({
minimum: z.string(),
pinned: z.string().optional(),
})
.optional()
.describe("node.js engine version(s) to pin and require a minimum of"),
owner: z
.string()
.describe("GitHub organization or user the repository is underneath"),
packageData: z
.object({
dependencies: z.record(z.string(), z.string()).optional(),
devDependencies: z.record(z.string(), z.string()).optional(),
scripts: z.record(z.string(), z.string()).optional(),
})
.optional()
.describe("additional properties to include in `package.json`"),
repository: z
.string()
.describe("'kebab-case' or 'PascalCase' title of the repository"),
rulesetId: z
.string()
.optional()
.describe("GitHub branch ruleset ID for main branch protections"),
title: z.string().describe("'Title Case' title for the repository"),
usage: z
.string()
.optional()
.describe("markdown docs to put in README.md under the ## Usage heading"),
version: z
.string()
.optional()
.describe("package version to publish as and store in `package.json`"),
},
prepare({ options, take }) {
const allContributors = lazyValue(async () => {
const contributions = (await take(inputFromFileJSON, {
filePath: ".all-contributorsrc",
})) as AllContributorsData;
return contributions.contributors;
});
const documentation = lazyValue(async () => readDocumentation(take));
const nvmrc = lazyValue(
async () =>
await take(inputFromFile, {
filePath: ".nvmrc",
}),
);
const githubCliUser = lazyValue(async () => {
return swallowError(
await take(inputFromScript, {
command: "gh config get user -h github.com",
}),
)?.stdout?.toString();
});
const readme = lazyValue(async () => await readFileSafe("README.md", ""));
const rulesetId = lazyValue(async () => {
const rulesets = (await take(inputFromOctokit, {
endpoint: "GET /repos/{owner}/{repo}/rulesets",
options: {
owner: await owner(),
repo: await repository(),
},
})) as undefined | { id: string; name: string }[];
return rulesets?.find(
(ruleset) => ruleset.name === "Branch protection for main",
)?.id;
});
// TODO: Make these all use take
const gitDefaults = tryCatchLazyValueAsync(async () =>
gitUrlParse(await gitRemoteOriginUrl()),
);
const npmDefaults = tryCatchLazyValueAsync(async () => {
const whoami = (await execaCommand(`npm whoami`)).stdout;
return whoami ? await npmUser(whoami) : undefined;
});
const packageData = lazyValue(readPackageData);
const packageAuthor = lazyValue(async () =>
parsePackageAuthor(await packageData()),
);
const author = lazyValue(
async () =>
(await packageAuthor()).author ??
(await npmDefaults())?.name ??
options.owner,
);
const node = lazyValue(async () => {
const { engines } = await packageData();
return {
minimum:
(engines?.node && /[\d+.]+/.exec(engines.node))?.[0] ?? "18.3.0",
pinned: swallowError(await nvmrc())?.trim() ?? "20.18.0",
};
});
const version = lazyValue(async () => (await packageData()).version);
const owner = lazyValue(
async () =>
(await gitDefaults())?.organization ??
(await packageAuthor()).author ??
(await githubCliUser()),
);
const repository = lazyValue(
async () =>
options.repository ??
(await gitDefaults())?.name ??
(await packageData()).name ??
options.directory,
);
return {
access: "public" as const,
author,
bin: async () => (await packageData()).bin,
contributors: allContributors,
description: async () => await readDescription(packageData, readme),
documentation,
email: async () => readEmails(npmDefaults, packageAuthor),
funding: readFunding,
guide: readGuide,
login: author,
node,
owner,
packageData: async () => {
const original = await packageData();
return {
dependencies: original.dependencies,
devDependencies: original.devDependencies,
scripts: original.scripts,
};
},
repository,
rulesetId,
...readDefaultsFromReadme(readme, repository),
version,
};
},
});
export type BaseOptions = BaseOptionsFor<typeof base>;