Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trim preceding ./ from package.json bin #2084

Merged
merged 2 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { z } from "zod";
import { readAccess } from "./options/readAccess.js";
import { readAllContributors } from "./options/readAllContributors.js";
import { readAuthor } from "./options/readAuthor.js";
import { readBin } from "./options/readBin.js";
import { readDescription } from "./options/readDescription.js";
import { readDocumentation } from "./options/readDocumentation.js";
import { readEmailFromCodeOfConduct } from "./options/readEmailFromCodeOfConduct.js";
Expand Down Expand Up @@ -181,7 +182,7 @@ export const base = createBase({
await readAuthor(getPackageAuthor, getNpmDefaults, options.owner),
);

const getBin = lazyValue(async () => (await getPackageData()).bin);
const getBin = lazyValue(async () => await readBin(getPackageData));

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

Expand Down
41 changes: 41 additions & 0 deletions src/options/readBin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it } from "vitest";

import { readBin } from "./readBin.js";

describe(readBin, () => {
it("resolves with undefined when package data has no bin", async () => {
const getPackageData = () => Promise.resolve({});

const actual = await readBin(getPackageData);

expect(actual).toBe(undefined);
});

it("resolves with a trimmed string when the package data has a string bin", async () => {
const getPackageData = () =>
Promise.resolve({
bin: "./index.js",
});

const actual = await readBin(getPackageData);

expect(actual).toBe("index.js");
});

it("resolves with an object of trimmed bins when the package data has a string bin", async () => {
const getPackageData = () =>
Promise.resolve({
bin: {
absolute: "index.js",
relative: "./index.js",
},
});

const actual = await readBin(getPackageData);

expect(actual).toEqual({
absolute: "index.js",
relative: "index.js",
});
});
});
17 changes: 17 additions & 0 deletions src/options/readBin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PartialPackageData } from "../types.js";
import { trimPrecedingSlash } from "../utils/trimPrecedingSlash.js";

export async function readBin(
getPackageData: () => Promise<PartialPackageData>,
) {
const { bin } = await getPackageData();

return typeof bin === "object"
? (Object.fromEntries(
Object.entries(bin).map(([key, value]) => [
key,
trimPrecedingSlash(value),
]),
) as typeof bin)
: trimPrecedingSlash(bin);
}
15 changes: 15 additions & 0 deletions src/utils/trimPrecedingSlash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, expect, test } from "vitest";

import { trimPrecedingSlash } from "./trimPrecedingSlash.js";

describe(trimPrecedingSlash, () => {
test.each([
[undefined, undefined],
["", ""],
["a", "a"],
["./a", "a"],
])("%s becomes %s", (input, expected) => {
const actual = trimPrecedingSlash(input);
expect(actual).toBe(expected);
});
});
3 changes: 3 additions & 0 deletions src/utils/trimPrecedingSlash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function trimPrecedingSlash(filePath: string | undefined) {
return filePath?.replace(/^\.\//, "");
}