Skip to content

Commit

Permalink
CLI : Fix package existence check for scoped packages (#3044)
Browse files Browse the repository at this point in the history
Fixes #2999
  • Loading branch information
ahutchings authored and chabou committed May 23, 2018
1 parent 16f163d commit 6923e5b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cli/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,25 @@ 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);
}
});
}

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) => {
Expand Down
44 changes: 44 additions & 0 deletions test/unit/cli-api.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit 6923e5b

Please sign in to comment.