-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathbase.ts
231 lines (214 loc) · 6.57 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
import { BaseOptionsFor, createBase } from "create";
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 { parsePackageAuthor } from "../shared/options/createOptionDefaults/parsePackageAuthor.js";
import { readDefaultsFromReadme } from "../shared/options/createOptionDefaults/readDefaultsFromReadme.js";
import { readEmails } from "../shared/options/createOptionDefaults/readEmails.js";
import { readFunding } from "../shared/options/createOptionDefaults/readFunding.js";
import { readGuide } from "../shared/options/createOptionDefaults/readGuide.js";
import { readPackageData } from "../shared/packages.js";
import { tryCatchLazyValueAsync } from "../shared/tryCatchLazyValueAsync.js";
import { AllContributorsData } from "../shared/types.js";
import { readDescription } from "./readDescription.js";
import { readDocumentation } from "./readDocumentation.js";
import { swallowError } from "./utils/swallowError.js";
export const base = createBase({
options: {
access: z
.union([z.literal("public"), z.literal("restricted")])
.optional()
.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(
z.object({
avatar_url: z.string(),
contributions: z.array(z.string()),
login: z.string(),
name: z.string(),
profile: z.string(),
}),
)
.optional()
.describe("AllContributors contributors to store in .all-contributorsrc"),
description: z
.string()
.describe("sentence case description of the repository"),
directory: z.string(),
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",
),
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",
),
hideTemplatedBy: z
.boolean()
.optional()
.describe(
"whether to hide the 'created by ...' notice at the bottom of the README.md",
),
keywords: z
.array(z.string())
.optional()
.describe("any number of keywords to include in `package.json`"),
logo: z
.object({
alt: z.string(),
src: z.string(),
})
.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("'Sentence case.' description of the repository"),
title: z.string().describe("'Title Case' title for the repository"),
version: z
.string()
.optional()
.describe("package version to publish as and store in `package.json`"),
},
produce({ 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();
});
// 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
)?.toLowerCase(),
);
const node = lazyValue(async () => {
const { engines } = await packageData();
return {
minimum:
(engines?.node && /[\d+.]+/.exec(engines.node))?.[0] ?? "18.3.0",
pinned: swallowError(await nvmrc()) ?? "20.18.0",
};
});
const version = lazyValue(async () => (await packageData()).version);
return {
author,
bin: async () => (await packageData()).bin,
contributors: allContributors,
description: async () => await readDescription(packageData),
documentation,
email: async () => readEmails(npmDefaults, packageAuthor),
funding: readFunding,
guide: readGuide,
login: author,
node,
owner: async () =>
(await gitDefaults())?.organization ??
(await packageAuthor()).author ??
(await githubCliUser()),
packageData: async () => {
const original = await packageData();
return {
dependencies: original.dependencies,
devDependencies: original.devDependencies,
scripts: original.scripts,
};
},
repository: async () =>
options.repository ??
(await gitDefaults())?.name ??
(await packageData()).name,
...readDefaultsFromReadme(),
version,
};
},
template: {
owner: "JoshuaKGoldberg",
repository: "create-typescript-app",
},
});
export type BaseOptions = BaseOptionsFor<typeof base>;