From 4042ed3108b6e59678c640e76e64f1f35d0121af Mon Sep 17 00:00:00 2001 From: David Sheldrick Date: Wed, 3 Mar 2021 11:15:03 +0000 Subject: [PATCH] don't run for dependencies --- README.md | 11 ++++++++--- src/index.ts | 15 +++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ba3bae74..f777056e 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,15 @@ git commit -m "fix brokenFile.js in some-package" ``` Patches created by `patch-package` are automatically and gracefully applied when -you use `npm`(>=5) or `yarn`. +you use `npm` or `yarn`. -No more waiting around for pull requests to be merged and published. No more -forking repos just to fix that one tiny thing preventing your app from working. +No more waiting around for pull requests to be merged and published! No more +forking repos just to change one line! + +ⓘ Is your project a published npm package? If so you should only patch dev +dependencies. Because of the way node_modules folders are managed, patch files +can't be reliably applied for your dependents. `patch-package` won't even +try. ## Set-up diff --git a/src/index.ts b/src/index.ts index a4691a28..066faa9a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,10 +7,11 @@ import { getAppRootPath } from "./getAppRootPath" import { makePatch } from "./makePatch" import { makeRegExp } from "./makeRegExp" import { detectPackageManager } from "./detectPackageManager" -import { join } from "./path" -import { normalize, sep } from "path" +import { join, resolve } from "./path" +import { basename, normalize, sep } from "path" import slash = require("slash") import isCi from "is-ci" +import { existsSync } from "fs" const appPath = getAppRootPath() const argv = minimist(process.argv.slice(2), { @@ -33,6 +34,16 @@ console.log( require(join(__dirname, "../package.json")).version, ) +if ( + basename(resolve(process.cwd(), "../")) === "node_modules" && + existsSync(resolve(process.cwd(), "../../package.json")) +) { + console.log( + `patch-package seems to be running as part of a dependency's postinstall script and will not attempt to apply patches.`, + ) + process.exit(0) +} + if (argv.version || argv.v) { // noop } else if (argv.help || argv.h) {