|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Tag a published version of the main ReScript packages with a given dist-tag. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * node scripts/npmRelease.js --version 12.0.1 --tag next |
| 7 | + * node scripts/npmRelease.js --version 12.0.1 --tag latest --otp 123456 |
| 8 | + * |
| 9 | + * - Runs `npm dist-tag add` for every non-private workspace (same as CI publish) |
| 10 | + * reusing the same OTP so you only get prompted once. |
| 11 | + * - Pass `--dry-run` to see the commands without executing them. |
| 12 | + */ |
| 13 | +import process from "node:process"; |
| 14 | +import readline from "node:readline/promises"; |
| 15 | +import { parseArgs } from "node:util"; |
| 16 | +import { npm, yarn } from "../lib_dev/process.js"; |
| 17 | + |
| 18 | +async function promptForOtp(existingOtp) { |
| 19 | + if (existingOtp) { |
| 20 | + return existingOtp; |
| 21 | + } |
| 22 | + const rl = readline.createInterface({ |
| 23 | + input: process.stdin, |
| 24 | + output: process.stdout, |
| 25 | + }); |
| 26 | + const answer = await rl.question("npm one-time password: "); |
| 27 | + rl.close(); |
| 28 | + return answer.trim(); |
| 29 | +} |
| 30 | + |
| 31 | +async function getPublicWorkspaces() { |
| 32 | + const { stdout } = await yarn("workspaces", [ |
| 33 | + "list", |
| 34 | + "--no-private", |
| 35 | + "--json", |
| 36 | + ]); |
| 37 | + return stdout |
| 38 | + .split("\n") |
| 39 | + .filter(Boolean) |
| 40 | + .map(line => JSON.parse(line)) |
| 41 | + .map(entry => entry.name); |
| 42 | +} |
| 43 | + |
| 44 | +async function runDistTag(pkgName, version, tag, otp, dryRun) { |
| 45 | + const spec = `${pkgName}@${version}`; |
| 46 | + const args = ["dist-tag", "add", spec, tag, "--otp", otp]; |
| 47 | + if (dryRun) { |
| 48 | + console.log(`[dry-run] npm ${args.join(" ")}`); |
| 49 | + return; |
| 50 | + } |
| 51 | + console.log(`Tagging ${spec} as ${tag}...`); |
| 52 | + await npm("dist-tag", ["add", spec, tag, "--otp", otp], { |
| 53 | + stdio: "inherit", |
| 54 | + throwOnFail: true, |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +async function main() { |
| 59 | + try { |
| 60 | + const { values } = parseArgs({ |
| 61 | + args: process.argv.slice(2), |
| 62 | + strict: true, |
| 63 | + options: { |
| 64 | + version: { type: "string", short: "v" }, |
| 65 | + tag: { type: "string", short: "t" }, |
| 66 | + otp: { type: "string" }, |
| 67 | + "dry-run": { type: "boolean" }, |
| 68 | + }, |
| 69 | + }); |
| 70 | + if (!values.version || !values.tag) { |
| 71 | + console.error( |
| 72 | + "Usage: node scripts/npmRelease.js --version <version> --tag <tag> [--otp <code>] [--dry-run]", |
| 73 | + ); |
| 74 | + process.exitCode = 1; |
| 75 | + return; |
| 76 | + } |
| 77 | + const workspaces = await getPublicWorkspaces(); |
| 78 | + if (workspaces.length === 0) { |
| 79 | + throw new Error("No public workspaces found."); |
| 80 | + } |
| 81 | + |
| 82 | + const otp = await promptForOtp(values.otp); |
| 83 | + if (!otp) { |
| 84 | + throw new Error("OTP is required to publish dist-tags."); |
| 85 | + } |
| 86 | + for (const workspace of workspaces) { |
| 87 | + await runDistTag( |
| 88 | + workspace, |
| 89 | + values.version, |
| 90 | + values.tag, |
| 91 | + otp, |
| 92 | + Boolean(values["dry-run"]), |
| 93 | + ); |
| 94 | + } |
| 95 | + if (values["dry-run"]) { |
| 96 | + console.log("Dry run complete."); |
| 97 | + } else { |
| 98 | + console.log("All packages tagged successfully."); |
| 99 | + } |
| 100 | + } catch (error) { |
| 101 | + console.error(error.message || error); |
| 102 | + process.exitCode = 1; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +await main(); |
0 commit comments