Skip to content

Commit

Permalink
Merge pull request #168 from ds300/fix/fail-on-unused-patch-file
Browse files Browse the repository at this point in the history
Fail when patch found for uninstalled package
  • Loading branch information
ds300 authored Sep 6, 2019
2 parents 32ab111 + ffc2327 commit ac708d8
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test fails-when-no-package: no package present failure 1`] = `
"SNAPSHOT: no package present failure
Error: Patch file found for package left-pad which is not present at node_modules/left-pad
error Command failed with exit code 1.
END SNAPSHOT"
`;
11 changes: 11 additions & 0 deletions integration-tests/fails-when-no-package/fails-when-no-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# make sure errors stop the script
set -e

echo "add patch-package"
yarn add $1

(>&2 echo "SNAPSHOT: no package present failure")
if yarn patch-package; then
exit 1
fi
(>&2 echo "END SNAPSHOT")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { runIntegrationTest } from "../runIntegrationTest"
runIntegrationTest({
projectName: "fails-when-no-package",
shouldProduceSnapshots: true,
})
9 changes: 9 additions & 0 deletions integration-tests/fails-when-no-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "fails-when-no-package",
"version": "1.0.0",
"description": "integration test for patch-package",
"main": "index.js",
"author": "",
"license": "ISC",
"dependencies": {}
}
Original file line number Diff line number Diff line change
@@ -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 = [
'',
4 changes: 4 additions & 0 deletions integration-tests/fails-when-no-package/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


13 changes: 13 additions & 0 deletions integration-tests/patches/left-pad+1.1.3.patch
Original file line number Diff line number Diff line change
@@ -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 = [
'',
40 changes: 26 additions & 14 deletions src/applyPatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import chalk from "chalk"
import { getPatchFiles } from "./patchFs"
import { executeEffects } from "./patch/apply"
import { existsSync } from "fs-extra"
import { join, resolve } from "./path"
import { join, resolve, relative } from "./path"
import { posix } from "path"
import {
getPackageDetailsFromPatchFilename,
Expand All @@ -17,6 +17,8 @@ import { readPatch } from "./patch/read"
// see https://github.com/ds300/patch-package/issues/86
const shouldExitPostinstallWithError = isCi || process.env.NODE_ENV === "test"

const exit = () => process.exit(shouldExitPostinstallWithError ? 1 : 0)

function findPatchFiles(patchesDirectory: string): string[] {
if (!existsSync(patchesDirectory)) {
return []
Expand All @@ -33,22 +35,35 @@ function getInstalledPackageVersion({
appPath: string
path: string
pathSpecifier: string
}): string | null {
}): string {
const packageDir = join(appPath, path)
if (!existsSync(packageDir)) {
console.log(
`${chalk.yellow(
"Warning:",
)} Patch file found for package ${posix.basename(pathSpecifier)}` +
` which is not present at ${packageDir}`,
console.error(
`${chalk.red("Error:")} Patch file found for package ${posix.basename(
pathSpecifier,
)}` + ` which is not present at ${relative(".", packageDir)}`,
)

return null
exit()
}

const { version } = require(join(packageDir, "package.json"))
// normalize version for `npm ci`
return semver.valid(version)
const result = semver.valid(version)
if (result === null) {
console.error(
`${chalk.red(
"Error:",
)} Version string '${version}' cannot be parsed from ${join(
packageDir,
"package.json",
)}`,
)

exit()
}

return result as string
}

export function applyPatchesForApp({
Expand Down Expand Up @@ -84,10 +99,6 @@ export function applyPatchesForApp({
pathSpecifier,
})

if (!installedPackageVersion) {
return
}

if (
applyPatch({
patchFilePath: resolve(patchesDirectory, filename) as string,
Expand Down Expand Up @@ -131,7 +142,8 @@ export function applyPatchesForApp({
pathSpecifier,
})
}
process.exit(shouldExitPostinstallWithError ? 1 : 0)

exit()
}
})
}
Expand Down

0 comments on commit ac708d8

Please sign in to comment.