From d68b6afa47f154a1ddc531dc23ab4af688d96b80 Mon Sep 17 00:00:00 2001 From: David Sheldrick Date: Fri, 14 Jul 2023 15:51:11 +0100 Subject: [PATCH] fix tests --- .../__snapshots__/append-patches.test.ts.snap | 5 ++ .../append-patches/append-patches.sh | 8 +- .../apply-multiple-patches.test.ts.snap | 83 ++++++++++++------- .../apply-multiple-patches.sh | 8 +- .../broken-patch-file.test.ts.snap | 2 + .../broken-patch-file/broken-patch-file.sh | 4 +- .../__snapshots__/collate-errors.test.ts.snap | 6 +- .../collate-errors/collate-errors.sh | 4 +- .../dev-only-patches.test.ts.snap | 20 +++-- .../dev-only-patches/dev-only-patches.sh | 4 +- .../__snapshots__/error-on-fail.test.ts.snap | 2 + .../error-on-fail/error-on-fail.sh | 4 +- .../__snapshots__/error-on-warn.test.ts.snap | 3 + .../error-on-warn/error-on-warn.sh | 4 +- .../fails-when-no-package.test.ts.snap | 2 + .../fails-when-no-package.sh | 4 +- .../ignore-whitespace.test.ts.snap | 20 +++-- .../ignore-whitespace/ignore-whitespace.sh | 4 +- ...res-scripts-when-making-patch.test.ts.snap | 6 +- .../ignores-scripts-when-making-patch.sh | 4 +- integration-tests/newIntegrationTest.ts | 4 +- .../no-symbolic-links.test.ts.snap | 4 + .../no-symbolic-links/no-symbolic-links.sh | 4 +- .../package-gets-updated.test.ts.snap | 44 ++++++++-- .../package-gets-updated.sh | 8 +- .../patch-parse-failure.test.ts.snap | 2 + .../patch-parse-failure.sh | 4 +- .../__snapshots__/rebase-insert.test.ts.snap | 1 + .../reverse-multiple-patches.test.ts.snap | 15 ++++ integration-tests/runIntegrationTest.ts | 2 +- ...pected-patch-creation-failure.test.ts.snap | 4 + .../unexpected-patch-creation-failure.sh | 4 +- property-based-tests/testCases.ts | 2 +- src/applyPatches.ts | 21 +++-- src/createIssue.ts | 2 +- src/detectPackageManager.ts | 4 +- src/filterFiles.ts | 6 +- src/getPackageResolution.ts | 6 +- src/index.ts | 4 +- src/makePatch.ts | 41 ++++----- src/makeRegExp.ts | 2 +- src/patch/apply.ts | 2 +- src/patch/read.test.ts | 20 +++-- src/patch/read.ts | 2 +- src/rebase.ts | 32 +++---- src/spawnSafe.ts | 70 ++++++++-------- src/stateFile.ts | 2 +- 47 files changed, 296 insertions(+), 213 deletions(-) diff --git a/integration-tests/append-patches/__snapshots__/append-patches.test.ts.snap b/integration-tests/append-patches/__snapshots__/append-patches.test.ts.snap index e6a36be8..a91095be 100644 --- a/integration-tests/append-patches/__snapshots__/append-patches.test.ts.snap +++ b/integration-tests/append-patches/__snapshots__/append-patches.test.ts.snap @@ -82,12 +82,17 @@ END SNAPSHOT" exports[`Test append-patches: 07: patch-package fails when a patch in the sequence is invalid 1`] = ` "SNAPSHOT: patch-package fails when a patch in the sequence is invalid +patch-package 0.0.0 +• Creating temporary folder +• Installing left-pad@1.3.0 with npm +• Diffing your files with clean files Failed to apply patch left-pad+1.3.0+001+FirstPatch.patch to left-pad END SNAPSHOT" `; exports[`Test append-patches: 08: --append is not compatible with --create-issue 1`] = ` "SNAPSHOT: --append is not compatible with --create-issue +patch-package 0.0.0 --create-issue is not compatible with --append. END SNAPSHOT" `; diff --git a/integration-tests/append-patches/append-patches.sh b/integration-tests/append-patches/append-patches.sh index 6f35a5a5..cefbe9f6 100755 --- a/integration-tests/append-patches/append-patches.sh +++ b/integration-tests/append-patches/append-patches.sh @@ -71,14 +71,14 @@ echo "END SNAPSHOT" echo "if one of the patches in the sequence is invalid, the sequence is not applied" npx replace 'use strict' 'use bananas' patches/*FirstPatch.patch -(>&2 echo "SNAPSHOT: patch-package fails when a patch in the sequence is invalid") +echo "SNAPSHOT: patch-package fails when a patch in the sequence is invalid" if patch-package left-pad --append 'Bananas' ; then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" -(>&2 echo "SNAPSHOT: --append is not compatible with --create-issue") +echo "SNAPSHOT: --append is not compatible with --create-issue" if patch-package left-pad --append 'Bananas' --create-issue ; then exit 1 fi -(>&2 echo "END SNAPSHOT") \ No newline at end of file +echo "END SNAPSHOT" \ No newline at end of file diff --git a/integration-tests/apply-multiple-patches/__snapshots__/apply-multiple-patches.test.ts.snap b/integration-tests/apply-multiple-patches/__snapshots__/apply-multiple-patches.test.ts.snap index 118f1eb8..c4fd0525 100644 --- a/integration-tests/apply-multiple-patches/__snapshots__/apply-multiple-patches.test.ts.snap +++ b/integration-tests/apply-multiple-patches/__snapshots__/apply-multiple-patches.test.ts.snap @@ -13,6 +13,7 @@ END SNAPSHOT" exports[`Test apply-multiple-patches: 01: patch-package stores a state file to list the patches that have been applied 1`] = ` "SNAPSHOT: patch-package stores a state file to list the patches that have been applied { + \\"isRebasing\\": false, \\"patches\\": [ { \\"didApply\\": true, @@ -30,62 +31,80 @@ exports[`Test apply-multiple-patches: 01: patch-package stores a state file to l \\"patchFilename\\": \\"left-pad+1.3.0+004+goodbye.patch\\" } ], - \\"version\\": 0 + \\"version\\": 1 }END SNAPSHOT" `; -exports[`Test apply-multiple-patches: 02: patch-package only applies the first patch if the second of three is invalid 1`] = ` +exports[`Test apply-multiple-patches: 02: patch-package fails when a patch in the sequence is invalid 1`] = ` +"SNAPSHOT: patch-package fails when a patch in the sequence is invalid +patch-package 0.0.0 +Applying patches... +left-pad@1.3.0 (1 hello) ✔ + +⛔ ERROR + +Failed to apply patch file left-pad+1.3.0+002+broken.patch. + +If this patch file is no longer useful, delete it and run + + patch-package + +Otherwise you should open node_modules/left-pad, manually apply the changes from the patch file, and run + + patch-package left-pad + +to update the patch file. + +END SNAPSHOT" +`; + +exports[`Test apply-multiple-patches: 03: patch-package only applies the first patch if the second of three is invalid 1`] = ` "SNAPSHOT: patch-package only applies the first patch if the second of three is invalid patch-package 0.0.0 Applying patches... left-pad@1.3.0 (1 hello) ✔ + +⛔ ERROR + +Failed to apply patch file left-pad+1.3.0+002+broken.patch. + +If this patch file is no longer useful, delete it and run + + patch-package + +Otherwise you should open node_modules/left-pad, manually apply the changes from the patch file, and run + + patch-package left-pad + +to update the patch file. + END SNAPSHOT" `; -exports[`Test apply-multiple-patches: 03: patch-package stores a state file of only the first patch if there was an error 1`] = ` +exports[`Test apply-multiple-patches: 04: patch-package stores a state file of only the first patch if there was an error 1`] = ` "SNAPSHOT: patch-package stores a state file of only the first patch if there was an error { + \\"isRebasing\\": true, \\"patches\\": [ { \\"didApply\\": true, \\"patchContentHash\\": \\"404c604ed830db6a0605f86cb9165ced136848f70986b23bf877bcf38968c1c9\\", \\"patchFilename\\": \\"left-pad+1.3.0+001+hello.patch\\" + }, + { + \\"didApply\\": false, + \\"patchContentHash\\": \\"9c5c141e2578b4178fd57dd7726488c2f7eab32e23a7848701da29dcb371b9f2\\", + \\"patchFilename\\": \\"left-pad+1.3.0+002+broken.patch\\" } ], - \\"version\\": 0 + \\"version\\": 1 }END SNAPSHOT" `; -exports[`Test apply-multiple-patches: 04: patch-package fails when a patch in the sequence is invalid 1`] = ` -"SNAPSHOT: patch-package fails when a patch in the sequence is invalid - -**ERROR** Failed to apply patch for package left-pad at path - - node_modules/left-pad - - This error was caused because patch-package cannot apply the following patch file: - - patches/left-pad+1.3.0+002+broken.patch - - Try removing node_modules and trying again. If that doesn't work, maybe there was - an accidental change made to the patch file? Try recreating it by manually - editing the appropriate files and running: - - patch-package left-pad - - If that doesn't work, then it's a bug in patch-package, so please submit a bug - report. Thanks! - - https://github.com/ds300/patch-package/issues - - ---- -patch-package finished with 1 error(s). -END SNAPSHOT" -`; - exports[`Test apply-multiple-patches: 05: patch-package fails when a patch file is removed 1`] = ` "SNAPSHOT: patch-package fails when a patch file is removed +patch-package 0.0.0 +Applying patches... Error: The patches for left-pad have changed. You should reinstall your node_modules folder to make sure the package is up to date END SNAPSHOT" `; diff --git a/integration-tests/apply-multiple-patches/apply-multiple-patches.sh b/integration-tests/apply-multiple-patches/apply-multiple-patches.sh index 1055b7dc..f8a344aa 100755 --- a/integration-tests/apply-multiple-patches/apply-multiple-patches.sh +++ b/integration-tests/apply-multiple-patches/apply-multiple-patches.sh @@ -26,12 +26,12 @@ cp *broken.patch patches/ rm -rf node_modules npm install -(>&2 echo "SNAPSHOT: patch-package fails when a patch in the sequence is invalid") +echo "SNAPSHOT: patch-package fails when a patch in the sequence is invalid" if patch-package then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" echo "SNAPSHOT: patch-package only applies the first patch if the second of three is invalid" @@ -47,9 +47,9 @@ echo "END SNAPSHOT" rm patches/*hello.patch -(>&2 echo "SNAPSHOT: patch-package fails when a patch file is removed") +echo "SNAPSHOT: patch-package fails when a patch file is removed" if patch-package then exit 1 fi -(>&2 echo "END SNAPSHOT") \ No newline at end of file +echo "END SNAPSHOT" \ No newline at end of file diff --git a/integration-tests/broken-patch-file/__snapshots__/broken-patch-file.test.ts.snap b/integration-tests/broken-patch-file/__snapshots__/broken-patch-file.test.ts.snap index d7203853..afebace1 100644 --- a/integration-tests/broken-patch-file/__snapshots__/broken-patch-file.test.ts.snap +++ b/integration-tests/broken-patch-file/__snapshots__/broken-patch-file.test.ts.snap @@ -2,6 +2,8 @@ exports[`Test broken-patch-file: 00: patch-package fails when patch file is invalid 1`] = ` "SNAPSHOT: patch-package fails when patch file is invalid +patch-package 0.0.0 +Applying patches... **ERROR** Failed to apply patch for package left-pad at path diff --git a/integration-tests/broken-patch-file/broken-patch-file.sh b/integration-tests/broken-patch-file/broken-patch-file.sh index 33d102a7..2813ebd5 100755 --- a/integration-tests/broken-patch-file/broken-patch-file.sh +++ b/integration-tests/broken-patch-file/broken-patch-file.sh @@ -5,9 +5,9 @@ echo "add patch-package" yarn add $1 alias patch-package=./node_modules/.bin/patch-package -(>&2 echo "SNAPSHOT: patch-package fails when patch file is invalid") +echo "SNAPSHOT: patch-package fails when patch file is invalid" if patch-package then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" diff --git a/integration-tests/collate-errors/__snapshots__/collate-errors.test.ts.snap b/integration-tests/collate-errors/__snapshots__/collate-errors.test.ts.snap index 3320fcc7..5d756db7 100644 --- a/integration-tests/collate-errors/__snapshots__/collate-errors.test.ts.snap +++ b/integration-tests/collate-errors/__snapshots__/collate-errors.test.ts.snap @@ -2,16 +2,12 @@ exports[`Test collate-errors: 00: left-pad, lodash, and zfs apply 1`] = ` "SNAPSHOT: left-pad, lodash, and zfs apply +SNAPSHOT: underscore does not apply, left-pad warns patch-package 0.0.0 Applying patches... left-pad@1.1.1 ✔ lodash@4.17.21 ✔ zfs@1.3.0 ✔ -END SNAPSHOT" -`; - -exports[`Test collate-errors: 01: underscore does not apply, left-pad warns 1`] = ` -"SNAPSHOT: underscore does not apply, left-pad warns Warning: patch-package detected a patch file version mismatch diff --git a/integration-tests/collate-errors/collate-errors.sh b/integration-tests/collate-errors/collate-errors.sh index a16b6aeb..9da30e8f 100755 --- a/integration-tests/collate-errors/collate-errors.sh +++ b/integration-tests/collate-errors/collate-errors.sh @@ -6,10 +6,10 @@ yarn add $1 alias patch-package=./node_modules/.bin/patch-package echo "SNAPSHOT: left-pad, lodash, and zfs apply" -(>&2 echo "SNAPSHOT: underscore does not apply, left-pad warns") +echo "SNAPSHOT: underscore does not apply, left-pad warns" if patch-package; then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" echo "END SNAPSHOT" \ No newline at end of file diff --git a/integration-tests/dev-only-patches/__snapshots__/dev-only-patches.test.ts.snap b/integration-tests/dev-only-patches/__snapshots__/dev-only-patches.test.ts.snap index 52f7c899..5eb97868 100644 --- a/integration-tests/dev-only-patches/__snapshots__/dev-only-patches.test.ts.snap +++ b/integration-tests/dev-only-patches/__snapshots__/dev-only-patches.test.ts.snap @@ -9,18 +9,12 @@ Skipping dev-only slash@3.0.0 ✔ END SNAPSHOT" `; -exports[`Test dev-only-patches: 01: fake-package should be skipped 1`] = ` -"SNAPSHOT: fake-package should be skipped +exports[`Test dev-only-patches: 01: patch-package fails to find fake-package 1`] = ` +"SNAPSHOT: patch-package fails to find fake-package patch-package 0.0.0 Applying patches... -Skipping dev-only fake-package@3.0.0 ✔ left-pad@1.3.0 ✔ Skipping dev-only slash@3.0.0 ✔ -END SNAPSHOT" -`; - -exports[`Test dev-only-patches: 02: patch-package fails to find fake-package 1`] = ` -"SNAPSHOT: patch-package fails to find fake-package Error: Patch file found for package fake-package which is not present at node_modules/fake-package If this package is a dev dependency, rename the patch file to @@ -31,3 +25,13 @@ Error: Patch file found for package fake-package which is not present at node_mo patch-package finished with 1 error(s). END SNAPSHOT" `; + +exports[`Test dev-only-patches: 02: fake-package should be skipped 1`] = ` +"SNAPSHOT: fake-package should be skipped +patch-package 0.0.0 +Applying patches... +Skipping dev-only fake-package@3.0.0 ✔ +left-pad@1.3.0 ✔ +Skipping dev-only slash@3.0.0 ✔ +END SNAPSHOT" +`; diff --git a/integration-tests/dev-only-patches/dev-only-patches.sh b/integration-tests/dev-only-patches/dev-only-patches.sh index 98a5a130..4030906a 100755 --- a/integration-tests/dev-only-patches/dev-only-patches.sh +++ b/integration-tests/dev-only-patches/dev-only-patches.sh @@ -16,12 +16,12 @@ echo "END SNAPSHOT" echo "create fake-package+3.0.0.patch" cp patches/slash+3.0.0.patch patches/fake-package+3.0.0.patch -(>&2 echo "SNAPSHOT: patch-package fails to find fake-package") +echo "SNAPSHOT: patch-package fails to find fake-package" if patch-package then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" echo "rename fake-package patch file to .dev.patch" mv patches/fake-package+3.0.0.patch patches/fake-package+3.0.0.dev.patch diff --git a/integration-tests/error-on-fail/__snapshots__/error-on-fail.test.ts.snap b/integration-tests/error-on-fail/__snapshots__/error-on-fail.test.ts.snap index 0b17701f..296a6d81 100644 --- a/integration-tests/error-on-fail/__snapshots__/error-on-fail.test.ts.snap +++ b/integration-tests/error-on-fail/__snapshots__/error-on-fail.test.ts.snap @@ -2,6 +2,8 @@ exports[`Test error-on-fail: 00: at dev time patch-package fails but returns 0 1`] = ` "SNAPSHOT: at dev time patch-package fails but returns 0 +patch-package 0.0.0 +Applying patches... **ERROR** Failed to apply patch for package left-pad at path diff --git a/integration-tests/error-on-fail/error-on-fail.sh b/integration-tests/error-on-fail/error-on-fail.sh index ee3b7635..2765323c 100755 --- a/integration-tests/error-on-fail/error-on-fail.sh +++ b/integration-tests/error-on-fail/error-on-fail.sh @@ -8,12 +8,12 @@ alias patch-package=./node_modules/.bin/patch-package export NODE_ENV="development" export CI="true" -(>&2 echo "SNAPSHOT: at dev time patch-package fails but returns 0") +echo "SNAPSHOT: at dev time patch-package fails but returns 0" if ! patch-package; then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" echo "adding --error-on-fail forces patch-package to return 1 at dev time" if patch-package --error-on-fail; diff --git a/integration-tests/error-on-warn/__snapshots__/error-on-warn.test.ts.snap b/integration-tests/error-on-warn/__snapshots__/error-on-warn.test.ts.snap index 9a399047..590d5750 100644 --- a/integration-tests/error-on-warn/__snapshots__/error-on-warn.test.ts.snap +++ b/integration-tests/error-on-warn/__snapshots__/error-on-warn.test.ts.snap @@ -2,6 +2,9 @@ exports[`Test error-on-warn: 00: at dev time patch-package warns but returns 0 1`] = ` "SNAPSHOT: at dev time patch-package warns but returns 0 +patch-package 0.0.0 +Applying patches... +left-pad@1.1.2 ✔ Warning: patch-package detected a patch file version mismatch diff --git a/integration-tests/error-on-warn/error-on-warn.sh b/integration-tests/error-on-warn/error-on-warn.sh index aa1459d9..af31eb65 100755 --- a/integration-tests/error-on-warn/error-on-warn.sh +++ b/integration-tests/error-on-warn/error-on-warn.sh @@ -8,12 +8,12 @@ alias patch-package=./node_modules/.bin/patch-package export NODE_ENV="development" export CI="" -(>&2 echo "SNAPSHOT: at dev time patch-package warns but returns 0") +echo "SNAPSHOT: at dev time patch-package warns but returns 0" if ! patch-package; then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" echo "adding --error-on-warn forces patch-package to return 1 at dev time" if patch-package --error-on-warn; diff --git a/integration-tests/fails-when-no-package/__snapshots__/fails-when-no-package.test.ts.snap b/integration-tests/fails-when-no-package/__snapshots__/fails-when-no-package.test.ts.snap index b2907f2d..ef54f873 100644 --- a/integration-tests/fails-when-no-package/__snapshots__/fails-when-no-package.test.ts.snap +++ b/integration-tests/fails-when-no-package/__snapshots__/fails-when-no-package.test.ts.snap @@ -2,6 +2,8 @@ exports[`Test fails-when-no-package: 00: no package present failure 1`] = ` "SNAPSHOT: no package present failure +patch-package 0.0.0 +Applying patches... Error: Patch file found for package left-pad which is not present at node_modules/left-pad --- patch-package finished with 1 error(s). diff --git a/integration-tests/fails-when-no-package/fails-when-no-package.sh b/integration-tests/fails-when-no-package/fails-when-no-package.sh index b35f9503..a9564692 100755 --- a/integration-tests/fails-when-no-package/fails-when-no-package.sh +++ b/integration-tests/fails-when-no-package/fails-when-no-package.sh @@ -5,8 +5,8 @@ echo "add patch-package" yarn add $1 alias patch-package=./node_modules/.bin/patch-package -(>&2 echo "SNAPSHOT: no package present failure") +echo "SNAPSHOT: no package present failure" if patch-package; then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" diff --git a/integration-tests/ignore-whitespace/__snapshots__/ignore-whitespace.test.ts.snap b/integration-tests/ignore-whitespace/__snapshots__/ignore-whitespace.test.ts.snap index 765c26fd..1e9d3cde 100644 --- a/integration-tests/ignore-whitespace/__snapshots__/ignore-whitespace.test.ts.snap +++ b/integration-tests/ignore-whitespace/__snapshots__/ignore-whitespace.test.ts.snap @@ -1,6 +1,17 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Test ignore-whitespace: 00: line a changed 1`] = ` +exports[`Test ignore-whitespace: 00: empty changeset when adding whitespace 1`] = ` +"SNAPSHOT: empty changeset when adding whitespace +patch-package 0.0.0 +• Creating temporary folder +• Installing alphabet@1.0.0 with yarn +• Diffing your files with clean files +⁉️ Not creating patch file for package 'alphabet' +⁉️ There don't appear to be any changes. +END SNAPSHOT" +`; + +exports[`Test ignore-whitespace: 01: line a changed 1`] = ` "SNAPSHOT: line a changed diff --git a/node_modules/alphabet/index.js b/node_modules/alphabet/index.js index 7811d3b..454414b 100644 @@ -14,10 +25,3 @@ index 7811d3b..454414b 100644 // d END SNAPSHOT" `; - -exports[`Test ignore-whitespace: 01: empty changeset when adding whitespace 1`] = ` -"SNAPSHOT: empty changeset when adding whitespace -⁉️ Not creating patch file for package 'alphabet' -⁉️ There don't appear to be any changes. -END SNAPSHOT" -`; diff --git a/integration-tests/ignore-whitespace/ignore-whitespace.sh b/integration-tests/ignore-whitespace/ignore-whitespace.sh index 106788cf..f380c447 100755 --- a/integration-tests/ignore-whitespace/ignore-whitespace.sh +++ b/integration-tests/ignore-whitespace/ignore-whitespace.sh @@ -9,12 +9,12 @@ echo "add random bits of whitespace" node add-whitespace.js echo "try to make patch file (should be empty)" -(>&2 echo "SNAPSHOT: empty changeset when adding whitespace") +echo "SNAPSHOT: empty changeset when adding whitespace" if patch-package alphabet then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" echo "make a change to line a" node strip-whitespace.js diff --git a/integration-tests/ignores-scripts-when-making-patch/__snapshots__/ignores-scripts-when-making-patch.test.ts.snap b/integration-tests/ignores-scripts-when-making-patch/__snapshots__/ignores-scripts-when-making-patch.test.ts.snap index 068632ae..92a0a3e5 100644 --- a/integration-tests/ignores-scripts-when-making-patch/__snapshots__/ignores-scripts-when-making-patch.test.ts.snap +++ b/integration-tests/ignores-scripts-when-making-patch/__snapshots__/ignores-scripts-when-making-patch.test.ts.snap @@ -2,6 +2,7 @@ exports[`Test ignores-scripts-when-making-patch: 00: the patch creation output should look normal 1`] = ` "SNAPSHOT: the patch creation output should look normal +SNAPSHOT: there should be no stderr patch-package 0.0.0 • Creating temporary folder • Installing naughty-package@1.0.0 with yarn @@ -25,8 +26,3 @@ index 3784520..c4af29c 100755 if ls ../patch-package ; END SNAPSHOT" `; - -exports[`Test ignores-scripts-when-making-patch: 02: there should be no stderr 1`] = ` -"SNAPSHOT: there should be no stderr -END SNAPSHOT" -`; diff --git a/integration-tests/ignores-scripts-when-making-patch/ignores-scripts-when-making-patch.sh b/integration-tests/ignores-scripts-when-making-patch/ignores-scripts-when-making-patch.sh index 4f6fe336..4400ee03 100755 --- a/integration-tests/ignores-scripts-when-making-patch/ignores-scripts-when-making-patch.sh +++ b/integration-tests/ignores-scripts-when-making-patch/ignores-scripts-when-making-patch.sh @@ -8,10 +8,10 @@ alias patch-package=./node_modules/.bin/patch-package npx replace postinstall lol node_modules/naughty-package/postinstall.sh echo "SNAPSHOT: the patch creation output should look normal" -(>&2 echo "SNAPSHOT: there should be no stderr") +echo "SNAPSHOT: there should be no stderr" patch-package naughty-package echo "END SNAPSHOT" -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" echo "SNAPSHOT: a patch file got produced" cat patches/naughty-package+1.0.0.patch diff --git a/integration-tests/newIntegrationTest.ts b/integration-tests/newIntegrationTest.ts index 453e4e34..a88c9148 100644 --- a/integration-tests/newIntegrationTest.ts +++ b/integration-tests/newIntegrationTest.ts @@ -5,8 +5,8 @@ import { spawnSafeSync } from "../src/spawnSafe" const testName = process.argv[2] if (!testName || !testName.match(/[0-9a-zA-Z\-]+/)) { - console.error(`invalid name format '${testName}'`) - console.error("try something like this: blah-and-so-forth") + console.log(`invalid name format '${testName}'`) + console.log("try something like this: blah-and-so-forth") } console.log("making an integration test called", testName) diff --git a/integration-tests/no-symbolic-links/__snapshots__/no-symbolic-links.test.ts.snap b/integration-tests/no-symbolic-links/__snapshots__/no-symbolic-links.test.ts.snap index 486acf89..d7e5b305 100644 --- a/integration-tests/no-symbolic-links/__snapshots__/no-symbolic-links.test.ts.snap +++ b/integration-tests/no-symbolic-links/__snapshots__/no-symbolic-links.test.ts.snap @@ -2,6 +2,10 @@ exports[`Test no-symbolic-links: 00: patch-package fails to create a patch when there are symbolic links 1`] = ` "SNAPSHOT: patch-package fails to create a patch when there are symbolic links +patch-package 0.0.0 +• Creating temporary folder +• Installing left-pad@1.3.0 with yarn +• Diffing your files with clean files ⛔️ ERROR diff --git a/integration-tests/no-symbolic-links/no-symbolic-links.sh b/integration-tests/no-symbolic-links/no-symbolic-links.sh index 5aa02c60..26b65940 100755 --- a/integration-tests/no-symbolic-links/no-symbolic-links.sh +++ b/integration-tests/no-symbolic-links/no-symbolic-links.sh @@ -8,9 +8,9 @@ alias patch-package=./node_modules/.bin/patch-package echo "make symbolic link" ln -s package.json node_modules/left-pad/package.parent.json -(>&2 echo "SNAPSHOT: patch-package fails to create a patch when there are symbolic links") +echo "SNAPSHOT: patch-package fails to create a patch when there are symbolic links" if patch-package left-pad then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" diff --git a/integration-tests/package-gets-updated/__snapshots__/package-gets-updated.test.ts.snap b/integration-tests/package-gets-updated/__snapshots__/package-gets-updated.test.ts.snap index 76abdc19..d4b0f075 100644 --- a/integration-tests/package-gets-updated/__snapshots__/package-gets-updated.test.ts.snap +++ b/integration-tests/package-gets-updated/__snapshots__/package-gets-updated.test.ts.snap @@ -6,14 +6,22 @@ exports[`Test package-gets-updated: 00: left-pad should contain patch-package 1` END SNAPSHOT" `; -exports[`Test package-gets-updated: 01: left-pad should still contain patch-package 1`] = ` -"SNAPSHOT: left-pad should still contain patch-package - // devide \`len\` by 2, ditch the patch-package -END SNAPSHOT" -`; - -exports[`Test package-gets-updated: 02: warning when the patch was applied but version changed 1`] = ` +exports[`Test package-gets-updated: 01: warning when the patch was applied but version changed 1`] = ` "SNAPSHOT: warning when the patch was applied but version changed +[1/4] Resolving packages... +[2/4] Fetching packages... +[3/4] Linking dependencies... +[4/4] Building fresh packages... +success Saved lockfile. +success Saved 1 new dependency. +info Direct dependencies +└─ patch-package@0.0.0 +info All dependencies +└─ patch-package@0.0.0 +$ patch-package +patch-package 0.0.0 +Applying patches... +left-pad@1.1.1 ✔ Warning: patch-package detected a patch file version mismatch @@ -45,9 +53,27 @@ patch-package finished with 1 warning(s). END SNAPSHOT" `; +exports[`Test package-gets-updated: 02: left-pad should still contain patch-package 1`] = ` +"SNAPSHOT: left-pad should still contain patch-package + // devide \`len\` by 2, ditch the patch-package +END SNAPSHOT" +`; + exports[`Test package-gets-updated: 03: fail when the patch was not applied 1`] = ` "SNAPSHOT: fail when the patch was not applied -warning left-pad@1.1.3: use String.prototype.padStart() +[1/4] Resolving packages... +[2/4] Fetching packages... +[3/4] Linking dependencies... +[4/4] Building fresh packages... +success Saved lockfile. +success Saved 1 new dependency. +info Direct dependencies +└─ left-pad@1.1.3 +info All dependencies +└─ left-pad@1.1.3 +$ patch-package +patch-package 0.0.0 +Applying patches... **ERROR** Failed to apply patch for package left-pad at path @@ -77,6 +103,6 @@ warning left-pad@1.1.3: use String.prototype.padStart() --- patch-package finished with 1 error(s). -error Command failed with exit code 1. +info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command. END SNAPSHOT" `; diff --git a/integration-tests/package-gets-updated/package-gets-updated.sh b/integration-tests/package-gets-updated/package-gets-updated.sh index 384f9f40..157873fe 100755 --- a/integration-tests/package-gets-updated/package-gets-updated.sh +++ b/integration-tests/package-gets-updated/package-gets-updated.sh @@ -9,19 +9,19 @@ echo "SNAPSHOT: left-pad should contain patch-package" grep patch-package node_modules/left-pad/index.js echo "END SNAPSHOT" -(>&2 echo "SNAPSHOT: warning when the patch was applied but version changed") +echo "SNAPSHOT: warning when the patch was applied but version changed" yarn add left-pad@1.1.2 -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" echo "SNAPSHOT: left-pad should still contain patch-package" grep patch-package node_modules/left-pad/index.js echo "END SNAPSHOT" -(>&2 echo "SNAPSHOT: fail when the patch was not applied") +echo "SNAPSHOT: fail when the patch was not applied" if yarn add left-pad@1.1.3 ; then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" echo "left-pad should not contain patch-package" if grep patch-package node_modules/left-pad/index.js ; then diff --git a/integration-tests/patch-parse-failure/__snapshots__/patch-parse-failure.test.ts.snap b/integration-tests/patch-parse-failure/__snapshots__/patch-parse-failure.test.ts.snap index d2c93d22..89c5f681 100644 --- a/integration-tests/patch-parse-failure/__snapshots__/patch-parse-failure.test.ts.snap +++ b/integration-tests/patch-parse-failure/__snapshots__/patch-parse-failure.test.ts.snap @@ -2,6 +2,8 @@ exports[`Test patch-parse-failure: 00: patch parse failure message 1`] = ` "SNAPSHOT: patch parse failure message +patch-package 0.0.0 +Applying patches... **ERROR** Failed to apply patch for package left-pad diff --git a/integration-tests/patch-parse-failure/patch-parse-failure.sh b/integration-tests/patch-parse-failure/patch-parse-failure.sh index 38b2e875..4200c778 100755 --- a/integration-tests/patch-parse-failure/patch-parse-failure.sh +++ b/integration-tests/patch-parse-failure/patch-parse-failure.sh @@ -5,8 +5,8 @@ echo "add patch-package" yarn add $1 alias patch-package=./node_modules/.bin/patch-package -(>&2 echo "SNAPSHOT: patch parse failure message") +echo "SNAPSHOT: patch parse failure message" if patch-package; then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" diff --git a/integration-tests/rebase-insert/__snapshots__/rebase-insert.test.ts.snap b/integration-tests/rebase-insert/__snapshots__/rebase-insert.test.ts.snap index 86eac2c8..924899f2 100644 --- a/integration-tests/rebase-insert/__snapshots__/rebase-insert.test.ts.snap +++ b/integration-tests/rebase-insert/__snapshots__/rebase-insert.test.ts.snap @@ -49,6 +49,7 @@ patch-package 0.0.0 • Diffing your files with clean files ✔ Created file patches/left-pad+1.3.0+003+some-stuff.patch +Renaming left-pad+1.3.0+003+goodbye.patch to left-pad+1.3.0+004+goodbye.patch Fast forwarding... ✔ left-pad+1.3.0+004+goodbye.patch ls patches diff --git a/integration-tests/reverse-multiple-patches/__snapshots__/reverse-multiple-patches.test.ts.snap b/integration-tests/reverse-multiple-patches/__snapshots__/reverse-multiple-patches.test.ts.snap index 7364dfcc..d77ab581 100644 --- a/integration-tests/reverse-multiple-patches/__snapshots__/reverse-multiple-patches.test.ts.snap +++ b/integration-tests/reverse-multiple-patches/__snapshots__/reverse-multiple-patches.test.ts.snap @@ -36,6 +36,21 @@ patch-package 0.0.0 Applying patches... left-pad@1.3.0 (1 hello) ✔ left-pad@1.3.0 (2 world) ✔ + +⛔ ERROR + +Failed to apply patch file left-pad+1.3.0+003+goodbye.patch. + +If this patch file is no longer useful, delete it and run + + patch-package + +Otherwise you should open node_modules/left-pad, manually apply the changes from the patch file, and run + + patch-package left-pad + +to update the patch file. + reverse all but broken patch-package 0.0.0 Applying patches... diff --git a/integration-tests/runIntegrationTest.ts b/integration-tests/runIntegrationTest.ts index aa64fdab..e0d857ee 100644 --- a/integration-tests/runIntegrationTest.ts +++ b/integration-tests/runIntegrationTest.ts @@ -59,7 +59,7 @@ export function runIntegrationTest({ const output = result.stdout.toString() + "\n" + result.stderr.toString() if (result.status !== 0) { - console.error(output) + console.log(output) } it("should produce output", () => { diff --git a/integration-tests/unexpected-patch-creation-failure/__snapshots__/unexpected-patch-creation-failure.test.ts.snap b/integration-tests/unexpected-patch-creation-failure/__snapshots__/unexpected-patch-creation-failure.test.ts.snap index 4bb932d9..7c31f549 100644 --- a/integration-tests/unexpected-patch-creation-failure/__snapshots__/unexpected-patch-creation-failure.test.ts.snap +++ b/integration-tests/unexpected-patch-creation-failure/__snapshots__/unexpected-patch-creation-failure.test.ts.snap @@ -2,6 +2,10 @@ exports[`Test unexpected-patch-creation-failure: 00: patch-package fails to parse a patch it created 1`] = ` "SNAPSHOT: patch-package fails to parse a patch it created +patch-package 0.0.0 +• Creating temporary folder +• Installing left-pad@1.3.0 with yarn +• Diffing your files with clean files ⛔️ ERROR diff --git a/integration-tests/unexpected-patch-creation-failure/unexpected-patch-creation-failure.sh b/integration-tests/unexpected-patch-creation-failure/unexpected-patch-creation-failure.sh index 15588a57..ac347f3b 100755 --- a/integration-tests/unexpected-patch-creation-failure/unexpected-patch-creation-failure.sh +++ b/integration-tests/unexpected-patch-creation-failure/unexpected-patch-creation-failure.sh @@ -17,12 +17,12 @@ then exit 1 fi -(>&2 echo "SNAPSHOT: patch-package fails to parse a patch it created") +echo "SNAPSHOT: patch-package fails to parse a patch it created" if patch-package left-pad then exit 1 fi -(>&2 echo "END SNAPSHOT") +echo "END SNAPSHOT" echo "there is now an error log file" ls ./patch-package-error.json.gz diff --git a/property-based-tests/testCases.ts b/property-based-tests/testCases.ts index 8fef3bb8..8875f754 100644 --- a/property-based-tests/testCases.ts +++ b/property-based-tests/testCases.ts @@ -121,7 +121,7 @@ function insertLinesIntoFile(file: File): File { function getUniqueFilename(files: Files) { let filename = makeFileName() const ks = Object.keys(files) - while (ks.some(k => k.startsWith(filename))) { + while (ks.some((k) => k.startsWith(filename))) { filename = makeFileName() } return filename diff --git a/src/applyPatches.ts b/src/applyPatches.ts index e3c9f476..b788151a 100644 --- a/src/applyPatches.ts +++ b/src/applyPatches.ts @@ -107,7 +107,7 @@ export function applyPatchesForApp({ const groupedPatches = getGroupedPatches(patchesDirectory) if (groupedPatches.numPatchFiles === 0) { - console.error(chalk.blueBright("No patch files found")) + console.log(chalk.blueBright("No patch files found")) return } @@ -128,10 +128,10 @@ export function applyPatchesForApp({ } for (const warning of warnings) { - console.warn(warning) + console.log(warning) } for (const error of errors) { - console.error(error) + console.log(error) } const problemsSummary = [] @@ -143,11 +143,8 @@ export function applyPatchesForApp({ } if (problemsSummary.length) { - console.error("---") - console.error( - "patch-package finished with", - problemsSummary.join(", ") + ".", - ) + console.log("---") + console.log("patch-package finished with", problemsSummary.join(", ") + ".") } if (errors.length && shouldExitWithError) { @@ -186,6 +183,9 @@ export function applyPatchesForPackage({ if (unappliedPatches && state) { for (let i = 0; i < state.patches.length; i++) { const patchThatWasApplied = state.patches[i] + if (!patchThatWasApplied.didApply) { + break + } const patchToApply = unappliedPatches[0] const currentPatchHash = hashFile( join(appPath, patchDir, patchToApply.patchFilename), @@ -194,7 +194,7 @@ export function applyPatchesForPackage({ // this patch was applied we can skip it appliedPatches.push(unappliedPatches.shift()!) } else { - console.error( + console.log( chalk.red("Error:"), `The patches for ${chalk.bold(pathSpecifier)} have changed.`, `You should reinstall your node_modules folder to make sure the package is up to date`, @@ -385,6 +385,9 @@ export function applyPatchesForPackage({ isRebasing: !!failedPatch, }) } + if (failedPatch) { + process.exit(1) + } } } diff --git a/src/createIssue.ts b/src/createIssue.ts index 1a30be7b..de9e721d 100644 --- a/src/createIssue.ts +++ b/src/createIssue.ts @@ -89,7 +89,7 @@ export function openIssueCreationLink({ const vcs = getPackageVCSDetails(packageDetails) if (!vcs) { - console.error( + console.log( `Error: Couldn't find VCS details for ${packageDetails.pathSpecifier}`, ) process.exit(1) diff --git a/src/detectPackageManager.ts b/src/detectPackageManager.ts index 00976527..c7bb7559 100644 --- a/src/detectPackageManager.ts +++ b/src/detectPackageManager.ts @@ -7,7 +7,7 @@ import findWorkspaceRoot from "find-yarn-workspace-root" export type PackageManager = "yarn" | "npm" | "npm-shrinkwrap" function printNoYarnLockfileError() { - console.error(` + console.log(` ${chalk.red.bold("**ERROR**")} ${chalk.red( `The --use-yarn option was specified but there is no yarn.lock file`, )} @@ -15,7 +15,7 @@ ${chalk.red.bold("**ERROR**")} ${chalk.red( } function printNoLockfilesError() { - console.error(` + console.log(` ${chalk.red.bold("**ERROR**")} ${chalk.red( `No package-lock.json, npm-shrinkwrap.json, or yarn.lock file. diff --git a/src/filterFiles.ts b/src/filterFiles.ts index 60983b32..c707cd72 100644 --- a/src/filterFiles.ts +++ b/src/filterFiles.ts @@ -8,10 +8,10 @@ export function removeIgnoredFiles( excludePaths: RegExp, ) { klawSync(dir, { nodir: true }) - .map(item => item.path.slice(`${dir}/`.length)) + .map((item) => item.path.slice(`${dir}/`.length)) .filter( - relativePath => + (relativePath) => !relativePath.match(includePaths) || relativePath.match(excludePaths), ) - .forEach(relativePath => removeSync(join(dir, relativePath))) + .forEach((relativePath) => removeSync(join(dir, relativePath))) } diff --git a/src/getPackageResolution.ts b/src/getPackageResolution.ts index 24db4bd6..a046e7f9 100644 --- a/src/getPackageResolution.ts +++ b/src/getPackageResolution.ts @@ -42,7 +42,7 @@ export function getPackageResolution({ try { appLockFile = yaml.parse(lockFileString) } catch (e) { - console.error(e) + console.log(e) throw new Error("Could not parse yarn v2 lock file") } } @@ -70,7 +70,7 @@ export function getPackageResolution({ } if (new Set(resolutions).size !== 1) { - console.warn( + console.log( `Ambigious lockfile entries for ${packageDetails.pathSpecifier}. Using version ${installedVersion}`, ) return installedVersion @@ -125,7 +125,7 @@ export function getPackageResolution({ if (require.main === module) { const packageDetails = getPatchDetailsFromCliString(process.argv[2]) if (!packageDetails) { - console.error(`Can't find package ${process.argv[2]}`) + console.log(`Can't find package ${process.argv[2]}`) process.exit(1) } console.log( diff --git a/src/index.ts b/src/index.ts index 703a45ee..43ac7c9d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -47,7 +47,7 @@ if (argv.version || argv.v) { } if ("rebase" in argv) { if (!argv.rebase) { - console.error( + console.log( chalk.red( "You must specify a patch file name or number when rebasing patches", ), @@ -55,7 +55,7 @@ if (argv.version || argv.v) { process.exit(1) } if (packageNames.length !== 1) { - console.error( + console.log( chalk.red( "You must specify exactly one package name when rebasing patches", ), diff --git a/src/makePatch.ts b/src/makePatch.ts index 33b307fc..aa26fb88 100644 --- a/src/makePatch.ts +++ b/src/makePatch.ts @@ -7,7 +7,6 @@ import { mkdirpSync, mkdirSync, realpathSync, - unlinkSync, writeFileSync, } from "fs-extra" import { sync as rimraf } from "rimraf" @@ -47,7 +46,7 @@ function printNoPackageFoundError( packageName: string, packageJsonPath: string, ) { - console.error( + console.log( `No such package ${packageName} File not found: ${packageJsonPath}`, @@ -76,7 +75,7 @@ export function makePatch({ const packageDetails = getPatchDetailsFromCliString(packagePathSpecifier) if (!packageDetails) { - console.error("No such package", packagePathSpecifier) + console.log("No such package", packagePathSpecifier) return } @@ -112,12 +111,12 @@ export function makePatch({ : existingPatches.slice(0, -1) if (createIssue && mode.type === "append") { - console.error("--create-issue is not compatible with --append.") + console.log("--create-issue is not compatible with --append.") process.exit(1) } if (createIssue && isRebasing) { - console.error("--create-issue is not compatible with rebasing.") + console.log("--create-issue is not compatible with rebasing.") process.exit(1) } @@ -269,7 +268,7 @@ export function makePatch({ }) ) { // TODO: add better error message once --rebase is implemented - console.error( + console.log( `Failed to apply patch ${patchDetails.patchFilename} to ${packageDetails.pathSpecifier}`, ) process.exit(1) @@ -309,10 +308,10 @@ export function makePatch({ ) if (diffResult.stdout.length === 0) { - console.warn( + console.log( `⁉️ Not creating patch file for package '${packagePathSpecifier}'`, ) - console.warn(`⁉️ There don't appear to be any changes.`) + console.log(`⁉️ There don't appear to be any changes.`) process.exit(1) return } @@ -323,7 +322,7 @@ export function makePatch({ if ( (e as Error).message.includes("Unexpected file mode string: 120000") ) { - console.error(` + console.log(` ⛔️ ${chalk.red.bold("ERROR")} Your changes involve creating symlinks. patch-package does not yet support @@ -345,7 +344,7 @@ export function makePatch({ }), ), ) - console.error(` + console.log(` ⛔️ ${chalk.red.bold("ERROR")} patch-package was unable to read the patch-file made by git. This should not @@ -369,19 +368,7 @@ export function makePatch({ } // maybe delete existing - if (!isRebasing && mode.type === "overwrite_last") { - const prevPatch = patchesToApplyBeforeDiffing[ - patchesToApplyBeforeDiffing.length - 1 - ] as PatchedPackageDetails | undefined - if (prevPatch) { - const patchFilePath = join(appPath, patchDir, prevPatch.patchFilename) - try { - unlinkSync(patchFilePath) - } catch (e) { - // noop - } - } - } else if (!isRebasing && existingPatches.length === 1) { + if (mode.type === "append" && !isRebasing && existingPatches.length === 1) { // if we are appending to an existing patch that doesn't have a sequence number let's rename it const prevPatch = existingPatches[0] if (prevPatch.sequenceNumber === undefined) { @@ -445,6 +432,12 @@ export function makePatch({ sequenceName: p.sequenceName, sequenceNumber: next++, }) + console.log( + "Renaming", + chalk.bold(p.patchFilename), + "to", + chalk.bold(newName), + ) const oldPath = join(appPath, patchDir, p.patchFilename) const newPath = join(appPath, patchDir, newName) renameSync(oldPath, newPath) @@ -530,7 +523,7 @@ export function makePatch({ } } } catch (e) { - console.error(e) + console.log(e) throw e } finally { tmpRepo.removeCallback() diff --git a/src/makeRegExp.ts b/src/makeRegExp.ts index 3b3a080c..a64825ff 100644 --- a/src/makeRegExp.ts +++ b/src/makeRegExp.ts @@ -12,7 +12,7 @@ export const makeRegExp = ( try { return new RegExp(reString, caseSensitive ? "" : "i") } catch (_) { - console.error(`${chalk.red.bold("***ERROR***")} + console.log(`${chalk.red.bold("***ERROR***")} Invalid format for option --${name} Unable to convert the string ${JSON.stringify( diff --git a/src/patch/apply.ts b/src/patch/apply.ts index d1d6d5a2..ef5d13a9 100644 --- a/src/patch/apply.ts +++ b/src/patch/apply.ts @@ -66,7 +66,7 @@ export const executeEffects = ( (!isExecutable(eff.newMode) && !isExecutable(currentMode))) && dryRun ) { - console.warn( + console.log( `Mode change is not required for file ${humanReadable(eff.path)}`, ) } diff --git a/src/patch/read.test.ts b/src/patch/read.test.ts index 5e6c8a89..1ea4ef92 100644 --- a/src/patch/read.test.ts +++ b/src/patch/read.test.ts @@ -16,14 +16,16 @@ jest.mock("./parse", () => ({ }), })) -const error = jest.fn() -console.error = error +const log = jest.fn() +console.log = log process.cwd = jest.fn(() => "/test/root") process.exit = jest.fn() as any +const lastLog = () => log.mock.calls[log.mock.calls.length - 1][0] + describe(readPatch, () => { beforeEach(() => { - error.mockReset() + log.mockReset() }) it("throws an error for basic packages", () => { readPatch({ @@ -32,7 +34,7 @@ describe(readPatch, () => { patchDir: "patches/", }) - expect(removeAnsiCodes(error.mock.calls[0][0])).toMatchInlineSnapshot(` + expect(removeAnsiCodes(lastLog())).toMatchInlineSnapshot(` " **ERROR** Failed to apply patch for package test @@ -62,7 +64,7 @@ describe(readPatch, () => { patchDir: "patches/", }) - expect(removeAnsiCodes(error.mock.calls[0][0])).toMatchInlineSnapshot(` + expect(removeAnsiCodes(lastLog())).toMatchInlineSnapshot(` " **ERROR** Failed to apply patch for package @david/test @@ -91,7 +93,7 @@ describe(readPatch, () => { patchDir: "patches/", }) - expect(removeAnsiCodes(error.mock.calls[0][0])).toMatchInlineSnapshot(` + expect(removeAnsiCodes(lastLog())).toMatchInlineSnapshot(` " **ERROR** Failed to apply patch for package @david/test => react-native @@ -120,7 +122,7 @@ describe(readPatch, () => { patchDir: ".cruft/patches", }) - expect(removeAnsiCodes(error.mock.calls[0][0])).toMatchInlineSnapshot(` + expect(removeAnsiCodes(lastLog())).toMatchInlineSnapshot(` " **ERROR** Failed to apply patch for package @david/test => react-native @@ -151,7 +153,7 @@ describe(readPatch, () => { expect(process.cwd).toHaveBeenCalled() - expect(removeAnsiCodes(error.mock.calls[0][0])).toMatchInlineSnapshot(` + expect(removeAnsiCodes(lastLog())).toMatchInlineSnapshot(` " **ERROR** Failed to apply patch for package @david/test => react-native @@ -184,7 +186,7 @@ describe(readPatch, () => { expect(process.cwd).toHaveBeenCalled() - expect(removeAnsiCodes(error.mock.calls[0][0])).toMatchInlineSnapshot(` + expect(removeAnsiCodes(lastLog())).toMatchInlineSnapshot(` " **ERROR** Failed to apply patch for package @david/test => react-native diff --git a/src/patch/read.ts b/src/patch/read.ts index ea456038..3d8380ac 100644 --- a/src/patch/read.ts +++ b/src/patch/read.ts @@ -40,7 +40,7 @@ export function readPatch({ ) } - console.error(` + console.log(` ${chalk.red.bold("**ERROR**")} ${chalk.red( `Failed to apply patch for package ${chalk.bold( patchDetails.humanReadablePathSpecifier, diff --git a/src/rebase.ts b/src/rebase.ts index eb0d4d9a..893cf7ca 100644 --- a/src/rebase.ts +++ b/src/rebase.ts @@ -25,14 +25,14 @@ export function rebase({ const groupedPatches = getGroupedPatches(patchesDirectory) if (groupedPatches.numPatchFiles === 0) { - console.error(chalk.blueBright("No patch files found")) + console.log(chalk.blueBright("No patch files found")) process.exit(1) } const packagePatches = groupedPatches.pathSpecifierToPatchFiles[packagePathSpecifier] if (!packagePatches) { - console.error( + console.log( chalk.blueBright("No patch files found for package"), packagePathSpecifier, ) @@ -42,7 +42,7 @@ export function rebase({ const state = getPatchApplicationState(packagePatches[0]) if (!state) { - console.error( + console.log( chalk.blueBright("No patch state found"), "Did you forget to run", chalk.bold("patch-package"), @@ -51,7 +51,7 @@ export function rebase({ process.exit(1) } if (state.isRebasing) { - console.error( + console.log( chalk.blueBright("Already rebasing"), "Make changes to the files in", chalk.bold(packagePatches[0].path), @@ -64,13 +64,13 @@ export function rebase({ packagePatches[packagePatches.length - 1].patchFilename } file`, ) - console.error( + console.log( `💡 To remove a broken patch file, delete it and reinstall node_modules`, ) process.exit(1) } if (state.patches.length !== packagePatches.length) { - console.error( + console.log( chalk.blueBright("Some patches have not been applied."), "Reinstall node_modules and try again.", ) @@ -83,7 +83,7 @@ export function rebase({ packagePatches[i].patchFilename, ) if (!existsSync(fullPatchPath)) { - console.error( + console.log( chalk.blueBright("Expected patch file"), fullPatchPath, "to exist but it is missing. Try completely reinstalling node_modules first.", @@ -91,7 +91,7 @@ export function rebase({ process.exit(1) } if (patch.patchContentHash !== hashFile(fullPatchPath)) { - console.error( + console.log( chalk.blueBright("Patch file"), fullPatchPath, "has changed since it was applied. Try completely reinstalling node_modules first.", @@ -148,14 +148,14 @@ to insert a new patch file. }) if (!target) { - console.error( + console.log( chalk.red("Could not find target patch file"), chalk.bold(targetPatch), ) - console.error() - console.error("The list of available patch files is:") + console.log() + console.log("The list of available patch files is:") packagePatches.forEach((p) => { - console.error(` - ${p.patchFilename}`) + console.log(` - ${p.patchFilename}`) }) process.exit(1) @@ -166,12 +166,12 @@ to insert a new patch file. (p) => p.patchContentHash === currentHash, ) if (!prevApplication) { - console.error( + console.log( chalk.red("Could not find previous application of patch file"), chalk.bold(target.patchFilename), ) - console.error() - console.error("You should reinstall node_modules and try again.") + console.log() + console.log("You should reinstall node_modules and try again.") process.exit(1) } @@ -232,7 +232,7 @@ function unApplyPatches({ cwd: process.cwd(), }) ) { - console.error( + console.log( chalk.red("Failed to un-apply patch file"), chalk.bold(patch.patchFilename), "Try completely reinstalling node_modules.", diff --git a/src/spawnSafe.ts b/src/spawnSafe.ts index 3ea042f7..5278d121 100644 --- a/src/spawnSafe.ts +++ b/src/spawnSafe.ts @@ -1,35 +1,35 @@ -import { sync as spawnSync } from "cross-spawn" -import { SpawnOptions } from "child_process" - -export interface SpawnSafeOptions extends SpawnOptions { - throwOnError?: boolean - logStdErrOnError?: boolean - maxBuffer?: number -} - -const defaultOptions: SpawnSafeOptions = { - logStdErrOnError: true, - throwOnError: true, -} - -export const spawnSafeSync = ( - command: string, - args?: string[], - options?: SpawnSafeOptions, -) => { - const mergedOptions = Object.assign({}, defaultOptions, options) - const result = spawnSync(command, args, options) - if (result.error || result.status !== 0) { - if (mergedOptions.logStdErrOnError) { - if (result.stderr) { - console.error(result.stderr.toString()) - } else if (result.error) { - console.error(result.error) - } - } - if (mergedOptions.throwOnError) { - throw result - } - } - return result -} +import { sync as spawnSync } from "cross-spawn" +import { SpawnOptions } from "child_process" + +export interface SpawnSafeOptions extends SpawnOptions { + throwOnError?: boolean + logStdErrOnError?: boolean + maxBuffer?: number +} + +const defaultOptions: SpawnSafeOptions = { + logStdErrOnError: true, + throwOnError: true, +} + +export const spawnSafeSync = ( + command: string, + args?: string[], + options?: SpawnSafeOptions, +) => { + const mergedOptions = Object.assign({}, defaultOptions, options) + const result = spawnSync(command, args, options) + if (result.error || result.status !== 0) { + if (mergedOptions.logStdErrOnError) { + if (result.stderr) { + console.log(result.stderr.toString()) + } else if (result.error) { + console.log(result.error) + } + } + if (mergedOptions.throwOnError) { + throw result + } + } + return result +} diff --git a/src/stateFile.ts b/src/stateFile.ts index c56cdc42..422cdf80 100644 --- a/src/stateFile.ts +++ b/src/stateFile.ts @@ -32,7 +32,7 @@ export function getPatchApplicationState( return null } if (state.version !== version) { - console.error( + console.log( `You upgraded patch-package and need to fully reinstall node_modules to continue.`, ) process.exit(1)