Skip to content

Commit 33e235b

Browse files
fix: default skipRestore to true when mode is create (#1793)
## 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. 😎 💖
1 parent b39d65f commit 33e235b

File tree

3 files changed

+91
-26
lines changed

3 files changed

+91
-26
lines changed

src/shared/options/readOptions.test.ts

+85-22
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const emptyOptions = {
4646
skipGitHubApi: undefined,
4747
skipInstall: undefined,
4848
skipRemoval: undefined,
49-
skipRestore: undefined,
49+
skipRestore: false,
5050
skipUninstall: undefined,
5151
title: undefined,
5252
};
@@ -63,7 +63,7 @@ vi.mock("../cli/spinners.ts", () => ({
6363
},
6464
}));
6565

66-
const mockReadPackageData = vi.fn();
66+
const mockReadPackageData = vi.fn().mockResolvedValue({});
6767

6868
vi.mock("../packages.js", () => ({
6969
get readPackageData() {
@@ -140,7 +140,7 @@ describe("readOptions", () => {
140140
.object({ base: optionsSchemaShape.base })
141141
.safeParse({ base: "b" });
142142

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

145145
expect(actual).toStrictEqual({
146146
cancelled: true,
@@ -156,11 +156,12 @@ describe("readOptions", () => {
156156
value: undefined,
157157
}));
158158

159-
expect(await readOptions([], "create")).toStrictEqual({
159+
expect(await readOptions([], "migrate")).toStrictEqual({
160160
cancelled: true,
161161
error,
162162
options: {
163163
...emptyOptions,
164+
base: "minimal",
164165
},
165166
});
166167
});
@@ -171,11 +172,12 @@ describe("readOptions", () => {
171172
value: undefined,
172173
}));
173174

174-
expect(await readOptions([], "create")).toStrictEqual({
175+
expect(await readOptions([], "migrate")).toStrictEqual({
175176
cancelled: true,
176177
error: undefined,
177178
options: {
178179
...emptyOptions,
180+
base: "minimal",
179181
},
180182
});
181183
});
@@ -186,11 +188,12 @@ describe("readOptions", () => {
186188
.mockImplementationOnce(() => ({ value: "MockOwner" }))
187189
.mockImplementation(() => ({ value: undefined }));
188190

189-
expect(await readOptions([], "create")).toStrictEqual({
191+
expect(await readOptions([], "migrate")).toStrictEqual({
190192
cancelled: true,
191193
error: undefined,
192194
options: {
193195
...emptyOptions,
196+
base: "minimal",
194197
owner: "MockOwner",
195198
},
196199
});
@@ -204,11 +207,12 @@ describe("readOptions", () => {
204207
.mockImplementation(() => ({ value: undefined }));
205208
mockEnsureRepositoryExists.mockResolvedValue({});
206209

207-
expect(await readOptions([], "create")).toStrictEqual({
210+
expect(await readOptions([], "migrate")).toStrictEqual({
208211
cancelled: true,
209212
error: undefined,
210213
options: {
211214
...emptyOptions,
215+
base: "minimal",
212216
owner: "MockOwner",
213217
repository: "MockRepository",
214218
},
@@ -226,11 +230,12 @@ describe("readOptions", () => {
226230
repository: mockOptions.repository,
227231
});
228232

229-
expect(await readOptions([], "create")).toStrictEqual({
233+
expect(await readOptions([], "migrate")).toStrictEqual({
230234
cancelled: true,
231235
error: undefined,
232236
options: {
233237
...emptyOptions,
238+
base: "minimal",
234239
owner: "MockOwner",
235240
repository: "MockRepository",
236241
},
@@ -249,11 +254,12 @@ describe("readOptions", () => {
249254
repository: mockOptions.repository,
250255
});
251256

252-
expect(await readOptions([], "create")).toStrictEqual({
257+
expect(await readOptions([], "migrate")).toStrictEqual({
253258
cancelled: true,
254259
error: undefined,
255260
options: {
256261
...emptyOptions,
262+
base: "minimal",
257263
description: "Mock description.",
258264
owner: "MockOwner",
259265
repository: "MockRepository",
@@ -275,12 +281,13 @@ describe("readOptions", () => {
275281
});
276282

277283
expect(
278-
await readOptions(["--guide", "https://example.com"], "create"),
284+
await readOptions(["--guide", "https://example.com"], "migrate"),
279285
).toStrictEqual({
280286
cancelled: true,
281287
error: undefined,
282288
options: {
283289
...emptyOptions,
290+
base: "minimal",
284291
description: "Mock description.",
285292
guide: "https://example.com",
286293
owner: "MockOwner",
@@ -304,12 +311,13 @@ describe("readOptions", () => {
304311
});
305312

306313
expect(
307-
await readOptions(["--guide", "https://example.com"], "create"),
314+
await readOptions(["--guide", "https://example.com"], "migrate"),
308315
).toStrictEqual({
309316
cancelled: true,
310317
error: undefined,
311318
options: {
312319
...emptyOptions,
320+
base: "minimal",
313321
description: "Mock description.",
314322
guide: "https://example.com",
315323
owner: "MockOwner",
@@ -331,11 +339,12 @@ describe("readOptions", () => {
331339
repository: mockOptions.repository,
332340
});
333341

334-
expect(await readOptions(["--logo", "logo.svg"], "create")).toStrictEqual({
342+
expect(await readOptions(["--logo", "logo.svg"], "migrate")).toStrictEqual({
335343
cancelled: true,
336344
error: undefined,
337345
options: {
338346
...emptyOptions,
347+
base: "minimal",
339348
description: "Mock description.",
340349
logo: "logo.svg",
341350
owner: "MockOwner",
@@ -357,11 +366,12 @@ describe("readOptions", () => {
357366
repository: mockOptions.repository,
358367
});
359368

360-
expect(await readOptions([], "create")).toStrictEqual({
369+
expect(await readOptions([], "migrate")).toStrictEqual({
361370
cancelled: true,
362371
error: undefined,
363372
options: {
364373
...emptyOptions,
374+
base: "minimal",
365375
description: "Mock description.",
366376
owner: "MockOwner",
367377
repository: "MockRepository",
@@ -384,11 +394,12 @@ describe("readOptions", () => {
384394
});
385395
mockAugmentOptionsWithExcludes.mockResolvedValue(undefined);
386396

387-
expect(await readOptions([], "create")).toStrictEqual({
397+
expect(await readOptions([], "migrate")).toStrictEqual({
388398
cancelled: true,
389399
error: undefined,
390400
options: {
391401
...emptyOptions,
402+
base: "minimal",
392403
description: "Mock description.",
393404
owner: "MockOwner",
394405
repository: "MockRepository",
@@ -411,7 +422,7 @@ describe("readOptions", () => {
411422
});
412423

413424
expect(
414-
await readOptions(["--base", mockOptions.base], "create"),
425+
await readOptions(["--base", mockOptions.base], "migrate"),
415426
).toStrictEqual({
416427
cancelled: false,
417428
octokit: undefined,
@@ -445,7 +456,7 @@ describe("readOptions", () => {
445456
"--logo",
446457
"logo.svg",
447458
],
448-
"create",
459+
"migrate",
449460
),
450461
).toStrictEqual({
451462
cancelled: false,
@@ -464,7 +475,7 @@ describe("readOptions", () => {
464475
}));
465476

466477
expect(
467-
await readOptions(["--base", mockOptions.base], "create"),
478+
await readOptions(["--base", mockOptions.base], "migrate"),
468479
).toStrictEqual({
469480
cancelled: true,
470481
options: {
@@ -494,7 +505,7 @@ describe("readOptions", () => {
494505
}));
495506

496507
expect(
497-
await readOptions(["--base", mockOptions.base], "create"),
508+
await readOptions(["--base", mockOptions.base], "migrate"),
498509
).toStrictEqual({
499510
cancelled: false,
500511
octokit: undefined,
@@ -522,7 +533,7 @@ describe("readOptions", () => {
522533
expect(
523534
await readOptions(
524535
["--base", mockOptions.base, "--owner", "JoshuaKGoldberg"],
525-
"create",
536+
"migrate",
526537
),
527538
).toStrictEqual({
528539
cancelled: false,
@@ -533,6 +544,58 @@ describe("readOptions", () => {
533544
});
534545
});
535546

547+
it("defaults skipRestore to false when the mode is not create", async () => {
548+
mockAugmentOptionsWithExcludes.mockImplementationOnce(
549+
(options: Partial<Options>) => ({
550+
...options,
551+
...mockOptions,
552+
}),
553+
);
554+
mockEnsureRepositoryExists.mockResolvedValue({
555+
octokit: undefined,
556+
repository: mockOptions.repository,
557+
});
558+
mockGetPrefillOrPromptedOption.mockImplementation(() => ({
559+
value: "mock",
560+
}));
561+
562+
expect(
563+
await readOptions(["--base", mockOptions.base], "migrate"),
564+
).toStrictEqual({
565+
cancelled: false,
566+
octokit: undefined,
567+
options: expect.objectContaining({
568+
skipRestore: false,
569+
}),
570+
});
571+
});
572+
573+
it("defaults skipRestore to true when the mode is create", async () => {
574+
mockAugmentOptionsWithExcludes.mockImplementationOnce(
575+
(options: Partial<Options>) => ({
576+
...options,
577+
...mockOptions,
578+
}),
579+
);
580+
mockEnsureRepositoryExists.mockResolvedValue({
581+
octokit: undefined,
582+
repository: mockOptions.repository,
583+
});
584+
mockGetPrefillOrPromptedOption.mockImplementation(() => ({
585+
value: "mock",
586+
}));
587+
588+
expect(
589+
await readOptions(["--base", mockOptions.base], "create"),
590+
).toStrictEqual({
591+
cancelled: false,
592+
octokit: undefined,
593+
options: expect.objectContaining({
594+
skipRestore: true,
595+
}),
596+
});
597+
});
598+
536599
it("skips API calls when --offline is true", async () => {
537600
mockAugmentOptionsWithExcludes.mockImplementation((options: Options) => ({
538601
...emptyOptions,
@@ -548,7 +611,7 @@ describe("readOptions", () => {
548611
});
549612

550613
expect(
551-
await readOptions(["--base", mockOptions.base, "--offline"], "create"),
614+
await readOptions(["--base", mockOptions.base, "--offline"], "migrate"),
552615
).toStrictEqual({
553616
cancelled: false,
554617
octokit: undefined,
@@ -564,7 +627,7 @@ describe("readOptions", () => {
564627
},
565628
guide: undefined,
566629
logo: undefined,
567-
mode: "create",
630+
mode: "migrate",
568631
offline: true,
569632
owner: "mock",
570633
skipAllContributorsApi: true,
@@ -769,7 +832,7 @@ describe("readOptions", () => {
769832
"skipGitHubApi": true,
770833
"skipInstall": undefined,
771834
"skipRemoval": undefined,
772-
"skipRestore": undefined,
835+
"skipRestore": false,
773836
"skipUninstall": undefined,
774837
"title": "Test Title",
775838
},

src/shared/options/readOptions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export async function readOptions(
9999
skipGitHubApi: values["skip-github-api"] ?? values.offline,
100100
skipInstall: values["skip-install"],
101101
skipRemoval: values["skip-removal"],
102-
skipRestore: values["skip-restore"],
102+
skipRestore: values["skip-restore"] ?? mode === "create",
103103
skipUninstall: values["skip-uninstall"],
104104
title: values.title,
105105
};

src/shared/packages.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { readFileSafe } from "./readFileSafe.js";
44
import { PartialPackageData } from "./types.js";
55

66
export async function readPackageData() {
7-
return JSON.parse(
8-
await readFileSafe("./package.json", "{}"),
9-
) as PartialPackageData;
7+
return (
8+
(JSON.parse(await readFileSafe("./package.json", "{}")) as
9+
| PartialPackageData
10+
| undefined) ?? {}
11+
);
1012
}
1113

1214
export async function removeDependencies(

0 commit comments

Comments
 (0)