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 (#5837)
close #5826
- Loading branch information
Showing
25 changed files
with
166 additions
and
37 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,8 @@ | ||
--- | ||
"@pnpm/prepare-package": major | ||
"@pnpm/git-fetcher": major | ||
"@pnpm/tarball-fetcher": major | ||
"@pnpm/client": major | ||
--- | ||
|
||
A new required option added to the prepare package function: rawConfig. It is needed in order to create a proper environment for the package manager executed during the preparation of a git-hosted dependency. |
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
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 lifecycle from '@pnpm/npm-lifecycle' | ||
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' | ||
import omit from 'ramda/src/omit' | ||
|
||
export async function preparePackage (pkgDir: string) { | ||
const PREPUBLISH_SCRIPTS = [ | ||
'prepublish', | ||
'prepublishOnly', | ||
'prepack', | ||
'publish', | ||
'postpublish', | ||
] | ||
|
||
export async function preparePackage (opts: { rawConfig: object }, 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' | ||
// We can't prepare a package without running its lifecycle scripts. | ||
// An alternative solution could be to throw an exception. | ||
const config = omit(['ignore-scripts'], opts.rawConfig) | ||
const env = lifecycle.makeEnv(manifest, { config }) | ||
const execOpts = { cwd: pkgDir, env, extendEnv: true } | ||
try { | ||
await execa(pm, ['install'], execOpts) | ||
for (const scriptName of PREPUBLISH_SCRIPTS) { | ||
if (manifest.scripts[scriptName] == null || manifest.scripts[scriptName] === '') continue | ||
await execa(pm, ['run', scriptName], execOpts) | ||
} | ||
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', | ||
].some((scriptName) => packageScripts[scriptName] != null && packageScripts[scriptName] !== '') | ||
} |
10 changes: 10 additions & 0 deletions
10
exec/prepare-package/test/__fixtures__/has-prepublish-script/package.json
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,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" | ||
} | ||
} |
Empty file.
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,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({ rawConfig: {} }, tmp) | ||
expect(loadJsonFile(path.join(tmp, 'output.json'))).toStrictEqual([ | ||
'prepublish', | ||
]) | ||
}) |
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
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
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
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
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
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
Oops, something went wrong.