#!/usr/bin/env node import { mkdir, writeFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { parseSmokeArgs, printSmokeHelp, runDevCloudWorkbenchMobileSmoke, runM3StatusFixtureSmoke, runDevCloudWorkbenchSmoke } from "./src/dev-cloud-workbench-smoke-lib.mjs"; const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); export function decideSmokeReportWrite(args, report, options = {}) { if (!args.reportPath || report.status === "usage") { return { status: "not_requested", write: false }; } const root = options.repoRoot ?? repoRoot; const cwd = options.cwd ?? process.cwd(); const reportPath = path.resolve(cwd, args.reportPath); const liveReportPath = path.resolve(root, "reports/dev-gate/dev-cloud-workbench-live.json"); const displayPath = displayReportPath(reportPath, root); if (report.status === "skip") { return { status: "blocked", write: false, reportPath, summary: `Refusing to write ${displayPath}: ${report.mode ?? "smoke"} skipped and does not contain required browser evidence.` }; } if (reportPath === liveReportPath && report.mode !== "live") { return { status: "blocked", write: false, reportPath, summary: `Refusing to overwrite ${displayPath} with ${report.mode ?? "non-live"} evidence; run --live for the live report or choose a mode-specific report path.` }; } return { status: "pass", write: true, reportPath }; } export function smokeCliExitCode(report, reportWriteDecision = { status: "not_requested" }) { if (reportWriteDecision.status === "blocked") return 2; return report.status === "pass" || report.status === "usage" ? 0 : 2; } if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) { try { const args = parseSmokeArgs(process.argv.slice(2)); const report = args.help ? printSmokeHelp() : args.mobile ? await runDevCloudWorkbenchMobileSmoke() : args.mode === "m3-status-fixture" ? await runM3StatusFixtureSmoke() : await runDevCloudWorkbenchSmoke(process.argv.slice(2)); const reportWrite = decideSmokeReportWrite(args, report); if (reportWrite.write) { await mkdir(path.dirname(reportWrite.reportPath), { recursive: true }); report.artifacts = { ...(report.artifacts ?? {}), reportPath: reportWrite.reportPath }; await writeFile(reportWrite.reportPath, `${JSON.stringify(report, null, 2)}\n`, "utf8"); } else if (reportWrite.status === "blocked") { report.reportWrite = { status: "blocked", reportPath: reportWrite.reportPath, summary: reportWrite.summary }; process.stderr.write(`[dev-cloud-workbench-smoke] ${reportWrite.summary}\n`); } process.stdout.write(`${JSON.stringify(report, null, 2)}\n`); process.exitCode = smokeCliExitCode(report, reportWrite); } catch (error) { process.stderr.write(`[dev-cloud-workbench-smoke] ${error instanceof Error ? error.stack : String(error)}\n`); process.exitCode = 1; } } function displayReportPath(reportPath, root) { const relative = path.relative(root, reportPath); return relative && !relative.startsWith("..") && !path.isAbsolute(relative) ? relative : reportPath; }