|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Local-friendly release script: |
| 4 | + * - runs build |
| 5 | + * - runs `changeset publish` |
| 6 | + * - if publish fails due to OTP/2FA, prompts for OTP (TTY only) and retries |
| 7 | + * |
| 8 | + * CI should use an npm automation token (no OTP). In non-interactive shells this |
| 9 | + * script will NOT prompt; it will fail with guidance. |
| 10 | + */ |
| 11 | + |
| 12 | +import { spawn } from "node:child_process"; |
| 13 | +import process from "node:process"; |
| 14 | +import { createInterface } from "node:readline/promises"; |
| 15 | + |
| 16 | +function run(cmd, args, { env } = {}) { |
| 17 | + return new Promise((resolve) => { |
| 18 | + const child = spawn(cmd, args, { |
| 19 | + env: { ...process.env, ...(env ?? {}) }, |
| 20 | + stdio: ["inherit", "pipe", "pipe"], |
| 21 | + }); |
| 22 | + |
| 23 | + let combined = ""; |
| 24 | + |
| 25 | + child.stdout.on("data", (buf) => { |
| 26 | + const s = buf.toString(); |
| 27 | + combined += s; |
| 28 | + process.stdout.write(s); |
| 29 | + }); |
| 30 | + |
| 31 | + child.stderr.on("data", (buf) => { |
| 32 | + const s = buf.toString(); |
| 33 | + combined += s; |
| 34 | + process.stderr.write(s); |
| 35 | + }); |
| 36 | + |
| 37 | + child.on("close", (code) => resolve({ code: code ?? 0, output: combined })); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +function looksLikeOtpError(output) { |
| 42 | + // npm commonly uses EOTP; other tooling prints "one-time password" messages. |
| 43 | + return /EOTP\b|one[- ]time password|otp\b/i.test(output); |
| 44 | +} |
| 45 | + |
| 46 | +function looksLikeAuthError(output) { |
| 47 | + return /ENEEDAUTH\b|npm ERR!\s+need auth|not authorized|auth token/i.test(output); |
| 48 | +} |
| 49 | + |
| 50 | +async function promptOtp() { |
| 51 | + const rl = createInterface({ input: process.stdin, output: process.stdout }); |
| 52 | + try { |
| 53 | + const otp = (await rl.question("NPM OTP required. Enter OTP: ")).trim(); |
| 54 | + return otp; |
| 55 | + } finally { |
| 56 | + rl.close(); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +async function main() { |
| 61 | + // Allow passing flags through to `changeset publish` (e.g. --tag next, --dry-run). |
| 62 | + const publishArgs = process.argv.slice(2); |
| 63 | + |
| 64 | + // Build first (same behavior as old `bun run build && changeset publish`). |
| 65 | + const build = await run("bun", ["run", "build"]); |
| 66 | + if (build.code !== 0) process.exit(build.code); |
| 67 | + |
| 68 | + // First publish attempt. |
| 69 | + const first = await run("bunx", ["changeset", "publish", ...publishArgs]); |
| 70 | + if (first.code === 0) return; |
| 71 | + |
| 72 | + // If it's an auth/token issue, fail fast with a helpful message. |
| 73 | + if (looksLikeAuthError(first.output)) { |
| 74 | + console.error( |
| 75 | + "\nPublish failed due to npm auth. Ensure you're logged in (npm login) or set NPM_TOKEN.\n" |
| 76 | + ); |
| 77 | + process.exit(first.code); |
| 78 | + } |
| 79 | + |
| 80 | + // OTP flow: only prompt in interactive terminals and if OTP isn't already set. |
| 81 | + const alreadyHasOtp = !!process.env.NPM_CONFIG_OTP; |
| 82 | + const canPrompt = Boolean(process.stdin.isTTY && process.stdout.isTTY); |
| 83 | + |
| 84 | + if (!alreadyHasOtp && canPrompt && looksLikeOtpError(first.output)) { |
| 85 | + const otp = await promptOtp(); |
| 86 | + if (!otp) { |
| 87 | + console.error("No OTP entered; aborting publish."); |
| 88 | + process.exit(first.code); |
| 89 | + } |
| 90 | + |
| 91 | + const retry = await run( |
| 92 | + "bunx", |
| 93 | + ["changeset", "publish", ...publishArgs], |
| 94 | + { env: { NPM_CONFIG_OTP: otp } } |
| 95 | + ); |
| 96 | + process.exit(retry.code); |
| 97 | + } |
| 98 | + |
| 99 | + if (looksLikeOtpError(first.output) && !canPrompt) { |
| 100 | + console.error( |
| 101 | + "\nPublish failed because npm requires an OTP, but this is a non-interactive shell.\n" + |
| 102 | + 'Re-run locally in a TTY, or set NPM_CONFIG_OTP=123456, or use an npm automation token in CI.\n' |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + process.exit(first.code); |
| 107 | +} |
| 108 | + |
| 109 | +main().catch((err) => { |
| 110 | + console.error(err); |
| 111 | + process.exit(1); |
| 112 | +}); |
| 113 | + |
| 114 | + |
0 commit comments