2022-03-24 23:47:29 +09:00
|
|
|
import enquirer from "enquirer";
|
2025-02-22 11:58:23 +08:00
|
|
|
import spawn from "nano-spawn";
|
2022-10-14 17:17:47 +08:00
|
|
|
import { waitForEnter } from "../utils.js";
|
|
|
|
|
|
|
|
|
|
export default async function publishToNpm({ dry }) {
|
|
|
|
|
console.log(`Ready to publish to NPM${dry ? "(--dry-run)" : ""}`);
|
|
|
|
|
|
|
|
|
|
await waitForEnter();
|
|
|
|
|
|
|
|
|
|
const commonArgs = ["publish"];
|
|
|
|
|
if (dry) {
|
|
|
|
|
commonArgs.push("--dry-run");
|
|
|
|
|
}
|
2018-06-05 16:37:52 -03:00
|
|
|
|
2022-03-24 23:47:29 +09:00
|
|
|
const runNpmPublish = async () => {
|
2022-10-14 17:17:47 +08:00
|
|
|
const args = [...commonArgs];
|
|
|
|
|
|
|
|
|
|
if (!dry) {
|
|
|
|
|
const { otp } = await enquirer.prompt({
|
|
|
|
|
type: "input",
|
|
|
|
|
name: "otp",
|
|
|
|
|
message: "Please enter your npm OTP",
|
|
|
|
|
});
|
|
|
|
|
args.push("--otp", otp);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-09 08:38:46 +08:00
|
|
|
await spawn("npm", args, { cwd: "./dist/prettier" });
|
2022-03-24 23:47:29 +09:00
|
|
|
};
|
2022-10-14 17:17:47 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retry "npm publish" when to enter OTP is failed.
|
|
|
|
|
*/
|
2021-06-07 13:07:22 +09:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|