Skip to content

Commit

Permalink
Fix 2FA prompt when the package exists (#505)
Browse files Browse the repository at this point in the history
Co-authored-by: Kyle Holmberg <[email protected]>
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
3 people authored Apr 27, 2020
1 parent 703fd1e commit 4a32039
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
5 changes: 4 additions & 1 deletion source/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ updateNotifier({pkg: cli.pkg}).notify();

const runPublish = flags.publish && !pkg.private;

const availability = await isPackageNameAvailable(pkg);
const availability = flags.publish ? await isPackageNameAvailable(pkg) : {
isAvailable: false,
isUnknown: false
};

const version = cli.input.length > 0 ? cli.input[0] : false;

Expand Down
2 changes: 1 addition & 1 deletion source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ module.exports = async (input = 'patch', options) => {
]);

const isExternalRegistry = npm.isExternalRegistry(pkg);
if (!options.exists && !pkg.private && !isExternalRegistry) {
if (options.availability.isAvailable && !options.availability.isUnknown && !pkg.private && !isExternalRegistry) {
tasks.add([
{
title: 'Enabling two-factor authentication',
Expand Down
35 changes: 34 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import test from 'ava';
import sinon from 'sinon';
import proxyquire from 'proxyquire';
import np from '../source';

const defaultOptions = {
cleanup: true,
tests: true,
publish: true,
runPublish: true
runPublish: true,
availability: {
isAvailable: false,
isUnknown: false
}
};

test('version is invalid', async t => {
Expand All @@ -28,3 +34,30 @@ test('errors on too low version', async t => {
await t.throwsAsync(np('1.0.0', defaultOptions), /New version `1\.0\.0` should be higher than current version `\d+\.\d+\.\d+`/);
await t.throwsAsync(np('1.0.0-beta', defaultOptions), /New version `1\.0\.0-beta` should be higher than current version `\d+\.\d+\.\d+`/);
});

test('skip enabling 2FA if the package exists', async t => {
const enable2faStub = sinon.stub();

const np = proxyquire('../source', {
del: sinon.stub(),
execa: sinon.stub().returns({pipe: sinon.stub()}),
'./prerequisite-tasks': sinon.stub(),
'./git-tasks': sinon.stub(),
'./git-util': {
hasUpstream: sinon.stub().returns(true),
push: sinon.stub()
},
'./npm/enable-2fa': enable2faStub,
'./npm/publish': sinon.stub().returns({pipe: sinon.stub()})
});

await t.notThrowsAsync(np('1.0.0', {
...defaultOptions,
availability: {
isAvailable: false,
isUnknown: false
}
}));

t.true(enable2faStub.notCalled);
});

0 comments on commit 4a32039

Please sign in to comment.