diff --git a/README.md b/README.md index c3a910b0..271b9d40 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,10 @@ Run `patch-package` without arguments to apply all patches in your project. Specify the name for the directory in which the patch files are located +- `--expect-patch` + + Prints error when no patches happen. Best combined with `--error-on-fail` (enabled by default on CI). + #### Notes To apply patches individually, you may use `git`: diff --git a/integration-tests/expect-patch/__snapshots__/expect-patch.test.ts.snap b/integration-tests/expect-patch/__snapshots__/expect-patch.test.ts.snap new file mode 100644 index 00000000..c65b7d69 --- /dev/null +++ b/integration-tests/expect-patch/__snapshots__/expect-patch.test.ts.snap @@ -0,0 +1,29 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Test expect-patch: 00: expect-patch flag prints an error message and by default does not fail 1`] = ` +"SNAPSHOT: expect-patch flag prints an error message and by default does not fail +patch-package 0.0.0 +Applying patches... +No patch files found +--- +patch-package finished with 1 error(s). +END SNAPSHOT" +`; + +exports[`Test expect-patch: 01: expect-patch is silent when patches happen 1`] = ` +"SNAPSHOT: expect-patch is silent when patches happen +patch-package 0.0.0 +Applying patches... +left-pad@1.3.0 ✔ +END SNAPSHOT" +`; + +exports[`Test expect-patch: 02: expect-patch flag produces error for error-on-fail flag 1`] = ` +"SNAPSHOT: expect-patch flag produces error for error-on-fail flag +patch-package 0.0.0 +Applying patches... +No patch files found +--- +patch-package finished with 1 error(s). +END SNAPSHOT" +`; diff --git a/integration-tests/expect-patch/expect-patch.sh b/integration-tests/expect-patch/expect-patch.sh new file mode 100755 index 00000000..353bed36 --- /dev/null +++ b/integration-tests/expect-patch/expect-patch.sh @@ -0,0 +1,30 @@ +# 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="true" + +echo "SNAPSHOT: expect-patch flag prints an error message and by default does not fail" +if ! patch-package --expect-patch; +then + exit 1 +fi +echo "END SNAPSHOT" + +echo "SNAPSHOT: expect-patch is silent when patches happen" +if ! patch-package --expect-patch --patch-dir patches-custom; +then + exit 1 +fi +echo "END SNAPSHOT" + +echo "SNAPSHOT: expect-patch flag produces error for error-on-fail flag" +if patch-package --expect-patch --error-on-fail; +then + exit 1 +fi +echo "END SNAPSHOT" \ No newline at end of file diff --git a/integration-tests/expect-patch/expect-patch.test.ts b/integration-tests/expect-patch/expect-patch.test.ts new file mode 100644 index 00000000..7b0b96a6 --- /dev/null +++ b/integration-tests/expect-patch/expect-patch.test.ts @@ -0,0 +1,5 @@ +import { runIntegrationTest } from "../runIntegrationTest" +runIntegrationTest({ + projectName: "expect-patch", + shouldProduceSnapshots: true, +}) diff --git a/integration-tests/expect-patch/package.json b/integration-tests/expect-patch/package.json new file mode 100644 index 00000000..5c4c8339 --- /dev/null +++ b/integration-tests/expect-patch/package.json @@ -0,0 +1,11 @@ +{ + "name": "expect-patch", + "version": "1.0.0", + "description": "integration test for patch-package", + "main": "index.js", + "author": "", + "license": "ISC", + "dependencies": { + "left-pad": "^1.3.0" + } +} diff --git a/integration-tests/expect-patch/patches-custom/left-pad+1.3.0.patch b/integration-tests/expect-patch/patches-custom/left-pad+1.3.0.patch new file mode 100644 index 00000000..0600ef9f --- /dev/null +++ b/integration-tests/expect-patch/patches-custom/left-pad+1.3.0.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 +@@ -4,7 +4,7 @@ + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. */ + 'use strict'; +-module.exports = leftPad; ++module.exports = patch-package; + + var cache = [ + '', diff --git a/integration-tests/expect-patch/yarn.lock b/integration-tests/expect-patch/yarn.lock new file mode 100644 index 00000000..96608840 --- /dev/null +++ b/integration-tests/expect-patch/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +left-pad@^1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e" + integrity sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA== diff --git a/src/applyPatches.ts b/src/applyPatches.ts index 1a50d094..eb362e5b 100644 --- a/src/applyPatches.ts +++ b/src/applyPatches.ts @@ -95,6 +95,7 @@ export function applyPatchesForApp({ appPath, reverse, patchDir, + expectPatch, shouldExitWithError, shouldExitWithWarning, bestEffort, @@ -102,6 +103,7 @@ export function applyPatchesForApp({ appPath: string reverse: boolean patchDir: string + expectPatch: boolean shouldExitWithError: boolean shouldExitWithWarning: boolean bestEffort: boolean @@ -109,14 +111,18 @@ export function applyPatchesForApp({ const patchesDirectory = join(appPath, patchDir) const groupedPatches = getGroupedPatches(patchesDirectory) - if (groupedPatches.numPatchFiles === 0) { - console.log(chalk.blueBright("No patch files found")) - return - } - const errors: string[] = [] const warnings: string[] = [...groupedPatches.warnings] + if (groupedPatches.numPatchFiles === 0) { + if (expectPatch) { + errors.push("No patch files found") + } else { + console.log(chalk.blueBright("No patch files found")) + return + } + } + for (const patches of Object.values( groupedPatches.pathSpecifierToPatchFiles, )) { diff --git a/src/index.ts b/src/index.ts index 8ee449a9..a31c9557 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,6 +25,7 @@ const argv = minimist(process.argv.slice(2), { "error-on-warn", "create-issue", "partial", + "expect-patch", "", ], string: ["patch-dir", "append", "rebase"], @@ -114,11 +115,13 @@ if (argv.version || argv.v) { process.env.NODE_ENV === "test" const shouldExitWithWarning = !!argv["error-on-warn"] + const expectPatch = !!argv["expect-patch"] applyPatchesForApp({ appPath, reverse, patchDir, + expectPatch, shouldExitWithError, shouldExitWithWarning, bestEffort: argv.partial, @@ -177,6 +180,15 @@ Usage: This option was added to help people using CircleCI avoid an issue around caching and patch file updates (https://github.com/ds300/patch-package/issues/37), but might be useful in other contexts too. + + ${chalk.bold("--expect-patch")} + + Prints an error if no patch files were found. + + This option works in tandem with ${chalk.bold( + "--error-on-fail", + )} (enabled by default in CI) to prevent + accidental skips due to patch folder was missed or ignored. For example during COPY in Dockerfile, or in .dockerignore. 2. Creating patch files