Skip to content

Commit dcc6ad4

Browse files
fix: replace existing description with string, not regex (#861)
## PR Checklist - [x] Addresses an existing open issue: fixes #803 - [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 Applies two tangentially related fixes: * The original description can be replaced with a plain string - it doesn't need a regular expression * The rerun suggestion for descriptions should escape `$`s
1 parent d112a31 commit dcc6ad4

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

Diff for: src/steps/updateLocalFiles.test.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,37 @@ describe("updateLocalFiles", () => {
351351
`);
352352
});
353353

354+
it("does not replace an existing description when it does not exist", async () => {
355+
mockReadFileSafeAsJson.mockResolvedValue({});
356+
mockReplaceInFile.mockResolvedValue([]);
357+
358+
await updateLocalFiles(options, "initialize");
359+
360+
expect(mockReplaceInFile).not.toHaveBeenCalledWith({
361+
files: ["./.github/**/*", "./*.*"],
362+
from: expect.anything(),
363+
to: options.description,
364+
});
365+
});
366+
it("replaces an existing description when it exists", async () => {
367+
const existingDescription = "Existing description.";
368+
369+
mockReadFileSafeAsJson.mockResolvedValue({
370+
description: existingDescription,
371+
});
372+
mockReplaceInFile.mockResolvedValue([]);
373+
374+
await updateLocalFiles(options, "initialize");
375+
376+
expect(mockReplaceInFile).toHaveBeenCalledWith({
377+
files: ["./.github/**/*", "./*.*"],
378+
from: existingDescription,
379+
to: options.description,
380+
});
381+
});
382+
354383
it("removes bin when the mode is initialize", async () => {
355384
mockReadFileSafeAsJson.mockResolvedValue({
356-
description: "Existing description",
357385
version: "1.2.3",
358386
});
359387
mockReplaceInFile.mockResolvedValue([]);
@@ -369,7 +397,6 @@ describe("updateLocalFiles", () => {
369397

370398
it("does not remove bin when the mode is migrate", async () => {
371399
mockReadFileSafeAsJson.mockResolvedValue({
372-
description: "Existing description",
373400
version: "1.2.3",
374401
});
375402
mockReplaceInFile.mockResolvedValue([]);
@@ -385,7 +412,6 @@ describe("updateLocalFiles", () => {
385412

386413
it("resets package version to 0.0.0 when mode is initialize", async () => {
387414
mockReadFileSafeAsJson.mockResolvedValue({
388-
description: "Existing description",
389415
version: "1.2.3",
390416
});
391417
mockReplaceInFile.mockResolvedValue([]);
@@ -401,7 +427,6 @@ describe("updateLocalFiles", () => {
401427

402428
it("does not reset package version to 0.0.0 when mode is migrate", async () => {
403429
mockReadFileSafeAsJson.mockResolvedValue({
404-
description: "Existing description",
405430
version: "1.2.3",
406431
});
407432
mockReplaceInFile.mockResolvedValue([]);

Diff for: src/steps/updateLocalFiles.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ export async function updateLocalFiles(options: Options, mode: Mode) {
4343
];
4444

4545
if (existingPackage.description) {
46-
replacements.push([
47-
new RegExp(existingPackage.description, "g"),
48-
options.description,
49-
]);
46+
replacements.push([existingPackage.description, options.description]);
5047
}
5148

5249
if (mode === "initialize" && existingPackage.version) {

0 commit comments

Comments
 (0)