Skip to content

Commit ac708d8

Browse files
authored
Merge pull request #168 from ds300/fix/fail-on-unused-patch-file
Fail when patch found for uninstalled package
2 parents 32ab111 + ffc2327 commit ac708d8

File tree

8 files changed

+89
-14
lines changed

8 files changed

+89
-14
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Test fails-when-no-package: no package present failure 1`] = `
4+
"SNAPSHOT: no package present failure
5+
Error: Patch file found for package left-pad which is not present at node_modules/left-pad
6+
error Command failed with exit code 1.
7+
END SNAPSHOT"
8+
`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# make sure errors stop the script
2+
set -e
3+
4+
echo "add patch-package"
5+
yarn add $1
6+
7+
(>&2 echo "SNAPSHOT: no package present failure")
8+
if yarn patch-package; then
9+
exit 1
10+
fi
11+
(>&2 echo "END SNAPSHOT")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { runIntegrationTest } from "../runIntegrationTest"
2+
runIntegrationTest({
3+
projectName: "fails-when-no-package",
4+
shouldProduceSnapshots: true,
5+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "fails-when-no-package",
3+
"version": "1.0.0",
4+
"description": "integration test for patch-package",
5+
"main": "index.js",
6+
"author": "",
7+
"license": "ISC",
8+
"dependencies": {}
9+
}
Lines changed: 13 additions & 0 deletions
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+
@@ -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.exports = leftPad;
10+
+module.exports = patch-package;
11+
12+
var cache = [
13+
'',
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
Lines changed: 13 additions & 0 deletions
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+
@@ -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.exports = leftPad;
10+
+module.exports = patch-package;
11+
12+
var cache = [
13+
'',

src/applyPatches.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import chalk from "chalk"
22
import { getPatchFiles } from "./patchFs"
33
import { executeEffects } from "./patch/apply"
44
import { existsSync } from "fs-extra"
5-
import { join, resolve } from "./path"
5+
import { join, resolve, relative } from "./path"
66
import { posix } from "path"
77
import {
88
getPackageDetailsFromPatchFilename,
@@ -17,6 +17,8 @@ import { readPatch } from "./patch/read"
1717
// see https://github.com/ds300/patch-package/issues/86
1818
const shouldExitPostinstallWithError = isCi || process.env.NODE_ENV === "test"
1919

20+
const exit = () => process.exit(shouldExitPostinstallWithError ? 1 : 0)
21+
2022
function findPatchFiles(patchesDirectory: string): string[] {
2123
if (!existsSync(patchesDirectory)) {
2224
return []
@@ -33,22 +35,35 @@ function getInstalledPackageVersion({
3335
appPath: string
3436
path: string
3537
pathSpecifier: string
36-
}): string | null {
38+
}): string {
3739
const packageDir = join(appPath, path)
3840
if (!existsSync(packageDir)) {
39-
console.log(
40-
`${chalk.yellow(
41-
"Warning:",
42-
)} Patch file found for package ${posix.basename(pathSpecifier)}` +
43-
` which is not present at ${packageDir}`,
41+
console.error(
42+
`${chalk.red("Error:")} Patch file found for package ${posix.basename(
43+
pathSpecifier,
44+
)}` + ` which is not present at ${relative(".", packageDir)}`,
4445
)
4546

46-
return null
47+
exit()
4748
}
4849

4950
const { version } = require(join(packageDir, "package.json"))
5051
// normalize version for `npm ci`
51-
return semver.valid(version)
52+
const result = semver.valid(version)
53+
if (result === null) {
54+
console.error(
55+
`${chalk.red(
56+
"Error:",
57+
)} Version string '${version}' cannot be parsed from ${join(
58+
packageDir,
59+
"package.json",
60+
)}`,
61+
)
62+
63+
exit()
64+
}
65+
66+
return result as string
5267
}
5368

5469
export function applyPatchesForApp({
@@ -84,10 +99,6 @@ export function applyPatchesForApp({
8499
pathSpecifier,
85100
})
86101

87-
if (!installedPackageVersion) {
88-
return
89-
}
90-
91102
if (
92103
applyPatch({
93104
patchFilePath: resolve(patchesDirectory, filename) as string,
@@ -131,7 +142,8 @@ export function applyPatchesForApp({
131142
pathSpecifier,
132143
})
133144
}
134-
process.exit(shouldExitPostinstallWithError ? 1 : 0)
145+
146+
exit()
135147
}
136148
})
137149
}

0 commit comments

Comments
 (0)