Skip to content

Commit

Permalink
allow for rebuilding by path
Browse files Browse the repository at this point in the history
PR-URL: #2342
Credit: @nlf
Close: #2342
Reviewed-by: @ruyadorno
  • Loading branch information
nlf committed Dec 15, 2020
1 parent d45e181 commit a9c4b15
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/rebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,25 @@ const getFilterFn = args => {
const spec = npa(arg)
if (spec.type === 'tag' && spec.rawSpec === '')
return spec
if (spec.type !== 'range' && spec.type !== 'version')

if (spec.type !== 'range' && spec.type !== 'version' && spec.type !== 'directory')
throw new Error('`npm rebuild` only supports SemVer version/range specifiers')

return spec
})

return node => specs.some(spec => {
const { version } = node.package
if (spec.type === 'directory')
return node.path === spec.fetchSpec

if (spec.name !== node.name)
return false

if (spec.rawSpec === '' || spec.rawSpec === '*')
return true

const { version } = node.package
// TODO: add tests for a package with missing version
return semver.satisfies(version, spec.fetchSpec)
})
}
Expand Down
44 changes: 42 additions & 2 deletions test/lib/rebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,48 @@ t.test('filter by pkg@<range>', t => {
})
})

t.test('filter must be a semver version/range', t => {
rebuild(['b:git+ssh://github.com/npm/arborist'], err => {
t.test('filter by directory', t => {
const path = t.testdir({
node_modules: {
a: {
'index.js': '',
'package.json': JSON.stringify({
name: 'a',
version: '1.0.0',
bin: 'index.js',
}),
},
b: {
'index.js': '',
'package.json': JSON.stringify({
name: 'b',
version: '1.0.0',
bin: 'index.js',
}),
},
},
})

npm.prefix = path

const aBinFile = resolve(path, 'node_modules/.bin/a')
const bBinFile = resolve(path, 'node_modules/.bin/b')
t.throws(() => fs.statSync(aBinFile))
t.throws(() => fs.statSync(bBinFile))

rebuild(['file:node_modules/b'], err => {
if (err)
throw err

t.throws(() => fs.statSync(aBinFile), 'should not link a bin')
t.ok(() => fs.statSync(bBinFile), 'should link filtered pkg bin')

t.end()
})
})

t.test('filter must be a semver version/range, or directory', t => {
rebuild(['git+ssh://github.com/npm/arborist'], err => {
t.match(
err,
/Error: `npm rebuild` only supports SemVer version\/range specifiers/,
Expand Down

0 comments on commit a9c4b15

Please sign in to comment.