From 321f73618373a96531676c46d8533d55d6219cfc Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Mon, 7 Jun 2021 13:07:22 +0900 Subject: [PATCH] Retry "npm publish" when to enter OTP is faeild (#11031) * Retry "npm publish" when to enter OTP is faeild * Fix else * Break * Use async function * Update cspell.json * Address review * Update scripts/release/steps/publish-to-npm.js Co-authored-by: fisker Cheung * Update scripts/release/steps/publish-to-npm.js Co-authored-by: fisker Cheung Co-authored-by: fisker Cheung --- cspell.json | 1 + scripts/release/steps/publish-to-npm.js | 30 +++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/cspell.json b/cspell.json index d7a43f52f..5daec9eba 100644 --- a/cspell.json +++ b/cspell.json @@ -108,6 +108,7 @@ "elektronik", "Eneman", "ENOENT", + "EOTP", "eqeqeq", "Ericsburgh", "errored", diff --git a/scripts/release/steps/publish-to-npm.js b/scripts/release/steps/publish-to-npm.js index 61f165fbf..73db7fe5a 100644 --- a/scripts/release/steps/publish-to-npm.js +++ b/scripts/release/steps/publish-to-npm.js @@ -5,18 +5,34 @@ const { string: outdentString } = require("outdent"); const execa = require("execa"); const { logPromise, waitForEnter } = require("../utils"); +/** + * Retry "npm publish" when to enter OTP is failed. + */ +async function retryNpmPublish() { + const runNpmPublish = () => + execa("npm", ["publish"], { + cwd: "./dist", + stdio: "inherit", // we need to input OTP if 2FA enabled + }); + for (let i = 5; i > 0; i--) { + try { + return await runNpmPublish(); + } catch (error) { + if (error.code === "EOTP" && i > 0) { + console.log(`To enter OTP is failed, you can retry it ${i} times.`); + continue; + } + throw error; + } + } +} + module.exports = async function ({ dry, version }) { if (dry) { return; } - await logPromise( - "Publishing to npm", - execa("npm", ["publish"], { - cwd: "./dist", - stdio: "inherit", // we need to input OTP if 2FA enabled - }) - ); + await logPromise("Publishing to npm", retryNpmPublish()); console.log( outdentString(chalk`