8000 Fix 2FA prompt when the package exists (#505) · JavaScriptExpert/np@4a32039 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a32039

Browse files
chinesedfankylemhsindresorhus
authored
Fix 2FA prompt when the package exists (sindresorhus#505)
Co-authored-by: Kyle Holmberg <inbox@kylemh.com> Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 703fd1e commit 4a32039

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

source/cli.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ updateNotifier({pkg: cli.pkg}).notify();
9797

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

100-
const availability = await isPackageNameAvailable(pkg);
100+
const availability = flags.publish ? await isPackageNameAvailable(pkg) : {
101+
isAvailable: false,
102+
isUnknown: false
103+
};
101104

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

source/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ module.exports = async (input = 'patch', options) => {
215215
]);
216216

217217
const isExternalRegistry = npm.isExternalRegistry(pkg);
218-
if (!options.exists && !pkg.private && !isExternalRegistry) {
218+
if (options.availability.isAvailable && !options.availability.isUnknown && !pkg.private && !isExternalRegistry) {
219219
tasks.add([
220220
{
221221
title: 'Enabling two-factor authentication',

test/index.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import test from 'ava';
2+
import sinon from 'sinon';
3+
import proxyquire from 'proxyquire';
24
import np from '../source';
35

46
const defaultOptions = {
57
cleanup: true,
68
tests: true,
79
publish: true,
8-
runPublish: true
10+
runPublish: true,
11+
availability: {
12+
isAvailable: false,
13+
isUnknown: false
14+
}
915
};
1016

1117
test('version is invalid', async t => {
@@ -28,3 +34,30 @@ test('errors on too low version', async t => {
2834
await t.throwsAsync(np('1.0.0', defaultOptions), /New version `1\.0\.0` should be higher than current version `\d+\.\d+\.\d+`/);
2935
await t.throwsAsync(np('1.0.0-beta', defaultOptions), /New version `1\.0\.0-beta` should be higher than current version `\d+\.\d+\.\d+`/);
3036
});
37+
38+
test('skip enabling 2FA if the package exists', async t => {
39+
const enable2faStub = sinon.stub();
40+
41+
const np = proxyquire('../source', {
42+
del: sinon.stub(),
43+
execa: sinon.stub().returns({pipe: sinon.stub()}),
44+
'./prerequisite-tasks': sinon.stub(),
45+
'./git-tasks': sinon.stub(),
46+
'./git-util': {
47+
hasUpstream: sinon.stub().returns(true),
48+
push: sinon.stub()
49+
},
50+
'./npm/enable-2fa': enable2faStub,
51+
'./npm/publish': sinon.stub().returns({pipe: sinon.stub()})
52+
});
53+
54+
await t.notThrowsAsync(np('1.0.0', {
55+
...defaultOptions,
56+
availability: {
57+
isAvailable: false,
58+
isUnknown: false
59+
}
60+
}));
61+
62+
t.true(enable2faStub.notCalled);
63+
});

0 commit comments

Comments
 (0)
0