Skip to content

Commit bfe0542

Browse files
authoredDec 5, 2024··
fix: assorted createOptionDefaults fixes and cleanups (#1741)
## PR Checklist - [x] Addresses an existing open issue: fixes #1740 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Brings in fixes and added unit tests from #1670. I ... don't see any actual behavioral changes. But 🤷 the code is more cleanly split and readable this way IMO. 💖
1 parent de07fad commit bfe0542

File tree

7 files changed

+63
-50
lines changed

7 files changed

+63
-50
lines changed
 

‎src/shared/options/createOptionDefaults/index.ts

+4-21
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import { tryCatchAsync } from "../../tryCatchAsync.js";
1010
import { tryCatchLazyValueAsync } from "../../tryCatchLazyValueAsync.js";
1111
import { PromptedOptions } from "../../types.js";
1212
import { parsePackageAuthor } from "./parsePackageAuthor.js";
13-
import { readDefaultsFromDevelopment } from "./readDefaultsFromDevelopment.js";
1413
import { readDefaultsFromReadme } from "./readDefaultsFromReadme.js";
15-
import { readGitHubEmail } from "./readGitHubEmail.js";
14+
import { readEmails } from "./readEmails.js";
15+
import { readGuide } from "./readGuide.js";
1616

1717
export function createOptionDefaults(promptedOptions?: PromptedOptions) {
1818
const gitDefaults = tryCatchLazyValueAsync(async () =>
@@ -34,38 +34,21 @@ export function createOptionDefaults(promptedOptions?: PromptedOptions) {
3434
(await packageAuthor()).author ?? (await npmDefaults())?.name,
3535
bin: async () => (await packageData()).bin,
3636
description: async () => (await packageData()).description,
37-
email: async () => {
38-
const githubEmail =
39-
(await readGitHubEmail()) ??
40-
(await tryCatchAsync(
41-
async () => (await $`git config --get user.email`).stdout,
42-
));
43-
const npmEmail =
44-
(await npmDefaults())?.email ?? (await packageAuthor()).email;
45-
46-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
47-
return githubEmail || npmEmail
48-
? {
49-
github: (githubEmail || npmEmail)!,
50-
npm: (npmEmail || githubEmail)!,
51-
}
52-
: undefined;
53-
/* eslint-enable @typescript-eslint/no-non-null-assertion */
54-
},
37+
email: async () => readEmails(npmDefaults, packageAuthor),
5538
funding: async () =>
5639
await tryCatchAsync(async () =>
5740
(await fs.readFile(".github/FUNDING.yml"))
5841
.toString()
5942
.split(":")[1]
6043
?.trim(),
6144
),
45+
guide: readGuide,
6246
owner: async () =>
6347
(await gitDefaults())?.organization ?? (await packageAuthor()).author,
6448
repository: async () =>
6549
promptedOptions?.repository ??
6650
(await gitDefaults())?.name ??
6751
(await packageData()).name,
68-
...readDefaultsFromDevelopment(),
6952
...readDefaultsFromReadme(),
7053
};
7154
}

‎src/shared/options/createOptionDefaults/readDefaultsFromDevelopment.ts

-22
This file was deleted.

‎src/shared/options/createOptionDefaults/readDefaultsFromReadme.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ export function readDefaultsFromReadme() {
3030
src,
3131
};
3232
},
33-
title: async () =>
34-
(/^<h1\s+align="center">(.+)<\/h1>/.exec(await readme()) ??
35-
/^# (.+)/.exec(await readme()))?.[1],
33+
title: async () => {
34+
const text = await readme();
35+
return (/^<h1\s+align="center">(.+)<\/h1>/.exec(text) ??
36+
/^# (.+)/.exec(text))?.[1];
37+
},
3638
};
3739
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { $ } from "execa";
2+
import { UserInfo } from "npm-user";
3+
4+
import { tryCatchAsync } from "../../tryCatchAsync.js";
5+
import { readGitHubEmail } from "./readGitHubEmail.js";
6+
7+
export async function readEmails(
8+
npmDefaults: () => Promise<undefined | UserInfo>,
9+
packageAuthor: () => Promise<{
10+
author: string | undefined;
11+
email: string | undefined;
12+
}>,
13+
) {
14+
const github =
15+
(await readGitHubEmail()) ??
16+
(await tryCatchAsync(
17+
async () => (await $`git config --get user.email`).stdout,
18+
));
19+
20+
const npm =
21+
((await npmDefaults())?.email ?? (await packageAuthor()).email) || github;
22+
23+
return npm ? { github: github || npm, npm } : undefined;
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import fs from "node:fs/promises";
2+
3+
import { tryCatchAsync } from "../../tryCatchAsync.js";
4+
5+
export async function readFunding() {
6+
return await tryCatchAsync(async () =>
7+
(await fs.readFile(".github/FUNDING.yml")).toString().split(":")[1]?.trim(),
8+
);
9+
}

‎src/shared/options/createOptionDefaults/readDefaultsFromDevelopment.test.ts ‎src/shared/options/createOptionDefaults/readGuide.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it, vi } from "vitest";
22

3-
import { readDefaultsFromDevelopment } from "./readDefaultsFromDevelopment.js";
3+
import { readGuide } from "./readGuide.js";
44

55
const mockReadFileSafe = vi.fn();
66

@@ -10,12 +10,12 @@ vi.mock("../../readFileSafe.js", () => ({
1010
},
1111
}));
1212

13-
describe("readDefaultsFromDevelopment", () => {
13+
describe("readGuide", () => {
1414
describe("guide", () => {
1515
it("defaults to undefined when .github/DEVELOPMENT.md cannot be found", async () => {
1616
mockReadFileSafe.mockResolvedValue("");
1717

18-
const guide = await readDefaultsFromDevelopment().guide();
18+
const guide = await readGuide();
1919

2020
expect(guide).toBeUndefined();
2121
});
@@ -27,7 +27,7 @@ describe("readDefaultsFromDevelopment", () => {
2727
> It'll walk you through the common activities you'll need to contribute.
2828
`);
2929

30-
const guide = await readDefaultsFromDevelopment().guide();
30+
const guide = await readGuide();
3131

3232
expect(guide).toEqual({
3333
href: "https://www.joshuakgoldberg.com/blog/contributing-to-a-create-typescript-app-repository",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { readFileSafe } from "../../readFileSafe.js";
2+
3+
export async function readGuide() {
4+
const development = await readFileSafe(".github/DEVELOPMENT.md", "");
5+
const tag = /> .*guided walkthrough, see \[((?!\[).+)\]\((.+)\)/i.exec(
6+
development,
7+
);
8+
9+
if (!tag) {
10+
return undefined;
11+
}
12+
13+
return {
14+
href: tag[2],
15+
title: tag[1],
16+
};
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.