Skip to content

Commit 3c073d7

Browse files
fix: trim preceding ./ from package.json bin (#2084)
## PR Checklist - [x] Addresses an existing open issue: fixes #2081 - [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 🎁
1 parent ad91eb9 commit 3c073d7

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

src/base.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { z } from "zod";
77
import { readAccess } from "./options/readAccess.js";
88
import { readAllContributors } from "./options/readAllContributors.js";
99
import { readAuthor } from "./options/readAuthor.js";
10+
import { readBin } from "./options/readBin.js";
1011
import { readDescription } from "./options/readDescription.js";
1112
import { readDocumentation } from "./options/readDocumentation.js";
1213
import { readEmailFromCodeOfConduct } from "./options/readEmailFromCodeOfConduct.js";
@@ -181,7 +182,7 @@ export const base = createBase({
181182
await readAuthor(getPackageAuthor, getNpmDefaults, options.owner),
182183
);
183184

184-
const getBin = lazyValue(async () => (await getPackageData()).bin);
185+
const getBin = lazyValue(async () => await readBin(getPackageData));
185186

186187
const getEmoji = lazyValue(async () => await readEmoji(getDescription));
187188

src/options/readBin.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { readBin } from "./readBin.js";
4+
5+
describe(readBin, () => {
6+
it("resolves with undefined when package data has no bin", async () => {
7+
const getPackageData = () => Promise.resolve({});
8+
9+
const actual = await readBin(getPackageData);
10+
11+
expect(actual).toBe(undefined);
12+
});
13+
14+
it("resolves with a trimmed string when the package data has a string bin", async () => {
15+
const getPackageData = () =>
16+
Promise.resolve({
17+
bin: "./index.js",
18+
});
19+
20+
const actual = await readBin(getPackageData);
21+
22+
expect(actual).toBe("index.js");
23+
});
24+
25+
it("resolves with an object of trimmed bins when the package data has a string bin", async () => {
26+
const getPackageData = () =>
27+
Promise.resolve({
28+
bin: {
29+
absolute: "index.js",
30+
relative: "./index.js",
31+
},
32+
});
33+
34+
const actual = await readBin(getPackageData);
35+
36+
expect(actual).toEqual({
37+
absolute: "index.js",
38+
relative: "index.js",
39+
});
40+
});
41+
});

src/options/readBin.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { PartialPackageData } from "../types.js";
2+
import { trimPrecedingSlash } from "../utils/trimPrecedingSlash.js";
3+
4+
export async function readBin(
5+
getPackageData: () => Promise<PartialPackageData>,
6+
) {
7+
const { bin } = await getPackageData();
8+
9+
return typeof bin === "object"
10+
? (Object.fromEntries(
11+
Object.entries(bin).map(([key, value]) => [
12+
key,
13+
trimPrecedingSlash(value),
14+
]),
15+
) as typeof bin)
16+
: trimPrecedingSlash(bin);
17+
}

src/utils/trimPrecedingSlash.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, expect, test } from "vitest";
2+
3+
import { trimPrecedingSlash } from "./trimPrecedingSlash.js";
4+
5+
describe(trimPrecedingSlash, () => {
6+
test.each([
7+
[undefined, undefined],
8+
["", ""],
9+
["a", "a"],
10+
["./a", "a"],
11+
])("%s becomes %s", (input, expected) => {
12+
const actual = trimPrecedingSlash(input);
13+
expect(actual).toBe(expected);
14+
});
15+
});

src/utils/trimPrecedingSlash.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function trimPrecedingSlash(filePath: string | undefined) {
2+
return filePath?.replace(/^\.\//, "");
3+
}

0 commit comments

Comments
 (0)