From 6923e5bbb3388cc876d7099332eab0a3bb870917 Mon Sep 17 00:00:00 2001 From: Andrew Hutchings Date: Wed, 23 May 2018 17:32:38 -0400 Subject: [PATCH] CLI : Fix package existence check for scoped packages (#3044) Fixes #2999 --- cli/api.js | 13 +++++++++++- test/unit/cli-api.test.js | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 test/unit/cli-api.test.js diff --git a/cli/api.js b/cli/api.js index a3e924ab..e6c7af30 100644 --- a/cli/api.js +++ b/cli/api.js @@ -69,7 +69,7 @@ function save() { } function existsOnNpm(plugin) { - const name = plugin.split('#')[0].split('@')[0]; + const name = getPackageName(plugin); return got.get(registryUrl + name.toLowerCase(), {timeout: 10000, json: true}).then(res => { if (!res.body.versions) { return Promise.reject(res); @@ -77,6 +77,17 @@ function existsOnNpm(plugin) { }); } +function getPackageName(plugin) { + const isScoped = plugin[0] === '@'; + const nameWithoutVersion = plugin.split('#')[0]; + + if (isScoped) { + return '@' + nameWithoutVersion.split('@')[1].replace('/', '%2f'); + } + + return nameWithoutVersion.split('@')[0]; +} + function install(plugin, locally) { const array = locally ? getLocalPlugins() : getPlugins(); return new Promise((resolve, reject) => { diff --git a/test/unit/cli-api.test.js b/test/unit/cli-api.test.js new file mode 100644 index 00000000..68ca771d --- /dev/null +++ b/test/unit/cli-api.test.js @@ -0,0 +1,44 @@ +import test from 'ava'; +const proxyquire = require('proxyquire').noCallThru(); + +test('existsOnNpm() builds the url for non-scoped packages', t => { + let getUrl; + const {existsOnNpm} = proxyquire('../../cli/api', { + got: { + get(url) { + getUrl = url; + return Promise.resolve({ + body: { + versions: [] + } + }); + } + }, + 'registry-url': () => 'https://registry.npmjs.org/' + }); + + return existsOnNpm('pkg').then(() => { + t.is(getUrl, 'https://registry.npmjs.org/pkg'); + }); +}); + +test('existsOnNpm() builds the url for scoped packages', t => { + let getUrl; + const {existsOnNpm} = proxyquire('../../cli/api', { + got: { + get(url) { + getUrl = url; + return Promise.resolve({ + body: { + versions: [] + } + }); + } + }, + 'registry-url': () => 'https://registry.npmjs.org/' + }); + + return existsOnNpm('@scope/pkg').then(() => { + t.is(getUrl, 'https://registry.npmjs.org/@scope%2fpkg'); + }); +});