Skip to content

Commit 2c305e8

Browse files
committed
pack: output generated tarball filename
Also, pass the options to `libnpmpack`, or else we end up getting invalid integrity values for remote tarballs for some reason. This fixes CITGM with npm v7.
1 parent d5d5ada commit 2c305e8

2 files changed

Lines changed: 67 additions & 32 deletions

File tree

lib/pack.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const npm = require('./npm.js')
88
const { getContents, logTar } = require('./utils/tar.js')
99

1010
const writeFile = util.promisify(require('fs').writeFile)
11+
const output = require('./utils/output.js')
1112

1213
const completion = require('./utils/completion/none.js')
1314
const usageUtil = require('./utils/usage.js')
@@ -18,20 +19,24 @@ const cmd = (args, cb) => pack(args).then(() => cb()).catch(cb)
1819
const pack = async (args) => {
1920
if (args.length === 0) args = ['.']
2021

21-
const opts = { ...npm.flatOptions }
22-
const { unicode } = opts
22+
const { unicode } = npm.flatOptions
2323

24-
const tarballs = await Promise.all(args.map((arg) => pack_(arg, opts)))
25-
tarballs.forEach(tar => logTar(tar, { log, unicode }))
24+
// clone the opts because pacote mutates it with resolved/integrity
25+
const tarballs = await Promise.all(args.map((arg) =>
26+
pack_(arg, { ...npm.flatOptions })))
2627

27-
return tarballs
28+
for (const tar of tarballs) {
29+
logTar(tar, { log, unicode })
30+
output(tar.filename)
31+
}
2832
}
2933

30-
const pack_ = async (arg, { dryRun }) => {
34+
const pack_ = async (arg, opts) => {
3135
const spec = npa(arg)
32-
const manifest = await pacote.manifest(spec)
36+
const { dryRun } = opts
37+
const manifest = await pacote.manifest(spec, opts)
3338
const filename = `${manifest.name}-${manifest.version}.tgz`
34-
const tarballData = await libpack(arg)
39+
const tarballData = await libpack(arg, opts)
3540
const pkgContents = await getContents(manifest, tarballData)
3641

3742
if (!dryRun) {

test/lib/pack.js

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
1-
const { test } = require('tap')
1+
const t = require('tap')
22
const requireInject = require('require-inject')
33

4-
test('should pack current directory with no arguments', (t) => {
4+
const OUTPUT = []
5+
const output = (...msg) => OUTPUT.push(msg)
6+
7+
const libnpmpackActual = require('libnpmpack')
8+
const libnpmpack = async (spec, opts) => {
9+
if (!opts) {
10+
throw new Error('expected options object')
11+
}
12+
return ''
13+
}
14+
15+
t.afterEach(cb => {
16+
OUTPUT.length = 0
17+
cb()
18+
})
19+
20+
t.test('should pack current directory with no arguments', (t) => {
521
const pack = requireInject('../../lib/pack.js', {
22+
'../../lib/utils/output.js': output,
623
'../../lib/npm.js': {
724
flatOptions: {
825
unicode: false,
926
json: false,
1027
dryRun: false
1128
}
1229
},
13-
'libnpmpack': () => {
14-
t.ok(true, 'libnpmpack is called')
15-
return ''
16-
},
17-
'npmlog': {
18-
'showProgress': () => {},
19-
'clearProgress': () => {}
30+
libnpmpack,
31+
npmlog: {
32+
notice: () => {},
33+
showProgress: () => {},
34+
clearProgress: () => {}
2035
}
2136
})
2237

23-
pack([], () => {
24-
t.end()
38+
return pack([], er => {
39+
if (er) {
40+
throw er
41+
}
42+
const filename = `npm-${require('../../package.json').version}.tgz`
43+
t.strictSame(OUTPUT, [[filename]])
2544
})
2645
})
2746

28-
test('should pack given directory', (t) => {
47+
t.test('should pack given directory', (t) => {
2948
const testDir = t.testdir({
3049
'package.json': JSON.stringify({
3150
name: 'my-cool-pkg',
@@ -34,31 +53,37 @@ test('should pack given directory', (t) => {
3453
})
3554

3655
const pack = requireInject('../../lib/pack.js', {
37-
'../../lib/utils/output.js': () => {},
56+
'../../lib/utils/output.js': output,
3857
'../../lib/npm.js': {
3958
flatOptions: {
4059
unicode: true,
4160
json: true,
4261
dryRun: true
4362
}
4463
},
45-
'libnpmpack': () => '',
46-
'npmlog': {
64+
libnpmpack,
65+
npmlog: {
66+
notice: () => {},
4767
'showProgress': () => {},
4868
'clearProgress': () => {}
4969
}
5070
})
5171

52-
pack([testDir], () => {
53-
t.end()
72+
return pack([testDir], er => {
73+
if (er) {
74+
throw er
75+
}
76+
const filename = 'my-cool-pkg-1.0.0.tgz'
77+
t.strictSame(OUTPUT, [[filename]])
5478
})
5579
})
5680

57-
test('should log pack contents', (t) => {
81+
t.test('should log pack contents', (t) => {
5882
const pack = requireInject('../../lib/pack.js', {
83+
'../../lib/utils/output.js': output,
5984
'../../lib/utils/tar.js': {
60-
'getContents': () => {},
61-
'logTar': () => {
85+
...require('../../lib/utils/tar.js'),
86+
logTar: () => {
6287
t.ok(true, 'logTar is called')
6388
}
6489
},
@@ -69,14 +94,19 @@ test('should log pack contents', (t) => {
6994
dryRun: false
7095
}
7196
},
72-
'libnpmpack': () => '',
73-
'npmlog': {
97+
libnpmpack,
98+
npmlog: {
99+
notice: () => {},
74100
'showProgress': () => {},
75101
'clearProgress': () => {}
76102
}
77103
})
78104

79-
pack([], () => {
80-
t.end()
105+
return pack([], er => {
106+
if (er) {
107+
throw er
108+
}
109+
const filename = `npm-${require('../../package.json').version}.tgz`
110+
t.strictSame(OUTPUT, [[filename]])
81111
})
82112
})

0 commit comments

Comments
 (0)