diff --git a/.changeset/five-teams-find.md b/.changeset/five-teams-find.md new file mode 100644 index 0000000000..fcf47385f4 --- /dev/null +++ b/.changeset/five-teams-find.md @@ -0,0 +1,6 @@ +--- +"@pnpm/plugin-commands-publishing": minor +"pnpm": minor +--- + +Added support for `--dry-run` to the `pack` command [#10301](https://github.com/pnpm/pnpm/issues/10301). diff --git a/releasing/plugin-commands-publishing/src/pack.ts b/releasing/plugin-commands-publishing/src/pack.ts index 5c988b52d3..82c6d55cd3 100644 --- a/releasing/plugin-commands-publishing/src/pack.ts +++ b/releasing/plugin-commands-publishing/src/pack.ts @@ -38,6 +38,7 @@ export function cliOptionsTypes (): Record { out: String, recursive: Boolean, ...pick([ + 'dry-run', 'pack-destination', 'pack-gzip-level', 'json', @@ -57,6 +58,10 @@ export function help (): string { title: 'Options', list: [ + { + description: 'Does everything `pnpm pack` would do except actually writing the tarball to disk.', + name: '--dry-run', + }, { description: 'Directory in which `pnpm pack` will save tarballs. The default is the current working directory.', name: '--pack-destination ', @@ -101,6 +106,7 @@ export type PackOptions = Pick & Pick { const destDir = packDestination ? (path.isAbsolute(packDestination) ? packDestination : path.join(dir, packDestination ?? '.')) : dir - await fs.promises.mkdir(destDir, { recursive: true }) - await packPkg({ - destFile: path.join(destDir, tarballName), - filesMap, - modulesDir: path.join(opts.dir, 'node_modules'), - packGzipLevel: opts.packGzipLevel, - manifest: publishManifest, - bins: [ - ...(await getBinsFromPackageManifest(publishManifest as DependencyManifest, dir)).map(({ path }) => path), - ...(manifest.publishConfig?.executableFiles ?? []) - .map((executableFile) => path.join(dir, executableFile)), - ], - }) - if (!opts.ignoreScripts) { - await _runScriptsIfPresent(['postpack'], entryManifest) + if (!opts.dryRun) { + await fs.promises.mkdir(destDir, { recursive: true }) + await packPkg({ + destFile: path.join(destDir, tarballName), + filesMap, + modulesDir: path.join(opts.dir, 'node_modules'), + packGzipLevel: opts.packGzipLevel, + manifest: publishManifest, + bins: [ + ...(await getBinsFromPackageManifest(publishManifest as DependencyManifest, dir)).map(({ path }) => path), + ...(manifest.publishConfig?.executableFiles ?? []) + .map((executableFile) => path.join(dir, executableFile)), + ], + }) + if (!opts.ignoreScripts) { + await _runScriptsIfPresent(['postpack'], entryManifest) + } } let packedTarballPath if (opts.dir !== destDir) { diff --git a/releasing/plugin-commands-publishing/src/publish.ts b/releasing/plugin-commands-publishing/src/publish.ts index 29670ab6cb..62e240d4aa 100644 --- a/releasing/plugin-commands-publishing/src/publish.ts +++ b/releasing/plugin-commands-publishing/src/publish.ts @@ -272,6 +272,7 @@ Do you want to continue?`, ...opts, dir, packDestination, + dryRun: false, }) await copyNpmrc({ dir, workspaceDir: opts.workspaceDir, packDestination }) const { status } = runNpm(opts.npmPath, ['publish', '--ignore-scripts', path.basename(tarballPath), ...args], { diff --git a/releasing/plugin-commands-publishing/test/pack.ts b/releasing/plugin-commands-publishing/test/pack.ts index 1ab8b9f047..f1bce34a7e 100644 --- a/releasing/plugin-commands-publishing/test/pack.ts +++ b/releasing/plugin-commands-publishing/test/pack.ts @@ -78,6 +78,24 @@ test('pack a package with scoped name', async () => { expect(fs.existsSync('pnpm-test-scope-0.0.0.tgz')).toBeTruthy() }) +test('pack: with dry-run', async () => { + prepare({ + name: 'test-publish-package.json', + version: '0.0.0', + }) + + await pack.handler({ + ...DEFAULT_OPTS, + argv: { original: [] }, + dir: process.cwd(), + extraBinPaths: [], + dryRun: true, + }) + + expect(fs.existsSync('test-publish-package.json-0.0.0.tgz')).toBeFalsy() + expect(fs.existsSync('package.json')).toBeTruthy() +}) + test('pack when there is bundledDependencies but without node-linker=hoisted', async () => { prepare({ name: 'bundled-deps-without-node-linker-hoisted',