Skip to content

Commit 6aac53e

Browse files
fix: default author.name to git config.user (#2280)
## PR Checklist - [x] Addresses an existing open issue: fixes #2279 - [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 If `getNpmDefaults` ([`npm-user`](https://npmjs.com/package/npm-user)) isn't able to retrieve the npm name, then we can always fall back to `git config user.name`. 🎁
1 parent 67102a8 commit 6aac53e

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/base.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ export const base = createBase({
177177

178178
const getAuthor = lazyValue(
179179
async () =>
180-
await readAuthor(getPackageAuthor, getNpmDefaults, options.owner),
180+
await readAuthor(
181+
getPackageAuthor,
182+
getNpmDefaults,
183+
getGitUser,
184+
options.owner,
185+
),
181186
);
182187

183188
const getBin = lazyValue(async () => await readBin(getPackageData));
@@ -236,6 +241,11 @@ export const base = createBase({
236241

237242
const getGitDefaults = lazyValue(async () => await readGitDefaults(take));
238243

244+
const getGitUser = lazyValue(
245+
async () =>
246+
await take(inputFromScript, { command: "git config user.name" }),
247+
);
248+
239249
const getGuide = lazyValue(async () => await readGuide(take));
240250

241251
const getLogo = lazyValue(async () => await readLogo(getReadme));

src/options/readAuthor.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Result } from "execa";
12
import { describe, expect, it, vi } from "vitest";
23

34
import { readAuthor } from "./readAuthor.js";
@@ -10,6 +11,7 @@ describe(readAuthor, () => {
1011
const actual = await readAuthor(
1112
() => Promise.resolve({ name }),
1213
getNpmDefaults,
14+
() => Promise.resolve(undefined),
1315
undefined,
1416
);
1517

@@ -23,6 +25,7 @@ describe(readAuthor, () => {
2325
const actual = await readAuthor(
2426
() => Promise.resolve({}),
2527
() => Promise.resolve({ name }),
28+
() => Promise.resolve(undefined),
2629
undefined,
2730
);
2831

@@ -35,16 +38,31 @@ describe(readAuthor, () => {
3538
const actual = await readAuthor(
3639
() => Promise.resolve({}),
3740
() => Promise.resolve(undefined),
41+
() => Promise.resolve(undefined),
3842
owner,
3943
);
4044

4145
expect(actual).toBe(owner);
4246
});
4347

48+
it("returns gitUser when only it exists", async () => {
49+
const gitUser = "test-owner";
50+
51+
const actual = await readAuthor(
52+
() => Promise.resolve({}),
53+
() => Promise.resolve(undefined),
54+
() => Promise.resolve({ stdout: gitUser } as Result),
55+
undefined,
56+
);
57+
58+
expect(actual).toBe(gitUser);
59+
});
60+
4461
it("returns undefined when no sources provide a value", async () => {
4562
const actual = await readAuthor(
4663
() => Promise.resolve({}),
4764
() => Promise.resolve(undefined),
65+
() => Promise.resolve(undefined),
4866
undefined,
4967
);
5068

src/options/readAuthor.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import { ExecaError, Result } from "execa";
2+
13
import { PackageAuthor } from "./readPackageAuthor.js";
24

35
export async function readAuthor(
46
getPackageAuthor: () => Promise<PackageAuthor>,
57
getNpmDefaults: () => Promise<undefined | { name?: string }>,
8+
getGitUser: () => Promise<ExecaError | Result | undefined>,
69
owner: string | undefined,
710
) {
811
return (
9-
(await getPackageAuthor()).name ?? (await getNpmDefaults())?.name ?? owner
12+
(await getPackageAuthor()).name ??
13+
(await getNpmDefaults())?.name ??
14+
(await getGitUser())?.stdout?.toString() ??
15+
owner
1016
);
1117
}

0 commit comments

Comments
 (0)