Skip to content

Commit

Permalink
fix: run prepublish scripts of packages installed from Git
Browse files Browse the repository at this point in the history
close #5826
  • Loading branch information
zkochan committed Dec 25, 2022
1 parent 084823f commit ba8e70c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/lucky-pugs-sell.md
@@ -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).
3 changes: 2 additions & 1 deletion exec/prepare-package/package.json
Expand Up @@ -36,7 +36,8 @@
},
"funding": "https://opencollective.com/pnpm",
"devDependencies": {
"@pnpm/prepare-package": "workspace:*"
"@pnpm/prepare-package": "workspace:*",
"@pnpm/types": "workspace:*"
},
"exports": {
".": "./lib/index.js"
Expand Down
40 changes: 33 additions & 7 deletions exec/prepare-package/src/index.ts
@@ -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] !== '')
}
3 changes: 3 additions & 0 deletions exec/prepare-package/tsconfig.json
Expand Up @@ -12,6 +12,9 @@
{
"path": "../../packages/error"
},
{
"path": "../../packages/types"
},
{
"path": "../../pkg-manifest/read-package-json"
}
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ba8e70c

Please sign in to comment.