import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { spawn } from "node:child_process"; import { repoRoot } from "./core.mjs"; const docsRoot = path.join(repoRoot, "apps", "docs"); const finalOutputDir = path.join(repoRoot, ".artifacts", "storybook-static"); const tempRootDir = path.join(repoRoot, ".artifacts", "storybook-builds"); async function run(command, args, options = {}) { await new Promise((resolve, reject) => { const child = spawn(command, args, { cwd: repoRoot, stdio: "inherit", ...options }); child.on("error", reject); child.on("exit", (code) => { if (code === 0) { resolve(); return; } reject(new Error(`${command} ${args.join(" ")} exited with code ${code ?? "unknown"}`)); }); }); } async function main() { await fs.mkdir(tempRootDir, { recursive: true }); const tempDir = await fs.mkdtemp(path.join(tempRootDir, `${os.hostname().replace(/[^a-zA-Z0-9_-]/g, "-")}-`)); try { await run( "pnpm", [ "exec", "storybook", "build", "--disable-telemetry", "--output-dir", tempDir ], { cwd: docsRoot, env: process.env } ); await fs.rm(finalOutputDir, { recursive: true, force: true }); await fs.rename(tempDir, finalOutputDir); } catch (error) { await fs.rm(tempDir, { recursive: true, force: true }); throw error; } } await main();