Skip to content

Commit

Permalink
pack: output generated tarball filename
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
isaacs committed Aug 4, 2020
1 parent d5d5ada commit 2c305e8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 32 deletions.
21 changes: 13 additions & 8 deletions lib/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const npm = require('./npm.js')
const { getContents, logTar } = require('./utils/tar.js')

const writeFile = util.promisify(require('fs').writeFile)
const output = require('./utils/output.js')

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

const opts = { ...npm.flatOptions }
const { unicode } = opts
const { unicode } = npm.flatOptions

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

return tarballs
for (const tar of tarballs) {
logTar(tar, { log, unicode })
output(tar.filename)
}
}

const pack_ = async (arg, { dryRun }) => {
const pack_ = async (arg, opts) => {
const spec = npa(arg)
const manifest = await pacote.manifest(spec)
const { dryRun } = opts
const manifest = await pacote.manifest(spec, opts)
const filename = `${manifest.name}-${manifest.version}.tgz`
const tarballData = await libpack(arg)
const tarballData = await libpack(arg, opts)
const pkgContents = await getContents(manifest, tarballData)

if (!dryRun) {
Expand Down
78 changes: 54 additions & 24 deletions test/lib/pack.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
const { test } = require('tap')
const t = require('tap')
const requireInject = require('require-inject')

test('should pack current directory with no arguments', (t) => {
const OUTPUT = []
const output = (...msg) => OUTPUT.push(msg)

const libnpmpackActual = require('libnpmpack')
const libnpmpack = async (spec, opts) => {
if (!opts) {
throw new Error('expected options object')
}
return ''
}

t.afterEach(cb => {
OUTPUT.length = 0
cb()
})

t.test('should pack current directory with no arguments', (t) => {
const pack = requireInject('../../lib/pack.js', {
'../../lib/utils/output.js': output,
'../../lib/npm.js': {
flatOptions: {
unicode: false,
json: false,
dryRun: false
}
},
'libnpmpack': () => {
t.ok(true, 'libnpmpack is called')
return ''
},
'npmlog': {
'showProgress': () => {},
'clearProgress': () => {}
libnpmpack,
npmlog: {
notice: () => {},
showProgress: () => {},
clearProgress: () => {}
}
})

pack([], () => {
t.end()
return pack([], er => {
if (er) {
throw er
}
const filename = `npm-${require('../../package.json').version}.tgz`
t.strictSame(OUTPUT, [[filename]])
})
})

test('should pack given directory', (t) => {
t.test('should pack given directory', (t) => {
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'my-cool-pkg',
Expand All @@ -34,31 +53,37 @@ test('should pack given directory', (t) => {
})

const pack = requireInject('../../lib/pack.js', {
'../../lib/utils/output.js': () => {},
'../../lib/utils/output.js': output,
'../../lib/npm.js': {
flatOptions: {
unicode: true,
json: true,
dryRun: true
}
},
'libnpmpack': () => '',
'npmlog': {
libnpmpack,
npmlog: {
notice: () => {},
'showProgress': () => {},
'clearProgress': () => {}
}
})

pack([testDir], () => {
t.end()
return pack([testDir], er => {
if (er) {
throw er
}
const filename = 'my-cool-pkg-1.0.0.tgz'
t.strictSame(OUTPUT, [[filename]])
})
})

test('should log pack contents', (t) => {
t.test('should log pack contents', (t) => {
const pack = requireInject('../../lib/pack.js', {
'../../lib/utils/output.js': output,
'../../lib/utils/tar.js': {
'getContents': () => {},
'logTar': () => {
...require('../../lib/utils/tar.js'),
logTar: () => {
t.ok(true, 'logTar is called')
}
},
Expand All @@ -69,14 +94,19 @@ test('should log pack contents', (t) => {
dryRun: false
}
},
'libnpmpack': () => '',
'npmlog': {
libnpmpack,
npmlog: {
notice: () => {},
'showProgress': () => {},
'clearProgress': () => {}
}
})

pack([], () => {
t.end()
return pack([], er => {
if (er) {
throw er
}
const filename = `npm-${require('../../package.json').version}.tgz`
t.strictSame(OUTPUT, [[filename]])
})
})

0 comments on commit 2c305e8

Please sign in to comment.