Skip to content

Commit b2049ff

Browse files
fix: allow empty paths for updating local files (#1022)
## PR Checklist - [x] Addresses an existing open issue: fixes #1015 - [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 Brings in https://github.com/adamreisnz/replace-in-file/#allow-emptyinvalid-paths to avoid crashing.
1 parent f773371 commit b2049ff

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/steps/updateLocalFiles.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ describe("updateLocalFiles", () => {
8080
[
8181
[
8282
{
83+
"allowEmptyPaths": true,
8384
"files": [
8485
"./.github/**/*",
8586
"./*.*",
@@ -90,6 +91,7 @@ describe("updateLocalFiles", () => {
9091
],
9192
[
9293
{
94+
"allowEmptyPaths": true,
9395
"files": [
9496
"./.github/**/*",
9597
"./*.*",
@@ -100,6 +102,7 @@ describe("updateLocalFiles", () => {
100102
],
101103
[
102104
{
105+
"allowEmptyPaths": true,
103106
"files": [
104107
"./.github/**/*",
105108
"./*.*",
@@ -110,69 +113,79 @@ describe("updateLocalFiles", () => {
110113
],
111114
[
112115
{
116+
"allowEmptyPaths": true,
113117
"files": ".eslintrc.cjs",
114118
"from": /\\\\/\\\\\\*\\\\n\\.\\+\\\\\\*\\\\/\\\\n\\\\n/gs,
115119
"to": "",
116120
},
117121
],
118122
[
119123
{
124+
"allowEmptyPaths": true,
120125
"files": "./package.json",
121126
"from": /"author": "\\.\\+"/g,
122127
"to": "\\"author\\": \\"undefined\\"",
123128
},
124129
],
125130
[
126131
{
132+
"allowEmptyPaths": true,
127133
"files": "./package.json",
128134
"from": /"bin": "\\.\\+\\\\n/g,
129135
"to": "",
130136
},
131137
],
132138
[
133139
{
140+
"allowEmptyPaths": true,
134141
"files": "./package.json",
135142
"from": /"test:create": "\\.\\+\\\\n/g,
136143
"to": "",
137144
},
138145
],
139146
[
140147
{
148+
"allowEmptyPaths": true,
141149
"files": "./package.json",
142150
"from": /"test:initialize": "\\.\\*/g,
143151
"to": "",
144152
},
145153
],
146154
[
147155
{
156+
"allowEmptyPaths": true,
148157
"files": "./package.json",
149158
"from": /"initialize": "\\.\\*/g,
150159
"to": "",
151160
},
152161
],
153162
[
154163
{
164+
"allowEmptyPaths": true,
155165
"files": "./package.json",
156166
"from": /"test:migrate": "\\.\\+\\\\n/g,
157167
"to": "",
158168
},
159169
],
160170
[
161171
{
172+
"allowEmptyPaths": true,
162173
"files": "./README.md",
163174
"from": /## Getting Started\\.\\*## Development/gs,
164175
"to": "## Development",
165176
},
166177
],
167178
[
168179
{
180+
"allowEmptyPaths": true,
169181
"files": "./.github/DEVELOPMENT.md",
170182
"from": /\\\\n## Setup Scripts\\.\\*\\$/gs,
171183
"to": "",
172184
},
173185
],
174186
[
175187
{
188+
"allowEmptyPaths": true,
176189
"files": "./knip.jsonc",
177190
"from": " \\"src/initialize/index.ts\\",
178191
",
@@ -181,6 +194,7 @@ describe("updateLocalFiles", () => {
181194
],
182195
[
183196
{
197+
"allowEmptyPaths": true,
184198
"files": "./knip.jsonc",
185199
"from": " \\"src/migrate/index.ts\\",
186200
",
@@ -189,20 +203,23 @@ describe("updateLocalFiles", () => {
189203
],
190204
[
191205
{
206+
"allowEmptyPaths": true,
192207
"files": "./knip.jsonc",
193208
"from": "[\\"src/index.ts!\\", \\"script/initialize*.js\\"]",
194209
"to": "\\"src/index.ts!\\"",
195210
},
196211
],
197212
[
198213
{
214+
"allowEmptyPaths": true,
199215
"files": "./knip.jsonc",
200216
"from": "[\\"src/**/*.ts!\\", \\"script/**/*.js\\"]",
201217
"to": "\\"src/**/*.ts!\\"",
202218
},
203219
],
204220
[
205221
{
222+
"allowEmptyPaths": true,
206223
"files": "./README.md",
207224
"from": "> 💙 This package is based on [@StubOwner](https://github.com/StubOwner)'s [stub-repository](https://github.com/JoshuaKGoldberg/stub-repository).",
208225
"to": "> 💙 This package is based on [@JoshuaKGoldberg](https://github.com/JoshuaKGoldberg)'s [create-typescript-app](https://github.com/JoshuaKGoldberg/create-typescript-app).",
@@ -222,6 +239,7 @@ describe("updateLocalFiles", () => {
222239
[
223240
[
224241
{
242+
"allowEmptyPaths": true,
225243
"files": [
226244
"./.github/**/*",
227245
"./*.*",
@@ -232,6 +250,7 @@ describe("updateLocalFiles", () => {
232250
],
233251
[
234252
{
253+
"allowEmptyPaths": true,
235254
"files": [
236255
"./.github/**/*",
237256
"./*.*",
@@ -242,6 +261,7 @@ describe("updateLocalFiles", () => {
242261
],
243262
[
244263
{
264+
"allowEmptyPaths": true,
245265
"files": [
246266
"./.github/**/*",
247267
"./*.*",
@@ -252,69 +272,79 @@ describe("updateLocalFiles", () => {
252272
],
253273
[
254274
{
275+
"allowEmptyPaths": true,
255276
"files": ".eslintrc.cjs",
256277
"from": /\\\\/\\\\\\*\\\\n\\.\\+\\\\\\*\\\\/\\\\n\\\\n/gs,
257278
"to": "",
258279
},
259280
],
260281
[
261282
{
283+
"allowEmptyPaths": true,
262284
"files": "./package.json",
263285
"from": /"author": "\\.\\+"/g,
264286
"to": "\\"author\\": \\"undefined\\"",
265287
},
266288
],
267289
[
268290
{
291+
"allowEmptyPaths": true,
269292
"files": "./package.json",
270293
"from": /"bin": "\\.\\+\\\\n/g,
271294
"to": "",
272295
},
273296
],
274297
[
275298
{
299+
"allowEmptyPaths": true,
276300
"files": "./package.json",
277301
"from": /"test:create": "\\.\\+\\\\n/g,
278302
"to": "",
279303
},
280304
],
281305
[
282306
{
307+
"allowEmptyPaths": true,
283308
"files": "./package.json",
284309
"from": /"test:initialize": "\\.\\*/g,
285310
"to": "",
286311
},
287312
],
288313
[
289314
{
315+
"allowEmptyPaths": true,
290316
"files": "./package.json",
291317
"from": /"initialize": "\\.\\*/g,
292318
"to": "",
293319
},
294320
],
295321
[
296322
{
323+
"allowEmptyPaths": true,
297324
"files": "./package.json",
298325
"from": /"test:migrate": "\\.\\+\\\\n/g,
299326
"to": "",
300327
},
301328
],
302329
[
303330
{
331+
"allowEmptyPaths": true,
304332
"files": "./README.md",
305333
"from": /## Getting Started\\.\\*## Development/gs,
306334
"to": "## Development",
307335
},
308336
],
309337
[
310338
{
339+
"allowEmptyPaths": true,
311340
"files": "./.github/DEVELOPMENT.md",
312341
"from": /\\\\n## Setup Scripts\\.\\*\\$/gs,
313342
"to": "",
314343
},
315344
],
316345
[
317346
{
347+
"allowEmptyPaths": true,
318348
"files": "./knip.jsonc",
319349
"from": " \\"src/initialize/index.ts\\",
320350
",
@@ -323,6 +353,7 @@ describe("updateLocalFiles", () => {
323353
],
324354
[
325355
{
356+
"allowEmptyPaths": true,
326357
"files": "./knip.jsonc",
327358
"from": " \\"src/migrate/index.ts\\",
328359
",
@@ -331,20 +362,23 @@ describe("updateLocalFiles", () => {
331362
],
332363
[
333364
{
365+
"allowEmptyPaths": true,
334366
"files": "./knip.jsonc",
335367
"from": "[\\"src/index.ts!\\", \\"script/initialize*.js\\"]",
336368
"to": "\\"src/index.ts!\\"",
337369
},
338370
],
339371
[
340372
{
373+
"allowEmptyPaths": true,
341374
"files": "./knip.jsonc",
342375
"from": "[\\"src/**/*.ts!\\", \\"script/**/*.js\\"]",
343376
"to": "\\"src/**/*.ts!\\"",
344377
},
345378
],
346379
[
347380
{
381+
"allowEmptyPaths": true,
348382
"files": "./README.md",
349383
"from": "> 💙 This package is based on [@StubOwner](https://github.com/StubOwner)'s [stub-repository](https://github.com/JoshuaKGoldberg/stub-repository).",
350384
"to": "> 💙 This package is based on [@JoshuaKGoldberg](https://github.com/JoshuaKGoldberg)'s [create-typescript-app](https://github.com/JoshuaKGoldberg/create-typescript-app).",
@@ -361,6 +395,7 @@ describe("updateLocalFiles", () => {
361395
await updateLocalFiles({ ...options, mode: "initialize" });
362396

363397
expect(mockReplaceInFile).not.toHaveBeenCalledWith({
398+
allowEmptyPaths: true,
364399
files: ["./.github/**/*", "./*.*"],
365400
from: expect.anything(),
366401
to: options.description,
@@ -377,6 +412,7 @@ describe("updateLocalFiles", () => {
377412
await updateLocalFiles({ ...options, mode: "initialize" });
378413

379414
expect(mockReplaceInFile).toHaveBeenCalledWith({
415+
allowEmptyPaths: true,
380416
files: ["./.github/**/*", "./*.*"],
381417
from: existingDescription,
382418
to: options.description,
@@ -392,6 +428,7 @@ describe("updateLocalFiles", () => {
392428
await updateLocalFiles({ ...options, mode: "initialize" });
393429

394430
expect(mockReplaceInFile).toHaveBeenCalledWith({
431+
allowEmptyPaths: true,
395432
files: "./package.json",
396433
from: /"bin": ".+\n/g,
397434
to: "",
@@ -407,6 +444,7 @@ describe("updateLocalFiles", () => {
407444
await updateLocalFiles({ ...options, mode: "migrate" });
408445

409446
expect(mockReplaceInFile).not.toHaveBeenCalledWith({
447+
allowEmptyPaths: true,
410448
files: "./package.json",
411449
from: /"bin": ".+\n/g,
412450
to: "",
@@ -422,6 +460,7 @@ describe("updateLocalFiles", () => {
422460
await updateLocalFiles({ ...options, mode: "initialize" });
423461

424462
expect(mockReplaceInFile).toHaveBeenCalledWith({
463+
allowEmptyPaths: true,
425464
files: "./package.json",
426465
from: /"version": "1.2.3"/g,
427466
to: '"version": "0.0.0"',
@@ -437,6 +476,7 @@ describe("updateLocalFiles", () => {
437476
await updateLocalFiles({ ...options, mode: "migrate" });
438477

439478
expect(mockReplaceInFile).not.toHaveBeenCalledWith({
479+
allowEmptyPaths: true,
440480
files: "./package.json",
441481
from: /"version": "1.2.3"/g,
442482
to: '"version": "0.0.0"',

src/steps/updateLocalFiles.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ export async function updateLocalFiles(options: Options) {
5858
for (const [from, to, files = ["./.github/**/*", "./*.*"]] of replacements) {
5959
try {
6060
// @ts-expect-error -- https://github.com/microsoft/TypeScript/issues/54342
61-
await replaceInFile({ files, from, to });
61+
await replaceInFile({
62+
allowEmptyPaths: true,
63+
files,
64+
from,
65+
to,
66+
});
6267
} catch (error) {
6368
throw new Error(
6469
`Failed to replace ${from.toString()} with ${to} in ${files.toString()}`,

0 commit comments

Comments
 (0)