Skip to content

Commit 00c8feb

Browse files
authored
npm: Support two-factor authentication: ask for a one-time password
Keywords: 2FA, OTP, npm Closes gh-104
1 parent ff3a435 commit 00c8feb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+7835
-5
lines changed

lib/npm.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,17 @@ module.exports = function( Release ) {
6262
return tags;
6363
},
6464

65-
_publishNpm: function() {
65+
// Ask for a one-time password for 2FA-enabled accounts;
66+
// returns OTP as string if provided, undefined otherwise.
67+
_getNpmOtp: async function() {
68+
var otp = await Release.promptFor(
69+
"Enter one-time password if you have 2FA enabled and press Enter.\n" +
70+
"Otherwise, just press Enter." );
71+
72+
return otp || undefined;
73+
},
74+
75+
_publishNpm: async function() {
6676
if ( !Release.npmPublish ) {
6777
return;
6878
}
@@ -71,16 +81,19 @@ module.exports = function( Release ) {
7181

7282
var npmPublish,
7383
newVersion = Release.readPackage().name + "@" + Release.newVersion,
74-
safety = Release.isTest ? " --dry-run" : "",
75-
npmTags = Release.npmTags();
84+
safety = Release.isTest ? "--dry-run" : "",
85+
npmTags = Release.npmTags(),
86+
otp = await Release._getNpmOtp();
7687

7788
if ( Release.isTest ) {
7889
console.log( "Actual release would now publish to npm using:" );
7990
} else {
8091
console.log( "Publishing to npm, running:" );
8192
}
8293

83-
npmPublish = "npm publish" + safety + " --tag " + npmTags.pop();
94+
npmPublish = `npm publish ${ safety } ${
95+
otp ? `--otp ${ otp }` : ""
96+
} --tag ${ npmTags.pop() }`;
8497
console.log( " " + chalk.cyan( npmPublish ) );
8598
Release.exec( npmPublish );
8699

lib/prompt.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

3-
var chalk = require( "chalk" );
3+
var chalk = require( "chalk" ),
4+
{ prompt } = require( "enquirer" );
45

56
module.exports = function( Release ) {
67

@@ -13,6 +14,17 @@ Release.define( {
1314
process.stdin.resume();
1415
},
1516

17+
// prints a given message, provide back the entered string after trimming.
18+
// Also, returns a promise resolving to that string.
19+
promptFor: async function( message ) {
20+
var { input } = await prompt( {
21+
type: "input",
22+
name: "input",
23+
message
24+
} );
25+
return input;
26+
},
27+
1628
confirm: function( fn ) {
1729
console.log( chalk.yellow( "Press enter to continue, or ctrl+c to cancel." ) );
1830
Release.prompt( fn );

node_modules/ansi-colors/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/ansi-colors/README.md

Lines changed: 279 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)