import { execFileSync } from "node:child_process"; import { cp, mkdtemp, readdir, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import process from "node:process"; const branch = process.env.DOCS_BRANCH ?? "docs"; const remote = process.env.DOCS_REMOTE ?? "origin"; const sourceDir = path.resolve( process.cwd(), process.env.DOCS_DIST_DIR ?? "apps/docs/dist/client", ); function getDeployRemoteUrl(remoteUrl: string) { if (process.env.DOCS_PUSH_REMOTE_URL) { return process.env.DOCS_PUSH_REMOTE_URL; } if (!URL.canParse(remoteUrl)) { return remoteUrl; } const url = new URL(remoteUrl); if (url.protocol !== "http:" && url.protocol !== "https:") { return remoteUrl; } const pathname = url.pathname.replace(/\/+$/, ""); return `ssh://git@${url.host}${pathname}`; } function run(command: string, args: Array, cwd: string) { execFileSync(command, args, { cwd, stdio: "inherit", }); } async function main() { const tempDir = await mkdtemp(path.join(os.tmpdir(), "hip-ui-docs-")); const publishDir = path.join(tempDir, "site"); const distEntries = await readdir(sourceDir).catch(() => {}); try { if (!distEntries || distEntries.length === 0) { throw new Error( `Docs output not found at "${sourceDir}". Run the docs build before publishing.`, ); } const remoteUrl = execFileSync("git", ["remote", "get-url", remote], { cwd: process.cwd(), encoding: "utf8", }).trim(); const deployRemoteUrl = getDeployRemoteUrl(remoteUrl); const commitSha = execFileSync("git", ["rev-parse", "--short", "HEAD"], { cwd: process.cwd(), encoding: "utf8", }).trim(); await cp(sourceDir, publishDir, { recursive: true }); run("git", ["init"], publishDir); run("git", ["checkout", "--orphan", branch], publishDir); run("git", ["add", "--all"], publishDir); run( "git", [ "-c", "user.name=Cursor", "-c", "user.email=cursor@example.com", "commit", "-m", `Publish docs from ${commitSha}`, ], publishDir, ); run("git", ["remote", "add", "deploy", deployRemoteUrl], publishDir); run("git", ["push", "--force", "deploy", `HEAD:${branch}`], publishDir); } finally { await rm(tempDir, { recursive: true, force: true }); } } // oxlint-disable-next-line unicorn/prefer-top-level-await main().catch((error: unknown) => { console.error(error); process.exitCode = 1; });