Skip to content

Commit

Permalink
Allow cache add to accept multiple specs
Browse files Browse the repository at this point in the history
This is a backwards incompatible change to the undocumented
`cache add pkg version`, but

Motivations for this can be found here:
#2976 (comment)

Signed-off-by: Marco Sirabella <[email protected]>
  • Loading branch information
mjsir911 committed Apr 18, 2021
1 parent a4e7f4e commit e914772
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
24 changes: 11 additions & 13 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,19 @@ with --force.`)
' npm cache add <tarball>\n' +
' npm cache add <folder>\n'
log.silly('cache add', 'args', args)
const spec = args[0] && args[0] +
(args[1] === undefined || args[1] === null ? '' : `@${args[1]}`)

if (!spec)
if (args.length === 0)
throw Object.assign(new Error(usage), { code: 'EUSAGE' })

log.silly('cache add', 'spec', spec)

// we ask pacote for the thing, and then just throw the data
// away so that it tee-pipes it into the cache like it does
// for a normal request.
await pacote.tarball.stream(spec, stream => {
stream.resume()
return stream.promise()
}, this.npm.flatOptions)
return Promise.all(args.map(spec => {
log.silly('cache add', 'spec', spec)
// we ask pacote for the thing, and then just throw the data
// away so that it tee-pipes it into the cache like it does
// for a normal request.
return pacote.tarball.stream(spec, stream => {
stream.resume()
return stream.promise()
}, this.npm.flatOptions)
}))
}

async verify () {
Expand Down
11 changes: 6 additions & 5 deletions test/lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,21 @@ t.test('cache add pkg only', t => {
})
})

t.test('cache add pkg w/ spec modifier', t => {
t.test('cache add multiple pkgs', t => {
t.teardown(() => {
logOutput = []
tarballStreamSpec = ''
tarballStreamOpts = {}
})

cache.exec(['add', 'mypkg', 'latest'], err => {
cache.exec(['add', 'mypkg', 'anotherpkg'], err => {
t.error(err)
t.strictSame(logOutput, [
['silly', 'cache add', 'args', ['mypkg', 'latest']],
['silly', 'cache add', 'spec', 'mypkg@latest'],
['silly', 'cache add', 'args', ['mypkg', 'anotherpkg']],
['silly', 'cache add', 'spec', 'mypkg'],
['silly', 'cache add', 'spec', 'anotherpkg'],
], 'logs correctly')
t.equal(tarballStreamSpec, 'mypkg@latest', 'passes the correct spec to pacote')
t.equal(tarballStreamSpec, 'anotherpkg', 'passes the correct spec to pacote')
t.same(tarballStreamOpts, npm.flatOptions, 'passes the correct options to pacote')
t.end()
})
Expand Down

0 comments on commit e914772

Please sign in to comment.