Skip to content

Commit

Permalink
Merge fc55fba into 8425a96
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 10, 2018
2 parents 8425a96 + fc55fba commit 44036ae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions npa.js
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
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://git@notgithub.com/user/foo#1.2.3': {
name: null,
escapedName: null,
Expand Down Expand Up @@ -449,6 +469,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 44036ae

Please sign in to comment.