Skip to content

Commit 2a3b260

Browse files
authored
fix: preserve package version during migration (#846)
<!-- 👋 Hi, thanks for sending a PR to create-typescript-app! 💖. Please fill out all fields below and make sure each item is true and [x] checked. Otherwise we may not be able to review your PR. --> ## PR Checklist - [x] Addresses an existing open issue: fixes #798 - [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 Added additional config parameter to `updateLocalFiles`, so we can skip overwriting package version inside migrate script.
1 parent 4d95dbe commit 2a3b260

File tree

3 files changed

+161
-4
lines changed

3 files changed

+161
-4
lines changed

src/initialize/initializeWithOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function initializeWithOptions({
1919
[
2020
"Updating local files",
2121
async () => {
22-
await updateLocalFiles(options);
22+
await updateLocalFiles(options, { resetPackageVersion: true });
2323
},
2424
],
2525
["Updating README.md", updateReadme],

src/steps/updateLocalFiles.test.ts

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ describe("updateLocalFiles", () => {
354354
});
355355
mockReplaceInFile.mockResolvedValue([]);
356356

357-
await updateLocalFiles(options);
357+
await updateLocalFiles(options, { resetPackageVersion: true });
358358

359359
expect(mockReplaceInFile.mock.calls).toMatchInlineSnapshot(`
360360
[
@@ -508,4 +508,158 @@ describe("updateLocalFiles", () => {
508508
]
509509
`);
510510
});
511+
it("does not overwrite package version when resetPackageVersion parameter is not set", async () => {
512+
mockReadFileSafeAsJson.mockResolvedValue({
513+
description: "Existing description",
514+
version: "1.2.3",
515+
});
516+
mockReplaceInFile.mockResolvedValue([]);
517+
518+
await updateLocalFiles(options);
519+
520+
expect(mockReplaceInFile.mock.calls).toMatchInlineSnapshot(`
521+
[
522+
[
523+
{
524+
"files": [
525+
"./.github/**/*",
526+
"./*.*",
527+
],
528+
"from": /Create TypeScript App/g,
529+
"to": "Stub Title",
530+
},
531+
],
532+
[
533+
{
534+
"files": [
535+
"./.github/**/*",
536+
"./*.*",
537+
],
538+
"from": /JoshuaKGoldberg\\(\\?!\\\\/console-fail-test\\)/g,
539+
"to": "StubOwner",
540+
},
541+
],
542+
[
543+
{
544+
"files": [
545+
"./.github/**/*",
546+
"./*.*",
547+
],
548+
"from": /create-typescript-app/g,
549+
"to": "stub-repository",
550+
},
551+
],
552+
[
553+
{
554+
"files": ".eslintrc.cjs",
555+
"from": /\\\\/\\\\\\*\\\\n\\.\\+\\\\\\*\\\\/\\\\n\\\\n/gs,
556+
"to": "",
557+
},
558+
],
559+
[
560+
{
561+
"files": "./package.json",
562+
"from": /"author": "\\.\\+"/g,
563+
"to": "\\"author\\": \\"undefined\\"",
564+
},
565+
],
566+
[
567+
{
568+
"files": "./package.json",
569+
"from": /"bin": "\\.\\+\\\\n/g,
570+
"to": "",
571+
},
572+
],
573+
[
574+
{
575+
"files": "./package.json",
576+
"from": /"test:create": "\\.\\+\\\\n/g,
577+
"to": "",
578+
},
579+
],
580+
[
581+
{
582+
"files": "./package.json",
583+
"from": /"test:initialize": "\\.\\*/g,
584+
"to": "",
585+
},
586+
],
587+
[
588+
{
589+
"files": "./package.json",
590+
"from": /"initialize": "\\.\\*/g,
591+
"to": "",
592+
},
593+
],
594+
[
595+
{
596+
"files": "./package.json",
597+
"from": /"test:migrate": "\\.\\+\\\\n/g,
598+
"to": "",
599+
},
600+
],
601+
[
602+
{
603+
"files": "./README.md",
604+
"from": /## Getting Started\\.\\*## Development/gs,
605+
"to": "## Development",
606+
},
607+
],
608+
[
609+
{
610+
"files": "./.github/DEVELOPMENT.md",
611+
"from": /\\\\n## Setup Scripts\\.\\*\\$/gs,
612+
"to": "",
613+
},
614+
],
615+
[
616+
{
617+
"files": "./knip.jsonc",
618+
"from": " \\"src/initialize/index.ts\\",
619+
",
620+
"to": "",
621+
},
622+
],
623+
[
624+
{
625+
"files": "./knip.jsonc",
626+
"from": " \\"src/migrate/index.ts\\",
627+
",
628+
"to": "",
629+
},
630+
],
631+
[
632+
{
633+
"files": "./knip.jsonc",
634+
"from": "[\\"src/index.ts!\\", \\"script/initialize*.js\\"]",
635+
"to": "\\"src/index.ts!\\"",
636+
},
637+
],
638+
[
639+
{
640+
"files": "./knip.jsonc",
641+
"from": "[\\"src/**/*.ts!\\", \\"script/**/*.js\\"]",
642+
"to": "\\"src/**/*.ts!\\"",
643+
},
644+
],
645+
[
646+
{
647+
"files": "./README.md",
648+
"from": "> 💙 This package is based on [@StubOwner](https://github.com/StubOwner)'s [stub-repository](https://github.com/JoshuaKGoldberg/stub-repository).",
649+
"to": "> 💙 This package is based on [@JoshuaKGoldberg](https://github.com/JoshuaKGoldberg)'s [create-typescript-app](https://github.com/JoshuaKGoldberg/create-typescript-app).",
650+
},
651+
],
652+
[
653+
{
654+
"files": [
655+
"./.github/**/*",
656+
"./*.*",
657+
],
658+
"from": /Existing description/g,
659+
"to": "Stub description.",
660+
},
661+
],
662+
]
663+
`);
664+
});
511665
});

src/steps/updateLocalFiles.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ interface ExistingPackageData {
88
version?: string;
99
}
1010

11-
export async function updateLocalFiles(options: Options) {
11+
export async function updateLocalFiles(
12+
options: Options,
13+
config = { resetPackageVersion: false },
14+
) {
1215
const existingPackage = ((await readFileSafeAsJson("./package.json")) ??
1316
{}) as ExistingPackageData;
1417

@@ -48,7 +51,7 @@ export async function updateLocalFiles(options: Options) {
4851
]);
4952
}
5053

51-
if (existingPackage.version) {
54+
if (config.resetPackageVersion && existingPackage.version) {
5255
replacements.push([
5356
new RegExp(`"version": "${existingPackage.version}"`, "g"),
5457
`"version": "0.0.0"`,

0 commit comments

Comments
 (0)