Skip to content

Commit

Permalink
Add support for --no-2fa flag (#559)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
Co-authored-by: Pedro Augusto de Paula Barbosa <[email protected]>
  • Loading branch information
3 people authored Aug 2, 2020
1 parent b8b1976 commit 0638d27
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ $ np --help
--no-yarn Don't use Yarn
--contents Subdirectory to publish
--no-release-draft Skips opening a GitHub release draft
--no-2fa Don't enable 2FA on new packages (not recommended)
Examples
$ np
Expand Down Expand Up @@ -93,6 +94,7 @@ Currently, these are the flags you can configure:
- `yarn` - Use yarn if possible (`true` by default).
- `contents` - Subdirectory to publish (`.` by default).
- `releaseDraft` - Open a GitHub release draft after releasing (`true` by default).
- `2fa` - Enable 2FA on new packages (`true` by default) (it's not recommended to set this to `false`).

For example, this configures `np` to never use Yarn and to use `dist` as the subdirectory to publish:

Expand Down
12 changes: 11 additions & 1 deletion source/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const cli = meow(`
--no-yarn Don't use Yarn
--contents Subdirectory to publish
--no-release-draft Skips opening a GitHub release draft
--no-2fa Don't enable 2FA on new packages (not recommended)
Examples
$ np
Expand Down Expand Up @@ -74,6 +75,9 @@ const cli = meow(`
},
preview: {
type: 'boolean'
},
'2fa': {
type: 'boolean'
}
}
});
Expand All @@ -88,7 +92,8 @@ updateNotifier({pkg: cli.pkg}).notify();
tests: true,
publish: true,
releaseDraft: true,
yarn: hasYarn()
yarn: hasYarn(),
'2fa': true
};

const localConfig = await config();
Expand All @@ -99,6 +104,11 @@ updateNotifier({pkg: cli.pkg}).notify();
...cli.flags
};

// Workaround for unintended auto-casing behavior from `meow`.
if ('2Fa' in flags) {
flags['2fa'] = flags['2Fa'];
}

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

const availability = flags.publish ? await isPackageNameAvailable(pkg) : {
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.availability.isAvailable && !options.availability.isUnknown && !pkg.private && !isExternalRegistry) {
if (options['2fa'] && options.availability.isAvailable && !options.availability.isUnknown && !pkg.private && !isExternalRegistry) {
tasks.add([
{
title: 'Enabling two-factor authentication',
Expand Down
28 changes: 28 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,31 @@ test('skip enabling 2FA if the package exists', async t => {

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

test('skip enabling 2FA if the `2fa` option is false', 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: true,
isUnknown: false
},
'2fa': false
}));

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

0 comments on commit 0638d27

Please sign in to comment.