CLI : Fix package existence check for scoped packages (#3044)

Fixes #2999
This commit is contained in:
Andrew Hutchings 2018-05-23 17:32:38 -04:00 committed by CHaBou
parent 16f163daf3
commit 6923e5bbb3
2 changed files with 56 additions and 1 deletions

View file

@ -69,7 +69,7 @@ function save() {
} }
function existsOnNpm(plugin) { 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 => { return got.get(registryUrl + name.toLowerCase(), {timeout: 10000, json: true}).then(res => {
if (!res.body.versions) { if (!res.body.versions) {
return Promise.reject(res); 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) { function install(plugin, locally) {
const array = locally ? getLocalPlugins() : getPlugins(); const array = locally ? getLocalPlugins() : getPlugins();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

44
test/unit/cli-api.test.js Normal file
View file

@ -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');
});
});