Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the correct repository URL for scoped packages when checking for package existence #3044

Merged
merged 1 commit into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});