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`