Skip to content

Commit

Permalink
feat(pack): add pack-gzip-level (#6406)
Browse files Browse the repository at this point in the history
This options allows specifying custom compression level.

close #6393

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
  • Loading branch information
jankaifer and zkochan committed Apr 16, 2023
1 parent 6706a7d commit 32f8e08
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/khaki-bats-learn.md
@@ -0,0 +1,7 @@
---
"@pnpm/plugin-commands-publishing": minor
"@pnpm/config": minor
"pnpm": minor
---

A custom compression level may be specified for the `pnpm pack` command using the `pack-gzip-level` setting [#6393](https://github.com/pnpm/pnpm/issues/6393).
1 change: 1 addition & 0 deletions config/config/src/Config.ts
Expand Up @@ -159,6 +159,7 @@ export interface Config {
dedupePeerDependents?: boolean
patchesDir?: string
ignoreWorkspaceCycles?: boolean
packGzipLevel?: number

registries: Registries
ignoreWorkspaceRootCheck: boolean
Expand Down
1 change: 1 addition & 0 deletions config/config/src/index.ts
Expand Up @@ -85,6 +85,7 @@ export const types = Object.assign({
'npm-path': String,
offline: Boolean,
'only-built-dependencies': [String],
'pack-gzip-level': Number,
'package-import-method': ['auto', 'hardlink', 'clone', 'copy'],
'patches-dir': String,
pnpmfile: String,
Expand Down
9 changes: 7 additions & 2 deletions releasing/plugin-commands-publishing/src/pack.ts
Expand Up @@ -30,6 +30,9 @@ export function rcOptionsTypes () {
export function cliOptionsTypes () {
return {
'pack-destination': String,
...pick([
'pack-gzip-level',
], allTypes),
}
}

Expand All @@ -55,7 +58,7 @@ export function help () {
}

export async function handler (
opts: Pick<UniversalOptions, 'dir'> & Pick<Config, 'ignoreScripts' | 'rawConfig' | 'embedReadme'> & Partial<Pick<Config, 'extraBinPaths' | 'extraEnv'>> & {
opts: Pick<UniversalOptions, 'dir'> & Pick<Config, 'ignoreScripts' | 'rawConfig' | 'embedReadme' | 'packGzipLevel'> & Partial<Pick<Config, 'extraBinPaths' | 'extraEnv'>> & {
argv: {
original: string[]
}
Expand Down Expand Up @@ -110,6 +113,7 @@ export async function handler (
projectDir: dir,
embedReadme: opts.embedReadme,
modulesDir: path.join(opts.dir, 'node_modules'),
packGzipLevel: opts.packGzipLevel,
})
if (!opts.ignoreScripts) {
await _runScriptsIfPresent(['postpack'], entryManifest)
Expand All @@ -133,6 +137,7 @@ async function packPkg (opts: {
projectDir: string
embedReadme?: boolean
modulesDir: string
packGzipLevel?: number
}): Promise<void> {
const {
destFile,
Expand Down Expand Up @@ -160,7 +165,7 @@ async function packPkg (opts: {
pack.entry({ mode, mtime, name }, fs.readFileSync(source))
}
const tarball = fs.createWriteStream(destFile)
pack.pipe(createGzip()).pipe(tarball)
pack.pipe(createGzip({ level: opts.packGzipLevel })).pipe(tarball)
pack.finalize()
return new Promise((resolve, reject) => {
tarball.on('close', () => {
Expand Down
2 changes: 1 addition & 1 deletion releasing/plugin-commands-publishing/src/publish.ts
Expand Up @@ -133,7 +133,7 @@ export async function publish (
engineStrict?: boolean
recursive?: boolean
workspaceDir?: string
} & Pick<Config, 'allProjects' | 'gitChecks' | 'ignoreScripts' | 'publishBranch' | 'embedReadme'>,
} & Pick<Config, 'allProjects' | 'gitChecks' | 'ignoreScripts' | 'publishBranch' | 'embedReadme' | 'packGzipLevel'>,
params: string[]
) {
if (opts.gitChecks !== false && await isGitRepo()) {
Expand Down
29 changes: 29 additions & 0 deletions releasing/plugin-commands-publishing/test/pack.ts
Expand Up @@ -280,3 +280,32 @@ test('pack to custom destination directory', async () => {

expect(output).toBe(path.resolve('custom-dest/custom-dest-0.0.0.tgz'))
})

test('pack: custom pack-gzip-level', async () => {
prepare({
name: 'test-publish-package.json',
version: '0.0.0',
})

const packOpts = {
...DEFAULT_OPTS,
argv: { original: [] },
dir: process.cwd(),
extraBinPaths: [],
}
await pack.handler({
...packOpts,
packGzipLevel: 9,
packDestination: path.resolve('../small'),
})

await pack.handler({
...packOpts,
packGzipLevel: 0,
packDestination: path.resolve('../big'),
})

const tgz1 = fs.statSync(path.resolve('../small/test-publish-package.json-0.0.0.tgz'))
const tgz2 = fs.statSync(path.resolve('../big/test-publish-package.json-0.0.0.tgz'))
expect(tgz1.size).not.toEqual(tgz2.size)
})

0 comments on commit 32f8e08

Please sign in to comment.