Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: run prepublish scripts of packages installed from Git #5837

Merged
merged 14 commits into from
Dec 25, 2022
6 changes: 6 additions & 0 deletions .changeset/lucky-pugs-sell.md
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).
14 changes: 10 additions & 4 deletions exec/plugin-commands-rebuild/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,14 @@ test('rebuilds specific dependencies', async () => {
'--save-dev',
'@pnpm.e2e/pre-and-postinstall-scripts-example@1.0.0',
'pnpm-e2e/install-scripts-example#b6cfdb8af6f8d5ebc5e7de6831af9d38084d765b',
`--registry=${REGISTRY}`,
`--store-dir=${storeDir}`,
`--cache-dir=${cacheDir}`,
'--ignore-scripts',
])
], {
env: {
npm_config_registry: REGISTRY,
},
})

const modulesManifest = await project.readModulesManifest()
await rebuild.handler({
Expand Down Expand Up @@ -154,11 +157,14 @@ test('rebuild with pending option', async () => {
pnpmBin,
'add',
'pnpm-e2e/install-scripts-example#b6cfdb8af6f8d5ebc5e7de6831af9d38084d765b',
`--registry=${REGISTRY}`,
`--store-dir=${storeDir}`,
`--cache-dir=${cacheDir}`,
'--ignore-scripts',
])
], {
env: {
npm_config_registry: REGISTRY,
},
})

let modules = await project.readModulesManifest()
expect(modules!.pendingBuilds).toStrictEqual([
Expand Down
13 changes: 9 additions & 4 deletions exec/prepare-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
"node": ">=14.6"
},
"scripts": {
"lint": "eslint src/**/*.ts",
"test": "pnpm run compile",
"lint": "eslint src/**/*.ts test/**/*.ts",
"test": "pnpm run compile && pnpm run _test",
"prepublishOnly": "pnpm run compile",
"compile": "tsc --build && pnpm run lint --fix"
"compile": "tsc --build && pnpm run lint --fix",
"_test": "jest"
},
"repository": "https://github.com/pnpm/pnpm/blob/main/exec/prepare-package",
"keywords": [
Expand All @@ -36,7 +37,11 @@
},
"funding": "https://opencollective.com/pnpm",
"devDependencies": {
"@pnpm/prepare-package": "workspace:*"
"@pnpm/prepare": "workspace:*",
"@pnpm/prepare-package": "workspace:*",
"@pnpm/test-fixtures": "workspace:*",
"@pnpm/types": "workspace:*",
"load-json-file": "^6.2.0"
},
"exports": {
".": "./lib/index.js"
Expand Down
40 changes: 33 additions & 7 deletions exec/prepare-package/src/index.ts
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] !== '')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "has-prepublish-script",
"version": "1.0.0",
"scripts": {
"prepublish": "node -e \"process.stdout.write('prepublish')\" | json-append output.json"
},
"devDependencies": {
"json-append": "1.1.1"
}
}
16 changes: 16 additions & 0 deletions exec/prepare-package/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from 'path'
import { preparePackage } from '@pnpm/prepare-package'
import { tempDir } from '@pnpm/prepare'
import { fixtures } from '@pnpm/test-fixtures'
import { sync as loadJsonFile } from 'load-json-file'

const f = fixtures(__dirname)

test('prepare package runs the prepbublish script', async () => {
const tmp = tempDir()
f.copy('has-prepublish-script', tmp)
await preparePackage(tmp)
expect(loadJsonFile(path.join(tmp, 'output.json'))).toStrictEqual([
'prepublish',
])
})
9 changes: 9 additions & 0 deletions exec/prepare-package/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@
"../../__typings__/**/*.d.ts"
],
"references": [
{
"path": "../../__utils__/prepare"
},
{
"path": "../../__utils__/test-fixtures"
},
{
"path": "../../packages/error"
},
{
"path": "../../packages/types"
},
{
"path": "../../pkg-manifest/read-package-json"
}
Expand Down
1 change: 1 addition & 0 deletions pkg-manager/core/test/install/lifecycleScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ test('run prepare script for git-hosted dependencies', async () => {
'install',
'postinstall',
'prepare',
'prepublishOnly',
'preinstall',
'install',
'postinstall',
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

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