Skip to content

Commit

Permalink
fix: default skipRestore to true when mode is create (#1793)
Browse files Browse the repository at this point in the history
## PR Checklist

- [x] Addresses an existing open issue: fixes #1085
- [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

When `mode` is `"create"`, `options.skipRestore` now has an additional
`??` to set it to `true`.

Most of the time spent here is adjusting mocks, since `readOptions`
previously always ran in that mode. Looking forward to the new `create`
engine to make this much more automatic and inferred. 😎

💖
  • Loading branch information
JoshuaKGoldberg authored Dec 21, 2024
1 parent b39d65f commit 33e235b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 26 deletions.
107 changes: 85 additions & 22 deletions src/shared/options/readOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const emptyOptions = {
skipGitHubApi: undefined,
skipInstall: undefined,
skipRemoval: undefined,
skipRestore: undefined,
skipRestore: false,
skipUninstall: undefined,
title: undefined,
};
Expand All @@ -63,7 +63,7 @@ vi.mock("../cli/spinners.ts", () => ({
},
}));

const mockReadPackageData = vi.fn();
const mockReadPackageData = vi.fn().mockResolvedValue({});

vi.mock("../packages.js", () => ({
get readPackageData() {
Expand Down Expand Up @@ -140,7 +140,7 @@ describe("readOptions", () => {
.object({ base: optionsSchemaShape.base })
.safeParse({ base: "b" });

const actual = await readOptions(["--base", "b"], "create");
const actual = await readOptions(["--base", "b"], "migrate");

expect(actual).toStrictEqual({
cancelled: true,
Expand All @@ -156,11 +156,12 @@ describe("readOptions", () => {
value: undefined,
}));

expect(await readOptions([], "create")).toStrictEqual({
expect(await readOptions([], "migrate")).toStrictEqual({
cancelled: true,
error,
options: {
...emptyOptions,
base: "minimal",
},
});
});
Expand All @@ -171,11 +172,12 @@ describe("readOptions", () => {
value: undefined,
}));

expect(await readOptions([], "create")).toStrictEqual({
expect(await readOptions([], "migrate")).toStrictEqual({
cancelled: true,
error: undefined,
options: {
...emptyOptions,
base: "minimal",
},
});
});
Expand All @@ -186,11 +188,12 @@ describe("readOptions", () => {
.mockImplementationOnce(() => ({ value: "MockOwner" }))
.mockImplementation(() => ({ value: undefined }));

expect(await readOptions([], "create")).toStrictEqual({
expect(await readOptions([], "migrate")).toStrictEqual({
cancelled: true,
error: undefined,
options: {
...emptyOptions,
base: "minimal",
owner: "MockOwner",
},
});
Expand All @@ -204,11 +207,12 @@ describe("readOptions", () => {
.mockImplementation(() => ({ value: undefined }));
mockEnsureRepositoryExists.mockResolvedValue({});

expect(await readOptions([], "create")).toStrictEqual({
expect(await readOptions([], "migrate")).toStrictEqual({
cancelled: true,
error: undefined,
options: {
...emptyOptions,
base: "minimal",
owner: "MockOwner",
repository: "MockRepository",
},
Expand All @@ -226,11 +230,12 @@ describe("readOptions", () => {
repository: mockOptions.repository,
});

expect(await readOptions([], "create")).toStrictEqual({
expect(await readOptions([], "migrate")).toStrictEqual({
cancelled: true,
error: undefined,
options: {
...emptyOptions,
base: "minimal",
owner: "MockOwner",
repository: "MockRepository",
},
Expand All @@ -249,11 +254,12 @@ describe("readOptions", () => {
repository: mockOptions.repository,
});

expect(await readOptions([], "create")).toStrictEqual({
expect(await readOptions([], "migrate")).toStrictEqual({
cancelled: true,
error: undefined,
options: {
...emptyOptions,
base: "minimal",
description: "Mock description.",
owner: "MockOwner",
repository: "MockRepository",
Expand All @@ -275,12 +281,13 @@ describe("readOptions", () => {
});

expect(
await readOptions(["--guide", "https://example.com"], "create"),
await readOptions(["--guide", "https://example.com"], "migrate"),
).toStrictEqual({
cancelled: true,
error: undefined,
options: {
...emptyOptions,
base: "minimal",
description: "Mock description.",
guide: "https://example.com",
owner: "MockOwner",
Expand All @@ -304,12 +311,13 @@ describe("readOptions", () => {
});

expect(
await readOptions(["--guide", "https://example.com"], "create"),
await readOptions(["--guide", "https://example.com"], "migrate"),
).toStrictEqual({
cancelled: true,
error: undefined,
options: {
...emptyOptions,
base: "minimal",
description: "Mock description.",
guide: "https://example.com",
owner: "MockOwner",
Expand All @@ -331,11 +339,12 @@ describe("readOptions", () => {
repository: mockOptions.repository,
});

expect(await readOptions(["--logo", "logo.svg"], "create")).toStrictEqual({
expect(await readOptions(["--logo", "logo.svg"], "migrate")).toStrictEqual({
cancelled: true,
error: undefined,
options: {
...emptyOptions,
base: "minimal",
description: "Mock description.",
logo: "logo.svg",
owner: "MockOwner",
Expand All @@ -357,11 +366,12 @@ describe("readOptions", () => {
repository: mockOptions.repository,
});

expect(await readOptions([], "create")).toStrictEqual({
expect(await readOptions([], "migrate")).toStrictEqual({
cancelled: true,
error: undefined,
options: {
...emptyOptions,
base: "minimal",
description: "Mock description.",
owner: "MockOwner",
repository: "MockRepository",
Expand All @@ -384,11 +394,12 @@ describe("readOptions", () => {
});
mockAugmentOptionsWithExcludes.mockResolvedValue(undefined);

expect(await readOptions([], "create")).toStrictEqual({
expect(await readOptions([], "migrate")).toStrictEqual({
cancelled: true,
error: undefined,
options: {
...emptyOptions,
base: "minimal",
description: "Mock description.",
owner: "MockOwner",
repository: "MockRepository",
Expand All @@ -411,7 +422,7 @@ describe("readOptions", () => {
});

expect(
await readOptions(["--base", mockOptions.base], "create"),
await readOptions(["--base", mockOptions.base], "migrate"),
).toStrictEqual({
cancelled: false,
octokit: undefined,
Expand Down Expand Up @@ -445,7 +456,7 @@ describe("readOptions", () => {
"--logo",
"logo.svg",
],
"create",
"migrate",
),
).toStrictEqual({
cancelled: false,
Expand All @@ -464,7 +475,7 @@ describe("readOptions", () => {
}));

expect(
await readOptions(["--base", mockOptions.base], "create"),
await readOptions(["--base", mockOptions.base], "migrate"),
).toStrictEqual({
cancelled: true,
options: {
Expand Down Expand Up @@ -494,7 +505,7 @@ describe("readOptions", () => {
}));

expect(
await readOptions(["--base", mockOptions.base], "create"),
await readOptions(["--base", mockOptions.base], "migrate"),
).toStrictEqual({
cancelled: false,
octokit: undefined,
Expand Down Expand Up @@ -522,7 +533,7 @@ describe("readOptions", () => {
expect(
await readOptions(
["--base", mockOptions.base, "--owner", "JoshuaKGoldberg"],
"create",
"migrate",
),
).toStrictEqual({
cancelled: false,
Expand All @@ -533,6 +544,58 @@ describe("readOptions", () => {
});
});

it("defaults skipRestore to false when the mode is not create", async () => {
mockAugmentOptionsWithExcludes.mockImplementationOnce(
(options: Partial<Options>) => ({
...options,
...mockOptions,
}),
);
mockEnsureRepositoryExists.mockResolvedValue({
octokit: undefined,
repository: mockOptions.repository,
});
mockGetPrefillOrPromptedOption.mockImplementation(() => ({
value: "mock",
}));

expect(
await readOptions(["--base", mockOptions.base], "migrate"),
).toStrictEqual({
cancelled: false,
octokit: undefined,
options: expect.objectContaining({
skipRestore: false,
}),
});
});

it("defaults skipRestore to true when the mode is create", async () => {
mockAugmentOptionsWithExcludes.mockImplementationOnce(
(options: Partial<Options>) => ({
...options,
...mockOptions,
}),
);
mockEnsureRepositoryExists.mockResolvedValue({
octokit: undefined,
repository: mockOptions.repository,
});
mockGetPrefillOrPromptedOption.mockImplementation(() => ({
value: "mock",
}));

expect(
await readOptions(["--base", mockOptions.base], "create"),
).toStrictEqual({
cancelled: false,
octokit: undefined,
options: expect.objectContaining({
skipRestore: true,
}),
});
});

it("skips API calls when --offline is true", async () => {
mockAugmentOptionsWithExcludes.mockImplementation((options: Options) => ({
...emptyOptions,
Expand All @@ -548,7 +611,7 @@ describe("readOptions", () => {
});

expect(
await readOptions(["--base", mockOptions.base, "--offline"], "create"),
await readOptions(["--base", mockOptions.base, "--offline"], "migrate"),
).toStrictEqual({
cancelled: false,
octokit: undefined,
Expand All @@ -564,7 +627,7 @@ describe("readOptions", () => {
},
guide: undefined,
logo: undefined,
mode: "create",
mode: "migrate",
offline: true,
owner: "mock",
skipAllContributorsApi: true,
Expand Down Expand Up @@ -769,7 +832,7 @@ describe("readOptions", () => {
"skipGitHubApi": true,
"skipInstall": undefined,
"skipRemoval": undefined,
"skipRestore": undefined,
"skipRestore": false,
"skipUninstall": undefined,
"title": "Test Title",
},
Expand Down
2 changes: 1 addition & 1 deletion src/shared/options/readOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export async function readOptions(
skipGitHubApi: values["skip-github-api"] ?? values.offline,
skipInstall: values["skip-install"],
skipRemoval: values["skip-removal"],
skipRestore: values["skip-restore"],
skipRestore: values["skip-restore"] ?? mode === "create",
skipUninstall: values["skip-uninstall"],
title: values.title,
};
Expand Down
8 changes: 5 additions & 3 deletions src/shared/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { readFileSafe } from "./readFileSafe.js";
import { PartialPackageData } from "./types.js";

export async function readPackageData() {
return JSON.parse(
await readFileSafe("./package.json", "{}"),
) as PartialPackageData;
return (
(JSON.parse(await readFileSafe("./package.json", "{}")) as
| PartialPackageData
| undefined) ?? {}
);
}

export async function removeDependencies(
Expand Down

0 comments on commit 33e235b

Please sign in to comment.