Skip to content

Commit

Permalink
fix: allow empty paths for updating local files
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Nov 12, 2023
1 parent f773371 commit d1af1ba
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/steps/updateLocalFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe("updateLocalFiles", () => {
[
[
{
"allowEmptyPaths": true,
"files": [
"./.github/**/*",
"./*.*",
Expand All @@ -90,6 +91,7 @@ describe("updateLocalFiles", () => {
],
[
{
"allowEmptyPaths": true,
"files": [
"./.github/**/*",
"./*.*",
Expand All @@ -100,6 +102,7 @@ describe("updateLocalFiles", () => {
],
[
{
"allowEmptyPaths": true,
"files": [
"./.github/**/*",
"./*.*",
Expand All @@ -110,69 +113,79 @@ describe("updateLocalFiles", () => {
],
[
{
"allowEmptyPaths": true,
"files": ".eslintrc.cjs",
"from": /\\\\/\\\\\\*\\\\n\\.\\+\\\\\\*\\\\/\\\\n\\\\n/gs,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"author": "\\.\\+"/g,
"to": "\\"author\\": \\"undefined\\"",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"bin": "\\.\\+\\\\n/g,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"test:create": "\\.\\+\\\\n/g,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"test:initialize": "\\.\\*/g,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"initialize": "\\.\\*/g,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"test:migrate": "\\.\\+\\\\n/g,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./README.md",
"from": /## Getting Started\\.\\*## Development/gs,
"to": "## Development",
},
],
[
{
"allowEmptyPaths": true,
"files": "./.github/DEVELOPMENT.md",
"from": /\\\\n## Setup Scripts\\.\\*\\$/gs,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./knip.jsonc",
"from": " \\"src/initialize/index.ts\\",
",
Expand All @@ -181,6 +194,7 @@ describe("updateLocalFiles", () => {
],
[
{
"allowEmptyPaths": true,
"files": "./knip.jsonc",
"from": " \\"src/migrate/index.ts\\",
",
Expand All @@ -189,20 +203,23 @@ describe("updateLocalFiles", () => {
],
[
{
"allowEmptyPaths": true,
"files": "./knip.jsonc",
"from": "[\\"src/index.ts!\\", \\"script/initialize*.js\\"]",
"to": "\\"src/index.ts!\\"",
},
],
[
{
"allowEmptyPaths": true,
"files": "./knip.jsonc",
"from": "[\\"src/**/*.ts!\\", \\"script/**/*.js\\"]",
"to": "\\"src/**/*.ts!\\"",
},
],
[
{
"allowEmptyPaths": true,
"files": "./README.md",
"from": "> 💙 This package is based on [@StubOwner](https://github.com/StubOwner)'s [stub-repository](https://github.com/JoshuaKGoldberg/stub-repository).",
"to": "> 💙 This package is based on [@JoshuaKGoldberg](https://github.com/JoshuaKGoldberg)'s [create-typescript-app](https://github.com/JoshuaKGoldberg/create-typescript-app).",
Expand All @@ -222,6 +239,7 @@ describe("updateLocalFiles", () => {
[
[
{
"allowEmptyPaths": true,
"files": [
"./.github/**/*",
"./*.*",
Expand All @@ -232,6 +250,7 @@ describe("updateLocalFiles", () => {
],
[
{
"allowEmptyPaths": true,
"files": [
"./.github/**/*",
"./*.*",
Expand All @@ -242,6 +261,7 @@ describe("updateLocalFiles", () => {
],
[
{
"allowEmptyPaths": true,
"files": [
"./.github/**/*",
"./*.*",
Expand All @@ -252,69 +272,79 @@ describe("updateLocalFiles", () => {
],
[
{
"allowEmptyPaths": true,
"files": ".eslintrc.cjs",
"from": /\\\\/\\\\\\*\\\\n\\.\\+\\\\\\*\\\\/\\\\n\\\\n/gs,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"author": "\\.\\+"/g,
"to": "\\"author\\": \\"undefined\\"",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"bin": "\\.\\+\\\\n/g,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"test:create": "\\.\\+\\\\n/g,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"test:initialize": "\\.\\*/g,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"initialize": "\\.\\*/g,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"test:migrate": "\\.\\+\\\\n/g,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./README.md",
"from": /## Getting Started\\.\\*## Development/gs,
"to": "## Development",
},
],
[
{
"allowEmptyPaths": true,
"files": "./.github/DEVELOPMENT.md",
"from": /\\\\n## Setup Scripts\\.\\*\\$/gs,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./knip.jsonc",
"from": " \\"src/initialize/index.ts\\",
",
Expand All @@ -323,6 +353,7 @@ describe("updateLocalFiles", () => {
],
[
{
"allowEmptyPaths": true,
"files": "./knip.jsonc",
"from": " \\"src/migrate/index.ts\\",
",
Expand All @@ -331,20 +362,23 @@ describe("updateLocalFiles", () => {
],
[
{
"allowEmptyPaths": true,
"files": "./knip.jsonc",
"from": "[\\"src/index.ts!\\", \\"script/initialize*.js\\"]",
"to": "\\"src/index.ts!\\"",
},
],
[
{
"allowEmptyPaths": true,
"files": "./knip.jsonc",
"from": "[\\"src/**/*.ts!\\", \\"script/**/*.js\\"]",
"to": "\\"src/**/*.ts!\\"",
},
],
[
{
"allowEmptyPaths": true,
"files": "./README.md",
"from": "> 💙 This package is based on [@StubOwner](https://github.com/StubOwner)'s [stub-repository](https://github.com/JoshuaKGoldberg/stub-repository).",
"to": "> 💙 This package is based on [@JoshuaKGoldberg](https://github.com/JoshuaKGoldberg)'s [create-typescript-app](https://github.com/JoshuaKGoldberg/create-typescript-app).",
Expand All @@ -361,6 +395,7 @@ describe("updateLocalFiles", () => {
await updateLocalFiles({ ...options, mode: "initialize" });

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

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

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

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

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

expect(mockReplaceInFile).not.toHaveBeenCalledWith({
allowEmptyPaths: true,
files: "./package.json",
from: /"version": "1.2.3"/g,
to: '"version": "0.0.0"',
Expand Down
7 changes: 6 additions & 1 deletion src/steps/updateLocalFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ export async function updateLocalFiles(options: Options) {
for (const [from, to, files = ["./.github/**/*", "./*.*"]] of replacements) {
try {
// @ts-expect-error -- https://github.com/microsoft/TypeScript/issues/54342
await replaceInFile({ files, from, to });
await replaceInFile({
allowEmptyPaths: true,
files,
from,
to,
});
} catch (error) {
throw new Error(
`Failed to replace ${from.toString()} with ${to} in ${files.toString()}`,
Expand Down

0 comments on commit d1af1ba

Please sign in to comment.