From fc55fba6fbf94a05da205e792be14d9b95da9669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Mon, 9 Apr 2018 17:16:56 -0700 Subject: [PATCH] feat: add npm: registry alias spec --- npa.js | 18 ++++++++++++++++++ test/basic.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/npa.js b/npa.js index dc885b1..4b9a536 100644 --- a/npa.js +++ b/npa.js @@ -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}) @@ -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 diff --git a/test/basic.js b/test/basic.js index 5b6be51..826dae4 100644 --- a/test/basic.js +++ b/test/basic.js @@ -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, @@ -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')