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 new file mode 100644 index 00000000..61340a24 --- /dev/null +++ b/integration-tests/error-on-warn/__snapshots__/error-on-warn.test.ts.snap @@ -0,0 +1,34 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Test error-on-warn: at dev time patch-package warns but returns 0 1`] = ` +"SNAPSHOT: at dev time patch-package warns but returns 0 + +Warning: patch-package detected a patch file version mismatch + + Don't worry! This is probably fine. The patch was still applied + successfully. Here's the deets: + + Patch file created for + + left-pad@1.1.2 + + applied to + + left-pad@1.1.3 + + At path + + node_modules/left-pad + + This warning is just to give you a heads-up. There is a small chance of + breakage even though the patch was applied successfully. Make sure the package + still behaves like you expect (you wrote tests, right?) and then run + + patch-package left-pad + + to update the version in the patch file name and make this warning go away. + +--- +patch-package finished with 1 warning(s). +END SNAPSHOT" +`; diff --git a/integration-tests/error-on-warn/error-on-warn.sh b/integration-tests/error-on-warn/error-on-warn.sh new file mode 100755 index 00000000..aa1459d9 --- /dev/null +++ b/integration-tests/error-on-warn/error-on-warn.sh @@ -0,0 +1,22 @@ +# make sure errors stop the script +set -e + +echo "add patch-package" +yarn add $1 +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") +if ! patch-package; +then + exit 1 +fi +(>&2 echo "END SNAPSHOT") + +echo "adding --error-on-warn forces patch-package to return 1 at dev time" +if patch-package --error-on-warn; +then + exit 1 +fi diff --git a/integration-tests/error-on-warn/error-on-warn.test.ts b/integration-tests/error-on-warn/error-on-warn.test.ts new file mode 100644 index 00000000..c875400d --- /dev/null +++ b/integration-tests/error-on-warn/error-on-warn.test.ts @@ -0,0 +1,5 @@ +import { runIntegrationTest } from "../runIntegrationTest" +runIntegrationTest({ + projectName: "error-on-warn", + shouldProduceSnapshots: true, +}) diff --git a/integration-tests/error-on-warn/package.json b/integration-tests/error-on-warn/package.json new file mode 100644 index 00000000..67c4d0fd --- /dev/null +++ b/integration-tests/error-on-warn/package.json @@ -0,0 +1,11 @@ +{ + "name": "error-on-warn", + "version": "1.0.0", + "description": "integration test for patch-package", + "main": "index.js", + "author": "", + "license": "ISC", + "dependencies": { + "left-pad": "1.1.3" + } +} diff --git a/integration-tests/error-on-warn/patches/left-pad+1.1.2.patch b/integration-tests/error-on-warn/patches/left-pad+1.1.2.patch new file mode 100644 index 00000000..e294d98b --- /dev/null +++ b/integration-tests/error-on-warn/patches/left-pad+1.1.2.patch @@ -0,0 +1,13 @@ +diff --git a/node_modules/left-pad/index.js b/node_modules/left-pad/index.js +index 26f73ff..60f3f56 100644 +--- a/node_modules/left-pad/index.js ++++ b/node_modules/left-pad/index.js +@@ -7,7 +7,7 @@ + module.exports = leftPad; + + var cache = [ +- '', ++ "", + ' ', + ' ', + ' ', \ No newline at end of file diff --git a/integration-tests/error-on-warn/yarn.lock b/integration-tests/error-on-warn/yarn.lock new file mode 100644 index 00000000..392d269a --- /dev/null +++ b/integration-tests/error-on-warn/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +left-pad@1.1.3: + version "1.1.3" + resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.1.3.tgz#612f61c033f3a9e08e939f1caebeea41b6f3199a" + integrity sha1-YS9hwDPzqeCOk58crr7qQbbzGZo= diff --git a/src/applyPatches.ts b/src/applyPatches.ts index adbe2dc2..2bc4aba3 100644 --- a/src/applyPatches.ts +++ b/src/applyPatches.ts @@ -84,11 +84,13 @@ export function applyPatchesForApp({ reverse, patchDir, shouldExitWithError, + shouldExitWithWarning, }: { appPath: string reverse: boolean patchDir: string shouldExitWithError: boolean + shouldExitWithWarning: boolean }): void { const patchesDirectory = join(appPath, patchDir) const files = findPatchFiles(patchesDirectory) @@ -221,9 +223,15 @@ export function applyPatchesForApp({ ) } - if (errors.length) { - process.exit(shouldExitWithError ? 1 : 0) + if (errors.length && shouldExitWithError) { + process.exit(1) + } + + if (warnings.length && shouldExitWithWarning) { + process.exit(1) } + + process.exit(0) } export function applyPatch({ diff --git a/src/index.ts b/src/index.ts index a4691a28..654f67d4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,6 +21,7 @@ const argv = minimist(process.argv.slice(2), { "help", "version", "error-on-fail", + "error-on-warn", "create-issue", ], string: ["patch-dir"], @@ -78,7 +79,16 @@ if (argv.version || argv.v) { // see https://github.com/ds300/patch-package/issues/86 const shouldExitWithError = !!argv["error-on-fail"] || isCi || process.env.NODE_ENV === "test" - applyPatchesForApp({ appPath, reverse, patchDir, shouldExitWithError }) + + const shouldExitWithWarning = !!argv["error-on-warn"] + + applyPatchesForApp({ + appPath, + reverse, + patchDir, + shouldExitWithError, + shouldExitWithWarning, + }) } } @@ -116,6 +126,12 @@ Usage: --error-on-fail is ${chalk.bold("switched on")} by default on CI. See https://github.com/ds300/patch-package/issues/86 for background. + + ${chalk.bold("--error-on-warn")} + + Forces patch-package to exit with code 1 after warning. + + See https://github.com/ds300/patch-package/issues/314 for background. ${chalk.bold("--reverse")}