Skip to content

Commit c8292f4

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 c8292f4

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

lib/cache.js

+19-21
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,30 @@ with --force.`)
8686
return rimraf(cachePath)
8787
}
8888

89-
// npm cache add <tarball-url>
90-
// npm cache add <pkg> <ver>
91-
// npm cache add <tarball>
92-
// npm cache add <folder>
89+
// npm cache add <tarball-url>...
90+
// npm cache add <pkg> <ver>...
91+
// npm cache add <tarball>...
92+
// npm cache add <folder>...
9393
async add (args) {
9494
const usage = 'Usage:\n' +
95-
' npm cache add <tarball-url>\n' +
96-
' npm cache add <pkg>@<ver>\n' +
97-
' npm cache add <tarball>\n' +
98-
' npm cache add <folder>\n'
95+
' npm cache add <tarball-url>...\n' +
96+
' npm cache add <pkg>@<ver>...\n' +
97+
' npm cache add <tarball>...\n' +
98+
' 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)