Skip to content

Commit 4eb3198

Browse files
author
Matthew Vargeson
committed
Add --error-on-warn flag to exit 1 on warnings
1 parent 5c2c92b commit 4eb3198

File tree

8 files changed

+120
-3
lines changed

8 files changed

+120
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Test error-on-warn: at dev time patch-package warns but returns 0 1`] = `
4+
"SNAPSHOT: at dev time patch-package warns but returns 0
5+
6+
Warning: patch-package detected a patch file version mismatch
7+
8+
Don't worry! This is probably fine. The patch was still applied
9+
successfully. Here's the deets:
10+
11+
Patch file created for
12+
13+
14+
15+
applied to
16+
17+
18+
19+
At path
20+
21+
node_modules/left-pad
22+
23+
This warning is just to give you a heads-up. There is a small chance of
24+
breakage even though the patch was applied successfully. Make sure the package
25+
still behaves like you expect (you wrote tests, right?) and then run
26+
27+
patch-package left-pad
28+
29+
to update the version in the patch file name and make this warning go away.
30+
31+
---
32+
patch-package finished with 1 warning(s).
33+
END SNAPSHOT"
34+
`;
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 warns 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-warn forces patch-package to return 1 at dev time"
19+
if patch-package --error-on-warn;
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-warn",
4+
shouldProduceSnapshots: true,
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "error-on-warn",
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.1.3"
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 26f73ff..60f3f56 100644
3+
--- a/node_modules/left-pad/index.js
4+
+++ b/node_modules/left-pad/index.js
5+
@@ -7,7 +7,7 @@
6+
module.exports = leftPad;
7+
8+
var cache = [
9+
- '',
10+
+ "",
11+
' ',
12+
' ',
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+
6+
version "1.1.3"
7+
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.1.3.tgz#612f61c033f3a9e08e939f1caebeea41b6f3199a"
8+
integrity sha1-YS9hwDPzqeCOk58crr7qQbbzGZo=

src/applyPatches.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ export function applyPatchesForApp({
8484
reverse,
8585
patchDir,
8686
shouldExitWithError,
87+
shouldExitWithWarning,
8788
}: {
8889
appPath: string
8990
reverse: boolean
9091
patchDir: string
9192
shouldExitWithError: boolean
93+
shouldExitWithWarning: boolean
9294
}): void {
9395
const patchesDirectory = join(appPath, patchDir)
9496
const files = findPatchFiles(patchesDirectory)
@@ -221,9 +223,15 @@ export function applyPatchesForApp({
221223
)
222224
}
223225

224-
if (errors.length) {
225-
process.exit(shouldExitWithError ? 1 : 0)
226+
if (errors.length && shouldExitWithError) {
227+
process.exit(1)
228+
}
229+
230+
if (warnings.length && shouldExitWithWarning) {
231+
process.exit(1)
226232
}
233+
234+
process.exit(0)
227235
}
228236

229237
export function applyPatch({

src/index.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const argv = minimist(process.argv.slice(2), {
2121
"help",
2222
"version",
2323
"error-on-fail",
24+
"error-on-warn",
2425
"create-issue",
2526
],
2627
string: ["patch-dir"],
@@ -78,7 +79,16 @@ if (argv.version || argv.v) {
7879
// see https://github.com/ds300/patch-package/issues/86
7980
const shouldExitWithError =
8081
!!argv["error-on-fail"] || isCi || process.env.NODE_ENV === "test"
81-
applyPatchesForApp({ appPath, reverse, patchDir, shouldExitWithError })
82+
83+
const shouldExitWithWarning = !!argv["error-on-warn"]
84+
85+
applyPatchesForApp({
86+
appPath,
87+
reverse,
88+
patchDir,
89+
shouldExitWithError,
90+
shouldExitWithWarning,
91+
})
8292
}
8393
}
8494

@@ -116,6 +126,12 @@ Usage:
116126
--error-on-fail is ${chalk.bold("switched on")} by default on CI.
117127
118128
See https://github.com/ds300/patch-package/issues/86 for background.
129+
130+
${chalk.bold("--error-on-warn")}
131+
132+
Forces patch-package to exit with code 1 after warning.
133+
134+
See https://github.com/ds300/patch-package/issues/314 for background.
119135
120136
${chalk.bold("--reverse")}
121137

0 commit comments

Comments
 (0)