Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix publish otp for yarn berry #741

Merged
merged 3 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {asyncExitHook} from 'exit-hook';
import logSymbols from 'log-symbols';
import prerequisiteTasks from './prerequisite-tasks.js';
import gitTasks from './git-tasks.js';
import {getPackagePublishArguments} from './npm/publish.js';
import {getPackagePublishArguments, runPublish} from './npm/publish.js';
import enable2fa, {getEnable2faArguments} from './npm/enable-2fa.js';
import handleNpmError from './npm/handle-npm-error.js';
import releaseTaskHelper from './release-task-helper.js';
Expand Down Expand Up @@ -179,12 +179,12 @@ const np = async (input = 'patch', {packageManager, ...options}, {package_, root
task(context, task) {
let hasError = false;

return from(execa(...getPublishCommand(options)))
return from(runPublish(getPublishCommand(options)))
.pipe(
catchError(error => handleNpmError(error, task, otp => {
context.otp = otp;

return execa(...getPublishCommand({...options, otp}));
return runPublish(getPublishCommand({...options, otp}));
})),
)
.pipe(
Expand Down
6 changes: 5 additions & 1 deletion source/npm/handle-npm-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ const handleNpmError = (error, task, message, executor) => {
}

// `one-time pass` is for npm and `Two factor authentication` is for Yarn.
if (error.stderr.includes('one-time pass') || error.stdout.includes('Two factor authentication')) {
if (
error.stderr.includes('one-time pass') // Npm
|| error.stdout.includes('Two factor authentication') // Yarn v1
|| error.stdout.includes('One-time password:') // Yarn berry
) {
const {title} = task;
task.title = `${title} ${chalk.yellow('(waiting for input…)')}`;

Expand Down
15 changes: 15 additions & 0 deletions source/npm/publish.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {execa} from 'execa';

export const getPackagePublishArguments = options => {
const arguments_ = ['publish'];

Expand All @@ -19,3 +21,16 @@ export const getPackagePublishArguments = options => {

return arguments_;
};

export function runPublish(arguments_) {
const cp = execa(...arguments_);

cp.stdout.on('data', chunk => {
// https://github.com/yarnpkg/berry/blob/a3e5695186f2aec3a68810acafc6c9b1e45191da/packages/plugin-npm/sources/npmHttpUtils.ts#L541
if (chunk.toString('utf8').includes('One-time password:')) {
cp.kill();
}
});

return cp;
}
10 changes: 8 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ test('skip enabling 2FA if the package exists', async t => {
verifyWorkingTreeIsClean: sinon.stub(),
},
'../source/npm/enable-2fa.js': enable2faStub,
'../source/npm/publish.js': sinon.stub().returns({pipe: sinon.stub()}),
'../source/npm/publish.js': {
getPackagePublishArguments: sinon.stub().returns([]),
runPublish: sinon.stub().returns(fakeExecaReturn()),
},
});

await t.notThrowsAsync(npMock('1.0.0', {
Expand Down Expand Up @@ -91,7 +94,10 @@ test('skip enabling 2FA if the `2fa` option is false', async t => {
verifyWorkingTreeIsClean: sinon.stub(),
},
'../source/npm/enable-2fa.js': enable2faStub,
'../source/npm/publish.js': sinon.stub().returns({pipe: sinon.stub()}),
'../source/npm/publish.js': {
getPackagePublishArguments: sinon.stub().returns([]),
runPublish: sinon.stub().returns(fakeExecaReturn()),
},
});

await t.notThrowsAsync(npMock('1.0.0', {
Expand Down