From 959d55f40ce2dfef9eb022c95097951134c8b79d Mon Sep 17 00:00:00 2001 From: Io Klarstrup Date: Wed, 28 Aug 2024 17:19:30 +0200 Subject: [PATCH] chore: Refactor publish-npm script to handle OTP code for publishing --- publish-npm.mjs | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/publish-npm.mjs b/publish-npm.mjs index ce3794c2..c56adf23 100644 --- a/publish-npm.mjs +++ b/publish-npm.mjs @@ -453,12 +453,38 @@ async function publish() { ]); const pubInd = ora(`Publishing ${name}@${tag} ${nextVersion}`).start(); if (!DRY_RUN) { - await run('npm', [ - 'publish', - '--access', - 'public', - `--tag=${tag.startsWith('experimental') ? 'experimental' : tag}` - ]); + async function publish(otpCode) { + try { + await run('npm', [ + 'publish', + '--access', + 'public', + `--tag=${ + tag.startsWith('experimental') + ? 'experimental' + : tag + }`, + otpCode ? `--otp=${otpCode}` : '' + ]); + } catch (error) { + if (error.includes('code EOTP')) { + pubInd.stop(); + const {otp} = await inquirer.prompt([ + { + type: 'input', + name: 'otp', + message: + 'Enter NPM OTP code from your authenticator' + } + ]); + pubInd.start(); + await publish(otp); + } else { + throw error; + } + } + } + await publish(); } pubInd.succeed( `Published ${name}@${