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(pack): "pnpm pack" should work as expected when prepack modifies the manifest #7558

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)).manifest
zkochan marked this conversation as resolved.
Show resolved Hide resolved
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()
})