Skip to content

Commit 67612d7

Browse files
fix: only append README.md notice if it doesn't yet exist (#845)
## PR Checklist - [x] Addresses an existing open issue: fixes #843 - [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 Uses the `<!-- ... -->` line as a detector to know whether to append. Note that _creation_ was fixed previously, just not _appending_ (updating).
1 parent 114e818 commit 67612d7

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/steps/updateReadme.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
3+
import { updateReadme } from "./updateReadme.js";
4+
5+
const mockAppendFile = vi.fn();
6+
7+
vi.mock("node:fs/promises", () => ({
8+
default: {
9+
get appendFile() {
10+
return mockAppendFile;
11+
},
12+
},
13+
}));
14+
15+
const mockReadFileSafe = vi.fn();
16+
17+
vi.mock("../shared/readFileSafe.js", () => ({
18+
get readFileSafe() {
19+
return mockReadFileSafe;
20+
},
21+
}));
22+
23+
describe("updateReadme", () => {
24+
it("adds a notice when the file does not contain it already", async () => {
25+
mockReadFileSafe.mockResolvedValue("");
26+
27+
await updateReadme();
28+
29+
expect(mockAppendFile.mock.calls).toMatchInlineSnapshot(`
30+
[
31+
[
32+
"./README.md",
33+
"
34+
<!-- You can remove this notice if you don't want it 🙂 no worries! -->
35+
36+
> 💙 This package is based on [@JoshuaKGoldberg](https://github.com/JoshuaKGoldberg)'s [create-typescript-app](https://github.com/JoshuaKGoldberg/create-typescript-app).
37+
",
38+
],
39+
]
40+
`);
41+
});
42+
43+
it("doesn't adds a notice when the file contains it already", async () => {
44+
mockReadFileSafe.mockResolvedValue(
45+
"<!-- You can remove this notice if you don't want it 🙂 no worries! -->",
46+
);
47+
48+
await updateReadme();
49+
50+
expect(mockAppendFile.mock.calls).toMatchInlineSnapshot("[]");
51+
});
52+
});

src/steps/updateReadme.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import fs from "node:fs/promises";
22
import { EOL } from "node:os";
33

4+
import { readFileSafe } from "../shared/readFileSafe.js";
5+
6+
const detectionLine = `<!-- You can remove this notice if you don't want it 🙂 no worries! -->`;
7+
48
export const endOfReadmeNotice = [
59
``,
6-
`<!-- You can remove this notice if you don't want it 🙂 no worries! -->`,
10+
detectionLine,
711
``,
812
`> 💙 This package is based on [@JoshuaKGoldberg](https://github.com/JoshuaKGoldberg)'s [create-typescript-app](https://github.com/JoshuaKGoldberg/create-typescript-app).`,
913
``,
1014
].join(EOL);
1115

1216
export async function updateReadme() {
13-
await fs.appendFile("./README.md", endOfReadmeNotice);
17+
const contents = await readFileSafe("./README.md", "");
18+
19+
if (!contents.includes(detectionLine)) {
20+
await fs.appendFile("./README.md", endOfReadmeNotice);
21+
}
1422
}

0 commit comments

Comments
 (0)