ds300 / patch-package

Fix broken node modules instantly 🏃🏽‍♀️💨
MIT License
10.16k stars 283 forks source link

patch-package should support patching itself in `postinstall` script #203

Open pdcastro opened 4 years ago

pdcastro commented 4 years ago

patch-package is great, so much so that I recently found myself attempting to use it to patch patch-package itself as I came across issue #201. It didn't work, at least in my setup, using "postinstall": "patch-package" in the package.json file. What I think was happening is that by the time patch-package had patched itself, it was too late -- the old unpatched code was already being used to patch other packages.

Perhaps the solution might be for patch-package to detect if one of the patch files is for patch-package itself, and if so, apply that patch first, then re-exec itself before continuing with other patches. (When re-exec'ing, perhaps add a command-line option like --no-self-patch to prevent patching itself in a loop.) :-)

mikehardy commented 4 years ago

I am about to do similar (attempt to patch patch-package) and I think this is achievable as-is by modifying your postinstall

Specifically,

  1. make a new patch directory called patch-package-patches
  2. in package.json::postinstall don't just call patch-package, call patch-package --patch-dir patch-package-patches && patch-package

That way your patch-package patches should go in, and should go first, then it will do it's normal run.

Should work?

pdcastro commented 4 years ago

@mikehardy, great suggestion! I wasn't aware of the --patch-dir option: it is not documented by npx patch-package --help, but indeed it is in the source code. I've successfully "solved" issue #201 in my project thanks to your tip. 👍

mikehardy commented 4 years ago

@pdcastro quick! Propose a PR for the help doc and claim the internet points :trophy: before someone else does :smile: https://github.com/ds300/patch-package/blob/master/src/index.ts#L104

pdcastro commented 4 years ago

Propose a PR for the help doc and claim the internet points 🏆

@mikehardy, I think you have a rightful claim for those points, 😄 and I am already collecting lots elsewhere at the moment! 💰

mikehardy commented 4 years ago

Mine all mine, precious internet points https://github.com/ds300/patch-package/pull/225 Cheers :-)