镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 83f0c57

Browse files
authored
fix(pkg): pack tarball inside npm publish --dry-run (#1076)
1 parent 1745223 commit 83f0c57

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

‎src/features/pkg/index.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Buffer } from 'node:buffer'
2+
import { mkdtemp, writeFile } from 'node:fs/promises'
3+
import { tmpdir } from 'node:os'
4+
import path from 'node:path'
5+
import { expect, test, vi } from 'vitest'
6+
import { fsRemove } from '../../utils/fs.ts'
7+
import { packTarball } from './index.ts'
8+
9+
const GZIP_MAGIC = Buffer.from([0x1f, 0x8b])
10+
11+
async function writeTempPackage() {
12+
const dir = await mkdtemp(path.join(tmpdir(), 'tsdown-pack-fixture-'))
13+
await Promise.all([
14+
writeFile(
15+
path.join(dir, 'package.json'),
16+
JSON.stringify({
17+
name: 'tsdown-pack-fixture',
18+
version: '0.0.0',
19+
files: ['index.js'],
20+
}),
21+
),
22+
// forces `npm` as the detected package manager
23+
writeFile(
24+
path.join(dir, 'package-lock.json'),
25+
JSON.stringify({ lockfileVersion: 3 }),
26+
),
27+
writeFile(path.join(dir, 'index.js'), 'module.exports = 1\n'),
28+
])
29+
return dir
30+
}
31+
32+
test('packs a tarball', async () => {
33+
const dir = await writeTempPackage()
34+
try {
35+
const tarball = await packTarball(path.join(dir, 'package.json'))
36+
expect(tarball.subarray(0, 2)).toEqual(GZIP_MAGIC)
37+
} finally {
38+
await fsRemove(dir)
39+
}
40+
})
41+
42+
test('packs a tarball inside `npm publish --dry-run`', async () => {
43+
vi.stubEnv('npm_config_dry_run', 'true')
44+
const dir = await writeTempPackage()
45+
try {
46+
const tarball = await packTarball(path.join(dir, 'package.json'))
47+
expect(tarball.subarray(0, 2)).toEqual(GZIP_MAGIC)
48+
} finally {
49+
await fsRemove(dir)
50+
vi.unstubAllEnvs()
51+
}
52+
})

‎src/features/pkg/index.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export async function bundleDone(
116116
ctx.resolve()
117117
}
118118

119-
async function packTarball(
119+
export async function packTarball(
120120
packageJsonPath: string,
121121
): Promise<Buffer<ArrayBuffer>> {
122122
const pkgDir = path.dirname(packageJsonPath)
@@ -250,7 +250,12 @@ async function pack(
250250

251251
const output = await x(command, args, {
252252
nodePath: false,
253-
nodeOptions: { cwd: dir },
253+
nodeOptions: {
254+
cwd: dir,
255+
// `npm publish --dry-run` exports `npm_config_dry_run=true` to lifecycle scripts,
256+
// which would make this nested pack print a file name without writing it.
257+
env: { npm_config_dry_run: undefined },
258+
},
254259
})
255260

256261
// Get first file that ends with `.tgz` in the pack destination.

0 commit comments

Comments
 (0)