Skip to content

Commit b28be31

Browse files
committed
add error-on-fail cli option
1 parent a3572b2 commit b28be31

File tree

9 files changed

+133
-15
lines changed

9 files changed

+133
-15
lines changed

README.md

+22-6
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,27 @@ files.
6161

6262
### Heroku
6363

64-
For `patch-package` to work on Heroku applications, you must specify [`NPM_CONFIG_PRODUCTION=false` or `YARN_PRODUCTION=false`](https://devcenter.heroku.com/articles/nodejs-support#package-installation). See [this issue](https://github.com/ds300/patch-package/issues/130) for more details.
64+
For `patch-package` to work on Heroku applications, you must specify
65+
[`NPM_CONFIG_PRODUCTION=false` or `YARN_PRODUCTION=false`](https://devcenter.heroku.com/articles/nodejs-support#package-installation).
66+
See [this issue](https://github.com/ds300/patch-package/issues/130) for more
67+
details.
6568

6669
### Docker and CI
6770

6871
- If having errors about working directory ("cannot run in wd [...]") when
6972
building in Docker, you might need to adjust configuration in `.npmrc`. See
7073
[#185](https://github.com/ds300/patch-package/issues/185).
71-
- In your `Dockerfile`, remember to copy over the patch files *before* running
74+
- In your `Dockerfile`, remember to copy over the patch files _before_ running
7275
`[npm|yarn] install`
7376
- If you cache `node_modules` rather than running `yarn install` every time,
7477
make sure that the `patches` dir is included in your cache key somehow.
7578
Otherwise if you update a patch then the change may not be reflected on
7679
subsequent CI runs.
77-
78-
E.g., for CircleCI: before loading/saving your cache run `cat patches/* | md5 > patches.hash`
79-
and then update your hash key to include a checksum of that file,
80+
81+
E.g., for CircleCI: before loading/saving your cache run
82+
`cat patches/* | md5 > patches.hash` and then update your hash key to include
83+
a checksum of that file,
8084
`{{ checksum "yarn.lock" }}-{{ checksum "patches.hash" }}`.
81-
8285

8386
## Usage
8487

@@ -154,6 +157,19 @@ Run `patch-package` without arguments to apply all patches in your project.
154157

155158
#### Options
156159

160+
- `--error-on-fail`
161+
162+
Forces patch-package to exit with code 1 after failing.
163+
164+
When running locally patch-package always exits with 0 by default. This
165+
happens even after failing to apply patches because otherwise yarn.lock and
166+
package.json might get out of sync with node_modules, which can be very
167+
confusing.
168+
169+
`--error-on-fail` is **switched on** by default on CI.
170+
171+
See https://github.com/ds300/patch-package/issues/86 for background.
172+
157173
- `--reverse`
158174

159175
Un-applies all patches.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Test error-on-fail: at dev time patch-package fails but returns 0 1`] = `
4+
"SNAPSHOT: at dev time patch-package fails but returns 0
5+
6+
**ERROR** Failed to apply patch for package left-pad at path
7+
8+
node_modules/left-pad
9+
10+
This error was caused because patch-package cannot apply the following patch file:
11+
12+
patches/left-pad+1.3.0.patch
13+
14+
Try removing node_modules and trying again. If that doesn't work, maybe there was
15+
an accidental change made to the patch file? Try recreating it by manually
16+
editing the appropriate files and running:
17+
18+
patch-package left-pad
19+
20+
If that doesn't work, then it's a bug in patch-package, so please submit a bug
21+
report. Thanks!
22+
23+
https://github.com/ds300/patch-package/issues
24+
25+
26+
---
27+
patch-package finished with 1 error(s).
28+
END SNAPSHOT"
29+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# make sure errors stop the script
2+
set -e
3+
4+
echo "add patch-package"
5+
yarn add $1
6+
alias patch-package=./node_modules/.bin/patch-package
7+
8+
export NODE_ENV="development"
9+
export CI=""
10+
11+
(>&2 echo "SNAPSHOT: at dev time patch-package fails but returns 0")
12+
if ! patch-package;
13+
then
14+
exit 1
15+
fi
16+
(>&2 echo "END SNAPSHOT")
17+
18+
echo "adding --error-on-fail forces patch-package to return 1 at dev time"
19+
if patch-package --error-on-fail;
20+
then
21+
exit 1
22+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { runIntegrationTest } from "../runIntegrationTest"
2+
runIntegrationTest({
3+
projectName: "error-on-fail",
4+
shouldProduceSnapshots: true,
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "error-on-fail",
3+
"version": "1.0.0",
4+
"description": "integration test for patch-package",
5+
"main": "index.js",
6+
"author": "",
7+
"license": "ISC",
8+
"dependencies": {
9+
"left-pad": "^1.3.0"
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/node_modules/left-pad/index.js b/node_modules/left-pad/index.js
2+
index e90aec3..4a20773 100644
3+
--- a/node_modules/left-pad/index.js
4+
+++ b/node_modules/left-pad/index.js
5+
@@ -4,7 +4,7 @@
6+
* To Public License, Version 2, as published by Sam Hocevar. See
7+
* http://www.wtfpl.net/ for more details. */
8+
'use strict';
9+
-module.notreal = leftPad;
10+
+module.patchPackage = leftPad;
11+
12+
var cache = [
13+
'',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
left-pad@^1.3.0:
6+
version "1.3.0"
7+
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e"
8+
integrity sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==

src/applyPatches.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
PackageDetails,
1010
} from "./PackageDetails"
1111
import { reversePatch } from "./patch/reverse"
12-
import isCi from "is-ci"
1312
import semver from "semver"
1413
import { readPatch } from "./patch/read"
1514
import { packageIsDevDependency } from "./packageIsDevDependency"
@@ -20,12 +19,6 @@ class PatchApplicationError extends Error {
2019
}
2120
}
2221

23-
// don't want to exit(1) on postinsall locally.
24-
// see https://github.com/ds300/patch-package/issues/86
25-
const shouldExitPostinstallWithError = isCi || process.env.NODE_ENV === "test"
26-
27-
const exit = () => process.exit(shouldExitPostinstallWithError ? 1 : 0)
28-
2922
function findPatchFiles(patchesDirectory: string): string[] {
3023
if (!existsSync(patchesDirectory)) {
3124
return []
@@ -90,10 +83,12 @@ export function applyPatchesForApp({
9083
appPath,
9184
reverse,
9285
patchDir,
86+
shouldExitWithError,
9387
}: {
9488
appPath: string
9589
reverse: boolean
9690
patchDir: string
91+
shouldExitWithError: boolean
9792
}): void {
9893
const patchesDirectory = join(appPath, patchDir)
9994
const files = findPatchFiles(patchesDirectory)
@@ -227,7 +222,7 @@ export function applyPatchesForApp({
227222
}
228223

229224
if (errors.length) {
230-
exit()
225+
process.exit(shouldExitWithError ? 1 : 0)
231226
}
232227
}
233228

src/index.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { detectPackageManager } from "./detectPackageManager"
1010
import { join } from "./path"
1111
import { normalize, sep } from "path"
1212
import slash = require("slash")
13+
import isCi from "is-ci"
1314

1415
const appPath = getAppRootPath()
1516
const argv = minimist(process.argv.slice(2), {
@@ -19,6 +20,7 @@ const argv = minimist(process.argv.slice(2), {
1920
"reverse",
2021
"help",
2122
"version",
23+
"error-on-fail",
2224
],
2325
string: ["patch-dir"],
2426
})
@@ -69,7 +71,11 @@ if (argv.version || argv.v) {
6971
} else {
7072
console.log("Applying patches...")
7173
const reverse = !!argv["reverse"]
72-
applyPatchesForApp({ appPath, reverse, patchDir })
74+
// don't want to exit(1) on postinsall locally.
75+
// see https://github.com/ds300/patch-package/issues/86
76+
const shouldExitWithError =
77+
!!argv["error-on-fail"] || isCi || process.env.NODE_ENV === "test"
78+
applyPatchesForApp({ appPath, reverse, patchDir, shouldExitWithError })
7379
}
7480
}
7581

@@ -94,6 +100,19 @@ Usage:
94100
${chalk.bold("--patch-dir <dirname>")}
95101
96102
Specify the name for the directory in which the patch files are located.
103+
104+
${chalk.bold("--error-on-fail")}
105+
106+
Forces patch-package to exit with code 1 after failing.
107+
108+
When running locally patch-package always exits with 0 by default.
109+
This happens even after failing to apply patches because otherwise
110+
yarn.lock and package.json might get out of sync with node_modules,
111+
which can be very confusing.
112+
113+
--error-on-fail is ${chalk.bold("switched on")} by default on CI.
114+
115+
See https://github.com/ds300/patch-package/issues/86 for background.
97116
98117
${chalk.bold("--reverse")}
99118

0 commit comments

Comments
 (0)