Skip to content

Commit

Permalink
Add OTP support for yarn publish (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Walter authored and sindresorhus committed Dec 14, 2018
1 parent 03871b2 commit add1387
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 46 deletions.
42 changes: 5 additions & 37 deletions index.js
Expand Up @@ -45,6 +45,8 @@ module.exports = (input, opts) => {
const runCleanup = opts.cleanup && !opts.yolo;
const runPublish = opts.publish;
const pkg = util.readPkg();
const pkgManager = opts.yarn === true ? 'yarn' : 'npm';
const pkgManagerName = opts.yarn === true ? 'Yarn' : 'npm';

const tasks = new Listr([
{
Expand Down Expand Up @@ -113,11 +115,6 @@ module.exports = (input, opts) => {
{
title: 'Bumping version using Yarn',
enabled: () => opts.yarn === true,
skip: () => {
if (runPublish && !pkg.private) {
return 'Public package: version will be bumped using yarn publish.';
}
},
task: () => exec('yarn', ['version', '--new-version', input])
},
{
Expand All @@ -130,42 +127,13 @@ module.exports = (input, opts) => {
if (runPublish) {
tasks.add([
{
title: 'Publishing package using Yarn',
enabled: () => opts.yarn === true,
skip: () => {
if (pkg.private) {
return 'Private package: not publishing to Yarn.';
}
},
task: () => {
const args = ['publish'];

if (opts.contents) {
args.push(opts.contents);
}

args.push('--new-version', input);

if (opts.tag) {
args.push('--tag', opts.tag);
}

if (opts.publishScoped) {
args.push('--access', 'public');
}

return exec('yarn', args);
}
},
{
title: 'Publishing package using npm',
enabled: () => opts.yarn === false,
title: `Publishing package using ${pkgManagerName}`,
skip: () => {
if (pkg.private) {
return 'Private package: not publishing to npm.';
return `Private package: not publishing to ${pkgManagerName}.`;
}
},
task: (ctx, task) => publish(task, opts)
task: (ctx, task) => publish(pkgManager, task, opts, input)
}
]);
}
Expand Down
22 changes: 13 additions & 9 deletions lib/publish.js
Expand Up @@ -5,13 +5,17 @@ const {throwError, from} = require('rxjs');
const {catchError} = require('rxjs/operators');
const chalk = require('chalk');

const npmPublish = options => {
const pkgPublish = (pkgManager, options, input) => {
const args = ['publish'];

if (options.contents) {
args.push(options.contents);
}

if (options.yarn) {
args.push('--new-version', input);
}

if (options.tag) {
args.push('--tag', options.tag);
}
Expand All @@ -24,30 +28,30 @@ const npmPublish = options => {
args.push('--access', 'public');
}

return execa('npm', args);
return execa(pkgManager, args);
};

const handleError = (task, err, options, message) => {
if (err.stderr.includes('one-time pass')) {
const handleError = (err, pkgManager, task, options, input, message) => {
if (err.stderr.includes('one-time pass') || err.message.includes('user TTY')) {
const {title} = task;
task.title = `${title} ${chalk.yellow('(waiting for input…)')}`;

return listrInput(message || 'Enter OTP:', {
done: otp => {
task.title = title;
return npmPublish(Object.assign({otp}, options));
return pkgPublish(pkgManager, Object.assign({otp}, options), input);
}
}).pipe(
catchError(err => handleError(task, err, options, 'OTP was incorrect, try again:'))
catchError(err => handleError(err, pkgManager, task, options, input, 'OTP was incorrect, try again:'))
);
}

return throwError(err);
};

const publish = (task, options) =>
from(npmPublish(options)).pipe(
catchError(err => handleError(task, err, options))
const publish = (pkgManager, task, options, input) =>
from(pkgPublish(pkgManager, options, input)).pipe(
catchError(err => handleError(err, pkgManager, task, options, input))
);

module.exports = publish;

0 comments on commit add1387

Please sign in to comment.