Skip to content

Commit 701627c

Browse files
mjsir911wraithgar
authored andcommitted
feat(cache): Allow 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]> PR-URL: #3098 Credit: @mjsir911 Close: #3098 Reviewed-by: @wraithgar
1 parent 99ff40d commit 701627c

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

docs/content/commands/npm-cache.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ description: Manipulates packages cache
77
### Synopsis
88

99
```bash
10-
npm cache add <tarball file>
11-
npm cache add <folder>
12-
npm cache add <tarball url>
13-
npm cache add <name>@<version>
10+
npm cache add <tarball file>...
11+
npm cache add <folder>...
12+
npm cache add <tarball url>...
13+
npm cache add <name>@<version>...
1414

1515
npm cache clean
1616
aliases: npm cache clear, npm cache rm
@@ -25,7 +25,7 @@ Note: This command is unaware of workspaces.
2525
Used to add, list, or clean the npm cache folder.
2626

2727
* add:
28-
Add the specified package to the local cache. This command is primarily
28+
Add the specified packages to the local cache. This command is primarily
2929
intended to be used internally by npm, but it can provide a way to
3030
add data to the local installation cache explicitly.
3131

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)