Skip to content

Commit b73b546

Browse files
fix: allow package.json to be empty in updateLocalFiles (#686)
## PR Checklist - [x] Addresses an existing open issue: fixes #685 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/template-typescript-node-package/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/template-typescript-node-package/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Yet another case where a non-existent (or empty) `package.json` caused issues.
1 parent c6ac4fb commit b73b546

File tree

2 files changed

+138
-4
lines changed

2 files changed

+138
-4
lines changed

src/steps/updateLocalFiles.test.ts

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,142 @@ describe("updateLocalFiles", () => {
4040
);
4141
});
4242

43-
it("replaces using the common replacements when the existing package data is empty", async () => {
43+
it("replaces using the common replacements when the existing package data is null", async () => {
44+
mockReadFileSafeAsJson.mockResolvedValue(null);
45+
mockReplaceInFile.mockResolvedValue([]);
46+
47+
await updateLocalFiles(stubOptions);
48+
49+
expect(mockReplaceInFile.mock.calls).toMatchInlineSnapshot(`
50+
[
51+
[
52+
{
53+
"files": [
54+
"./.github/**/*",
55+
"./*.*",
56+
],
57+
"from": /Template TypeScript Node Package/g,
58+
"to": "Stub Title",
59+
},
60+
],
61+
[
62+
{
63+
"files": [
64+
"./.github/**/*",
65+
"./*.*",
66+
],
67+
"from": /JoshuaKGoldberg/g,
68+
"to": "StubOwner",
69+
},
70+
],
71+
[
72+
{
73+
"files": [
74+
"./.github/**/*",
75+
"./*.*",
76+
],
77+
"from": /template-typescript-node-package/g,
78+
"to": "stub-repository",
79+
},
80+
],
81+
[
82+
{
83+
"files": ".eslintrc.cjs",
84+
"from": /\\\\/\\\\\\*\\\\n\\.\\+\\\\\\*\\\\/\\\\n\\\\n/gs,
85+
"to": "",
86+
},
87+
],
88+
[
89+
{
90+
"files": "./package.json",
91+
"from": /"author": "\\.\\+"/g,
92+
"to": "\\"author\\": \\"stub-npm-author\\"",
93+
},
94+
],
95+
[
96+
{
97+
"files": "./package.json",
98+
"from": /"bin": "\\.\\+\\\\n/g,
99+
"to": "",
100+
},
101+
],
102+
[
103+
{
104+
"files": "./package.json",
105+
"from": /"create:test": "\\.\\+\\\\n/g,
106+
"to": "",
107+
},
108+
],
109+
[
110+
{
111+
"files": "./package.json",
112+
"from": /"initialize:test": "\\.\\*/g,
113+
"to": "",
114+
},
115+
],
116+
[
117+
{
118+
"files": "./package.json",
119+
"from": /"initialize": "\\.\\*/g,
120+
"to": "",
121+
},
122+
],
123+
[
124+
{
125+
"files": "./package.json",
126+
"from": /"migrate:test": "\\.\\+\\\\n/g,
127+
"to": "",
128+
},
129+
],
130+
[
131+
{
132+
"files": "./README.md",
133+
"from": /## Getting Started\\.\\*## Development/gs,
134+
"to": "## Development",
135+
},
136+
],
137+
[
138+
{
139+
"files": "./.github/DEVELOPMENT.md",
140+
"from": /\\\\n## Setup Scripts\\.\\*\\$/gs,
141+
"to": "",
142+
},
143+
],
144+
[
145+
{
146+
"files": "./knip.jsonc",
147+
"from": " \\"src/initialize/index.ts\\",
148+
",
149+
"to": "",
150+
},
151+
],
152+
[
153+
{
154+
"files": "./knip.jsonc",
155+
"from": " \\"src/migrate/index.ts\\",
156+
",
157+
"to": "",
158+
},
159+
],
160+
[
161+
{
162+
"files": "./knip.jsonc",
163+
"from": "[\\"src/index.ts!\\", \\"script/initialize*.js\\"]",
164+
"to": "\\"src/index.ts!\\"",
165+
},
166+
],
167+
[
168+
{
169+
"files": "./knip.jsonc",
170+
"from": "[\\"src/**/*.ts!\\", \\"script/**/*.js\\"]",
171+
"to": "\\"src/**/*.ts!\\"",
172+
},
173+
],
174+
]
175+
`);
176+
});
177+
178+
it("replaces using the common replacements when the existing package data is an empty object", async () => {
44179
mockReadFileSafeAsJson.mockResolvedValue({});
45180
mockReplaceInFile.mockResolvedValue([]);
46181

src/steps/updateLocalFiles.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ export async function updateLocalFiles({
2222
repository,
2323
title,
2424
}: UpdateLocalFilesOptions) {
25-
const existingPackage = (await readFileSafeAsJson(
26-
"./package.json",
27-
)) as ExistingPackageData;
25+
const existingPackage = ((await readFileSafeAsJson("./package.json")) ??
26+
{}) as ExistingPackageData;
2827

2928
const replacements = [
3029
[/Template TypeScript Node Package/g, title],

0 commit comments

Comments
 (0)