Skip to content

Commit e914772

Browse files
committed
Allow cache add to accept multiple specs
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]>
1 parent a4e7f4e commit e914772

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

lib/cache.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,19 @@ with --force.`)
9797
' npm cache add <tarball>\n' +
9898
' npm cache add <folder>\n'
9999
log.silly('cache add', 'args', args)
100-
const spec = args[0] && args[0] +
101-
(args[1] === undefined || args[1] === null ? '' : `@${args[1]}`)
102-
103-
if (!spec)
100+
if (args.length === 0)
104101
throw Object.assign(new Error(usage), { code: 'EUSAGE' })
105102

106-
log.silly('cache add', 'spec', spec)
107-
108-
// we ask pacote for the thing, and then just throw the data
109-
// away so that it tee-pipes it into the cache like it does
110-
// for a normal request.
111-
await pacote.tarball.stream(spec, stream => {
112-
stream.resume()
113-
return stream.promise()
114-
}, this.npm.flatOptions)
103+
return Promise.all(args.map(spec => {
104+
log.silly('cache add', 'spec', spec)
105+
// we ask pacote for the thing, and then just throw the data
106+
// away so that it tee-pipes it into the cache like it does
107+
// for a normal request.
108+
return pacote.tarball.stream(spec, stream => {
109+
stream.resume()
110+
return stream.promise()
111+
}, this.npm.flatOptions)
112+
}))
115113
}
116114

117115
async verify () {

test/lib/cache.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,21 @@ t.test('cache add pkg only', t => {
134134
})
135135
})
136136

137-
t.test('cache add pkg w/ spec modifier', t => {
137+
t.test('cache add multiple pkgs', t => {
138138
t.teardown(() => {
139139
logOutput = []
140140
tarballStreamSpec = ''
141141
tarballStreamOpts = {}
142142
})
143143

144-
cache.exec(['add', 'mypkg', 'latest'], err => {
144+
cache.exec(['add', 'mypkg', 'anotherpkg'], err => {
145145
t.error(err)
146146
t.strictSame(logOutput, [
147-
['silly', 'cache add', 'args', ['mypkg', 'latest']],
148-
['silly', 'cache add', 'spec', 'mypkg@latest'],
147+
['silly', 'cache add', 'args', ['mypkg', 'anotherpkg']],
148+
['silly', 'cache add', 'spec', 'mypkg'],
149+
['silly', 'cache add', 'spec', 'anotherpkg'],
149150
], 'logs correctly')
150-
t.equal(tarballStreamSpec, 'mypkg@latest', 'passes the correct spec to pacote')
151+
t.equal(tarballStreamSpec, 'anotherpkg', 'passes the correct spec to pacote')
151152
t.same(tarballStreamOpts, npm.flatOptions, 'passes the correct options to pacote')
152153
t.end()
153154
})

0 commit comments

Comments
 (0)