diff --git a/integration-tests/fails-when-no-package/__snapshots__/fails-when-no-package.test.ts.snap b/integration-tests/fails-when-no-package/__snapshots__/fails-when-no-package.test.ts.snap new file mode 100644 index 00000000..6c19f537 --- /dev/null +++ b/integration-tests/fails-when-no-package/__snapshots__/fails-when-no-package.test.ts.snap @@ -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" +`; diff --git a/integration-tests/fails-when-no-package/fails-when-no-package.sh b/integration-tests/fails-when-no-package/fails-when-no-package.sh new file mode 100755 index 00000000..c187e288 --- /dev/null +++ b/integration-tests/fails-when-no-package/fails-when-no-package.sh @@ -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") diff --git a/integration-tests/fails-when-no-package/fails-when-no-package.test.ts b/integration-tests/fails-when-no-package/fails-when-no-package.test.ts new file mode 100644 index 00000000..fa8ccc19 --- /dev/null +++ b/integration-tests/fails-when-no-package/fails-when-no-package.test.ts @@ -0,0 +1,5 @@ +import { runIntegrationTest } from "../runIntegrationTest" +runIntegrationTest({ + projectName: "fails-when-no-package", + shouldProduceSnapshots: true, +}) diff --git a/integration-tests/fails-when-no-package/package.json b/integration-tests/fails-when-no-package/package.json new file mode 100644 index 00000000..2931fd16 --- /dev/null +++ b/integration-tests/fails-when-no-package/package.json @@ -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": {} +} diff --git a/integration-tests/fails-when-no-package/patches/left-pad+1.1.3.patch b/integration-tests/fails-when-no-package/patches/left-pad+1.1.3.patch new file mode 100644 index 00000000..0600ef9f --- /dev/null +++ b/integration-tests/fails-when-no-package/patches/left-pad+1.1.3.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/fails-when-no-package/yarn.lock b/integration-tests/fails-when-no-package/yarn.lock new file mode 100644 index 00000000..fb57ccd1 --- /dev/null +++ b/integration-tests/fails-when-no-package/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + diff --git a/integration-tests/patches/left-pad+1.1.3.patch b/integration-tests/patches/left-pad+1.1.3.patch new file mode 100644 index 00000000..0600ef9f --- /dev/null +++ b/integration-tests/patches/left-pad+1.1.3.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/src/applyPatches.ts b/src/applyPatches.ts index e21a4971..ed1e59d9 100644 --- a/src/applyPatches.ts +++ b/src/applyPatches.ts @@ -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, @@ -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 [] @@ -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({ @@ -84,10 +99,6 @@ export function applyPatchesForApp({ pathSpecifier, }) - if (!installedPackageVersion) { - return - } - if ( applyPatch({ patchFilePath: resolve(patchesDirectory, filename) as string, @@ -131,7 +142,8 @@ export function applyPatchesForApp({ pathSpecifier, }) } - process.exit(shouldExitPostinstallWithError ? 1 : 0) + + exit() } }) }