Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: run prepublish scripts of packages installed from Git
close #5826
- Loading branch information
Showing
5 changed files
with
47 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@pnpm/prepare-package": patch | ||
"pnpm": patch | ||
--- | ||
|
||
Run the prepublish scripts of packages installed from Git [#5826](https://github.com/pnpm/pnpm/issues/5826). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,45 @@ | ||
import path from 'path' | ||
import { PnpmError } from '@pnpm/error' | ||
import { safeReadPackageJsonFromDir } from '@pnpm/read-package-json' | ||
import { PackageScripts } from '@pnpm/types' | ||
import rimraf from '@zkochan/rimraf' | ||
import execa from 'execa' | ||
import preferredPM from 'preferred-pm' | ||
|
||
const PREPARE_SCRIPTS = [ | ||
'preinstall', | ||
'install', | ||
'postinstall', | ||
'prepare', | ||
] | ||
|
||
const PREPUBLISH_SCRIPTS = [ | ||
'prepublish', | ||
'prepublishOnly', | ||
'prepack', | ||
'publish', | ||
'postpublish', | ||
] | ||
|
||
export async function preparePackage (pkgDir: string) { | ||
const manifest = await safeReadPackageJsonFromDir(pkgDir) | ||
if (manifest?.scripts?.prepare != null && manifest.scripts.prepare !== '') { | ||
const pm = (await preferredPM(pkgDir))?.name ?? 'npm' | ||
try { | ||
await execa(pm, ['install'], { cwd: pkgDir }) | ||
} catch (err: any) { // eslint-disable-line | ||
throw new PnpmError('PREPARE_PKG_FAILURE', err.shortMessage ?? err.message) | ||
if (manifest?.scripts == null || !packageShouldBeBuilt(manifest.scripts)) return | ||
const pm = (await preferredPM(pkgDir))?.name ?? 'npm' | ||
try { | ||
await execa(pm, ['install'], { cwd: pkgDir }) | ||
for (const scriptName of PREPUBLISH_SCRIPTS) { | ||
if (manifest.scripts[scriptName] == null || manifest.scripts[scriptName] === '') continue | ||
await execa(pm, ['run', scriptName], { cwd: pkgDir }) | ||
} | ||
await rimraf(path.join(pkgDir, 'node_modules')) | ||
} catch (err: any) { // eslint-disable-line | ||
throw new PnpmError('PREPARE_PKG_FAILURE', err.shortMessage ?? err.message) | ||
} | ||
await rimraf(path.join(pkgDir, 'node_modules')) | ||
} | ||
|
||
function packageShouldBeBuilt (packageScripts: PackageScripts): boolean { | ||
return [ | ||
...PREPUBLISH_SCRIPTS, | ||
...PREPARE_SCRIPTS, | ||
].some((scriptName) => packageScripts[scriptName] != null && packageScripts[scriptName] !== '') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.