Skip to content

Commit

Permalink
feat(alias): add npm: registry alias spec (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 10, 2018
1 parent 647a0b3 commit ab99f8e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions npa.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ function resolve (name, spec, where, arg) {

if (spec && (isFilespec.test(spec) || /^file:/i.test(spec))) {
return fromFile(res, where)
} else if (spec && /^npm:/i.test(spec)) {
return fromAlias(res, where)
}
if (!HostedGit) HostedGit = require('hosted-git-info')
const hosted = HostedGit.fromUrl(spec, {noGitPlus: true, noCommittish: true})
Expand Down Expand Up @@ -253,6 +255,22 @@ function fromURL (res) {
return res
}

function fromAlias (res, where) {
const subSpec = npa(res.rawSpec.substr(4), where)
if (subSpec.type === 'alias') {
throw new Error('nested aliases not supported')
}
if (!subSpec.registry) {
throw new Error('aliases only work for registry deps')
}
res.subSpec = subSpec
res.registry = true
res.type = 'alias'
res.saveSpec = null
res.fetchSpec = null
return res
}

function fromRegistry (res) {
res.registry = true
const spec = res.rawSpec === '' ? 'latest' : res.rawSpec
Expand Down
28 changes: 28 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ require('tap').test('basic', function (t) {
rawSpec: '=v1.2.3'
},

'foo@npm:bar': {
name: 'foo',
escapedName: 'foo',
type: 'alias',
saveSpec: null,
fetchSpec: null,
raw: 'foo@npm:bar',
rawSpec: 'npm:bar',
subSpec: {
registry: true,
name: 'bar',
escapedName: 'bar',
type: 'tag',
raw: 'bar',
rawSpec: '',
saveSpec: null,
fetchSpec: 'latest'
}
},

'git+ssh://[email protected]/user/foo#1.2.3': {
name: null,
escapedName: null,
Expand Down Expand Up @@ -472,6 +492,14 @@ require('tap').test('basic', function (t) {
npa.resolve('invalid/name', '1.0.0')
}, 'Invalid names throw errrors')

t.throws(() => {
npa('foo@npm:bar@npm:baz')
}, 'nested aliases not supported')

t.throws(() => {
npa('foo@npm:foo/bar')
}, 'aliases only work for registry deps')

t.has(npa.resolve('foo', '^1.2.3', '/test/a/b'), {type: 'range'}, 'npa.resolve')
t.has(npa.resolve('foo', 'file:foo', '/test/a/b'), {type: 'directory', fetchSpec: '/test/a/b/foo'}, 'npa.resolve file:')
t.has(npa.resolve('foo', '../foo/bar', '/test/a/b'), {type: 'directory'}, 'npa.resolve no protocol')
Expand Down

0 comments on commit ab99f8e

Please sign in to comment.