Skip to content

Commit a9c4b15

Browse files
committed
allow for rebuilding by path
PR-URL: #2342 Credit: @nlf Close: #2342 Reviewed-by: @ruyadorno
1 parent d45e181 commit a9c4b15

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

lib/rebuild.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,25 @@ const getFilterFn = args => {
3939
const spec = npa(arg)
4040
if (spec.type === 'tag' && spec.rawSpec === '')
4141
return spec
42-
if (spec.type !== 'range' && spec.type !== 'version')
42+
43+
if (spec.type !== 'range' && spec.type !== 'version' && spec.type !== 'directory')
4344
throw new Error('`npm rebuild` only supports SemVer version/range specifiers')
45+
4446
return spec
4547
})
48+
4649
return node => specs.some(spec => {
47-
const { version } = node.package
50+
if (spec.type === 'directory')
51+
return node.path === spec.fetchSpec
52+
4853
if (spec.name !== node.name)
4954
return false
55+
5056
if (spec.rawSpec === '' || spec.rawSpec === '*')
5157
return true
58+
59+
const { version } = node.package
60+
// TODO: add tests for a package with missing version
5261
return semver.satisfies(version, spec.fetchSpec)
5362
})
5463
}

test/lib/rebuild.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,48 @@ t.test('filter by pkg@<range>', t => {
174174
})
175175
})
176176

177-
t.test('filter must be a semver version/range', t => {
178-
rebuild(['b:git+ssh://github.com/npm/arborist'], err => {
177+
t.test('filter by directory', t => {
178+
const path = t.testdir({
179+
node_modules: {
180+
a: {
181+
'index.js': '',
182+
'package.json': JSON.stringify({
183+
name: 'a',
184+
version: '1.0.0',
185+
bin: 'index.js',
186+
}),
187+
},
188+
b: {
189+
'index.js': '',
190+
'package.json': JSON.stringify({
191+
name: 'b',
192+
version: '1.0.0',
193+
bin: 'index.js',
194+
}),
195+
},
196+
},
197+
})
198+
199+
npm.prefix = path
200+
201+
const aBinFile = resolve(path, 'node_modules/.bin/a')
202+
const bBinFile = resolve(path, 'node_modules/.bin/b')
203+
t.throws(() => fs.statSync(aBinFile))
204+
t.throws(() => fs.statSync(bBinFile))
205+
206+
rebuild(['file:node_modules/b'], err => {
207+
if (err)
208+
throw err
209+
210+
t.throws(() => fs.statSync(aBinFile), 'should not link a bin')
211+
t.ok(() => fs.statSync(bBinFile), 'should link filtered pkg bin')
212+
213+
t.end()
214+
})
215+
})
216+
217+
t.test('filter must be a semver version/range, or directory', t => {
218+
rebuild(['git+ssh://github.com/npm/arborist'], err => {
179219
t.match(
180220
err,
181221
/Error: `npm rebuild` only supports SemVer version\/range specifiers/,

0 commit comments

Comments
 (0)