|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { existsSync, readFileSync, renameSync } from "node:fs"; |
| 3 | +import { createRequire } from "node:module"; |
| 4 | + |
| 5 | +import { resolvePrereleaseTag } from "./publish-state.mjs"; |
| 6 | + |
| 7 | +const expectedRef = "refs/heads/main"; |
| 8 | + |
| 9 | +if ( |
| 10 | + process.env.GITHUB_ACTIONS !== "true" || |
| 11 | + process.env.GITHUB_REF !== expectedRef || |
| 12 | + typeof process.env.GITHUB_SHA !== "string" |
| 13 | +) { |
| 14 | + throw new Error(`Publishing is restricted to GitHub Actions on ${expectedRef}`); |
| 15 | +} |
| 16 | + |
| 17 | +const gitStatus = spawnSync("git", ["status", "--porcelain"], { |
| 18 | + encoding: "utf8", |
| 19 | +}); |
| 20 | + |
| 21 | +if (gitStatus.error) { |
| 22 | + throw new Error("Could not inspect the git worktree", { |
| 23 | + cause: gitStatus.error, |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +if (gitStatus.status !== 0) { |
| 28 | + throw new Error(`git status exited with status ${gitStatus.status ?? "unknown"}`); |
| 29 | +} |
| 30 | + |
| 31 | +if (gitStatus.stdout.trim().length !== 0) { |
| 32 | + throw new Error("Refusing to publish from a dirty git worktree"); |
| 33 | +} |
| 34 | + |
| 35 | +const headResult = spawnSync("git", ["rev-parse", "HEAD"], { |
| 36 | + encoding: "utf8", |
| 37 | +}); |
| 38 | + |
| 39 | +if (headResult.error) { |
| 40 | + throw new Error("Could not resolve the git commit", { |
| 41 | + cause: headResult.error, |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +if (headResult.status !== 0) { |
| 46 | + throw new Error(`git rev-parse exited with status ${headResult.status ?? "unknown"}`); |
| 47 | +} |
| 48 | + |
| 49 | +if (headResult.stdout.trim() !== process.env.GITHUB_SHA) { |
| 50 | + throw new Error("Git HEAD does not match the GitHub Actions commit"); |
| 51 | +} |
| 52 | + |
| 53 | +const packageJson = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")); |
| 54 | +const preStateUrl = new URL("../.changeset/pre.json", import.meta.url); |
| 55 | +const hiddenPreStateUrl = new URL("../.changeset/pre.json.publish", import.meta.url); |
| 56 | +const preState = existsSync(preStateUrl) |
| 57 | + ? JSON.parse(readFileSync(preStateUrl, "utf8")) |
| 58 | + : undefined; |
| 59 | +const prereleaseTag = resolvePrereleaseTag(packageJson.version, preState); |
| 60 | +const require = createRequire(import.meta.url); |
| 61 | +const changesetsBin = require.resolve("@changesets/cli/bin.js"); |
| 62 | +const publishArguments = [changesetsBin, "publish"]; |
| 63 | + |
| 64 | +if (prereleaseTag !== undefined) { |
| 65 | + if (existsSync(hiddenPreStateUrl)) { |
| 66 | + throw new Error("Refusing to overwrite a hidden Changesets pre-state file"); |
| 67 | + } |
| 68 | + |
| 69 | + publishArguments.push("--tag", prereleaseTag); |
| 70 | + renameSync(preStateUrl, hiddenPreStateUrl); |
| 71 | +} |
| 72 | + |
| 73 | +let publishResult; |
| 74 | + |
| 75 | +try { |
| 76 | + // Changesets forces `latest` while a package has only prerelease history and |
| 77 | + // rejects an explicit tag in pre mode. The validated pre-state is hidden only |
| 78 | + // for this command so Changesets can publish to that same configured tag. |
| 79 | + publishResult = spawnSync(process.execPath, publishArguments, { |
| 80 | + stdio: "inherit", |
| 81 | + }); |
| 82 | +} finally { |
| 83 | + if (prereleaseTag !== undefined && existsSync(hiddenPreStateUrl)) { |
| 84 | + renameSync(hiddenPreStateUrl, preStateUrl); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +if (publishResult.error) { |
| 89 | + throw new Error("Could not start Changesets publish", { |
| 90 | + cause: publishResult.error, |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +if (publishResult.signal !== null) { |
| 95 | + throw new Error(`Changesets publish terminated with ${publishResult.signal}`); |
| 96 | +} |
| 97 | + |
| 98 | +process.exitCode = publishResult.status ?? 1; |
0 commit comments