Skip to content

Commit

Permalink
fix(pack): "pnpm pack" should work as expected when prepack modifies …
Browse files Browse the repository at this point in the history
…the manifest (#7558)
  • Loading branch information
await-ovo authored and zkochan committed Jan 23, 2024
1 parent b93bc1a commit 3c67269
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/afraid-news-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-publishing": patch
"pnpm": patch
---

`pnpm pack` should work as expected when "prepack" modifies the manifest [#7558](https://github.com/pnpm/pnpm/pull/7558).
3 changes: 2 additions & 1 deletion releasing/plugin-commands-publishing/src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export async function handler (
const dir = entryManifest.publishConfig?.directory
? path.join(opts.dir, entryManifest.publishConfig.directory)
: opts.dir
const manifest = (opts.dir !== dir) ? (await readProjectManifest(dir, opts)).manifest : entryManifest
// always read the latest manifest, as "prepack" or "prepare" script may modify package manifest.
const { manifest } = await readProjectManifest(dir, opts)
if (!manifest.name) {
throw new PnpmError('PACKAGE_NAME_NOT_FOUND', `Package name is not defined in the ${manifestFileName}.`)
}
Expand Down
47 changes: 47 additions & 0 deletions releasing/plugin-commands-publishing/test/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,50 @@ test('pack: should resolve correct files from publishConfig', async () => {
expect(await exists('./package/dist-index.js')).toBeTruthy()
expect(await exists('./package/dist-bin.js')).toBeTruthy()
})

test('pack: modify manifest in prepack script', async () => {
prepare({
name: 'custom-publish-dir',
version: '0.0.0',
main: './src/index.ts',
bin: './src/bin.js',
files: [
'dist',
],
scripts: {
prepack: 'node ./prepack.js',
},
})
fs.mkdirSync('./src')
fs.writeFileSync('./src/index.ts', 'index', 'utf8')
fs.writeFileSync('./src/bin.js', 'bin', 'utf8')
fs.mkdirSync('./dist')
fs.writeFileSync('./dist/index.js', 'index', 'utf8')
fs.writeFileSync('./dist/bin.js', 'bin', 'utf8')
fs.writeFileSync('./prepack.js', `
require('fs').writeFileSync('./package.json',
JSON.stringify({
name: 'custom-publish-dir',
version: '0.0.0',
main: './dist/index.js',
bin: './dist/bin.js',
files: [
'dist'
]
}, null, 2), 'utf8')
`, 'utf8')

await pack.handler({
...DEFAULT_OPTS,
argv: { original: [] },
dir: process.cwd(),
extraBinPaths: [],
packDestination: process.cwd(),
})
await tar.x({ file: 'custom-publish-dir-0.0.0.tgz' })
expect(await exists('./package/src/bin.js')).toBeFalsy()
expect(await exists('./package/src/index.ts')).toBeFalsy()
expect(await exists('./package/package.json')).toBeTruthy()
expect(await exists('./package/dist/index.js')).toBeTruthy()
expect(await exists('./package/dist/bin.js')).toBeTruthy()
})

0 comments on commit 3c67269

Please sign in to comment.