Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow for rebuilding by path #2342

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/rebuild.js
Expand Up @@ -39,16 +39,24 @@ 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
nlf marked this conversation as resolved.
Show resolved Hide resolved
return semver.satisfies(version, spec.fetchSpec)
})
}
Expand Down
44 changes: 42 additions & 2 deletions test/lib/rebuild.js
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