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

feat: infer base from package json scripts during migration #993

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
55 changes: 55 additions & 0 deletions src/shared/options/getBase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, expect, it, vi } from "vitest";

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

const readPackageData = vi.hoisted(() => vi.fn());
vi.mock("../packages.js", () => ({
readPackageData,
}));

describe("getBase", () => {
it("should return minimum with minimum scripts", async () => {
readPackageData.mockImplementationOnce(() =>
Promise.resolve({
scripts: {
build: "build",
lint: "lint",
test: "test",
},
}),
);

expect(await getBase()).toBe("minimum");
});
it("should return common with common scripts", async () => {
readPackageData.mockImplementationOnce(() =>
Promise.resolve({
scripts: {
build: "build",
lint: "lint",
"lint:knip": "knip",
test: "test",
},
}),
);

expect(await getBase()).toBe("common");
});
it("should return everything with everything scripts", async () => {
readPackageData.mockImplementationOnce(() =>
Promise.resolve({
scripts: {
build: "build",
lint: "lint",
"lint:knip": "knip",
"lint:md": "md",
"lint:package-json": "package-json",
"lint:packages": "packages",
test: "test",
},
}),
);

expect(await getBase()).toBe("everything");
});
});
34 changes: 34 additions & 0 deletions src/shared/options/getBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { readPackageData } from "../packages.js";
import { OptionsBase } from "../types.js";

const commonScripts = new Set(["lint:knip", "should-semantic-release", "test"]);

const everythingScripts = new Set([
"lint:md",
"lint:package-json",
"lint:packages",
"lint:spelling",
]);
export async function getBase(): Promise<OptionsBase> {
const scripts = Object.keys((await readPackageData()).scripts ?? {});

if (
scripts.reduce(
(acc, curr) => (everythingScripts.has(curr) ? acc + 1 : acc),
0,
) >= 3
) {
return "everything";
}

if (
scripts.reduce(
(acc, curr) => (commonScripts.has(curr) ? acc + 1 : acc),
0,
) >= 2
) {
return "common";
}

return "minimum";
}
41 changes: 41 additions & 0 deletions src/shared/options/readOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ vi.mock("./createOptionDefaults/index.js", () => ({
},
}));

const readPackageData = vi.hoisted(() => vi.fn());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about vi.hoisted, very nice! https://vitest.dev/api/vi.html#vi-hoisted

Elsewhere in this repo I've used mock* as a naming convention. I think both are valid and reasonable... but the mock* is used everywhere else, so I'll go ahead and make the change.

vi.mock("../packages.js", () => ({
readPackageData,
}));

describe("readOptions", () => {
it("returns a cancellation when an arg is invalid", async () => {
const validationResult = z
Expand Down Expand Up @@ -524,4 +529,40 @@ describe("readOptions", () => {
},
});
});

it("infers base from package scripts during migration", async () => {
readPackageData.mockImplementationOnce(() =>
Promise.resolve({
scripts: {
build: "build",
lint: "lint",
test: "test",
},
}),
);
expect(await readOptions(["--offline"], "migrate")).toStrictEqual({
cancelled: false,
github: mockOptions.github,
options: {
...emptyOptions,
...mockOptions,
access: "public",
base: "minimum",
description: "mock",
directory: "mock",
email: {
github: "mock",
npm: "mock",
},
guide: undefined,
logo: undefined,
mode: "migrate",
offline: true,
owner: "mock",
skipAllContributorsApi: true,
skipGitHubApi: true,
title: "mock",
},
});
});
});
5 changes: 5 additions & 0 deletions src/shared/options/readOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { augmentOptionsWithExcludes } from "./augmentOptionsWithExcludes.js";
import { createOptionDefaults } from "./createOptionDefaults/index.js";
import { detectEmailRedundancy } from "./detectEmailRedundancy.js";
import { ensureRepositoryExists } from "./ensureRepositoryExists.js";
import { getBase } from "./getBase.js";
import { GitHub, getGitHub } from "./getGitHub.js";
import { getPrefillOrPromptedOption } from "./getPrefillOrPromptedOption.js";
import { optionsSchema } from "./optionsSchema.js";
Expand Down Expand Up @@ -44,6 +45,10 @@ export async function readOptions(
tokens: true,
});

if (mode === "migrate" && !values.base) {
values.base = await getBase();
}
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

const mappedOptions = {
access: values.access,
author: values.author,
Expand Down
1 change: 1 addition & 0 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface PartialPackageData {
email?: string;
name?: string;
repository?: { type: string; url: string } | string;
scripts?: Record<string, string>;
}

export type OptionsAccess = "public" | "restricted";
Expand Down
Loading