// `umtool build --run` (release 36 lane C): the build chain as the // project page runs it -- a job on the running umtool app's runner (POST // /api/report/build), followed from the terminal (its log, polled), cancelled // on Ctrl-C (POST ?cancel=). The app owns the steps, the timeouts, the // process-group kill and the heavy slot; this only asks and watches. Without // `--run` the command prints the chain, as before. // // Plain ESM: the CLI runs under bare node. /** Where the app answers: `--url`, else UMTOOL_URL, else the local default. */ export const umtoolBase = (url, env = process.env) => String(url || env.UMTOOL_URL || "http://localhost:3050").replace(/\/+$/, ""); const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); /** * Start the build and follow it to the end. * * @param {{ base: string, project: string, preset?: string, only?: string | null, skipFetch?: boolean, * replace?: boolean, token?: string | null, pollMs?: number, fetchImpl?: typeof fetch, * log?: (line: string) => void, onJob?: (cancel: () => Promise) => void }} o * @returns {Promise<{ state: string, job: string | null, error: string | null, needsReplace?: boolean }>} */ export async function runBuildOverHttp({ base, project, preset = "fast", only = null, skipFetch = false, replace = false, token = null, pollMs = 1500, fetchImpl = fetch, log = (l) => console.log(l), onJob = () => {}, }) { const headers = { "content-type": "application/json", ...(token ? { authorization: `Bearer ${token}` } : {}) }; const call = async (method, route, body) => { try { const res = await fetchImpl(`${base}${route}`, { method, headers, ...(body !== undefined ? { body: JSON.stringify(body) } : {}) }); return { status: res.status, json: await res.json().catch(() => ({})) }; } catch (err) { return { status: 0, json: { error: `umtool at ${base} did not answer: ${err?.message ?? err}` } }; } }; const started = await call("POST", `/api/report/build${replace ? "?replace=1" : ""}`, { project, preset, ...(only ? { only } : {}), ...(skipFetch ? { skipFetch: true } : {}) }); if (started.status !== 200 || !started.json?.job?.id) { const j = started.json ?? {}; return { state: "refused", job: null, error: String(j.error ?? `HTTP ${started.status}`), ...(j.needsReplace ? { needsReplace: true } : {}) }; } const id = started.json.job.id; log(`job ${id}: ${started.json.job.kind ?? "build"}`); for (const s of started.json.steps ?? []) log(` step: ${s.label}`); onJob(async () => (await call("POST", `/api/report/build?cancel=${encodeURIComponent(id)}`)).json?.cancelled === true); let since = 0; let sinceEvent = 0; let misses = 0; for (;;) { const r = await call("GET", `/api/report/build?job=${encodeURIComponent(id)}&since=${since}&sinceEvent=${sinceEvent}`); if (r.status !== 200) { misses += 1; if (misses >= 40) return { state: "lost", job: id, error: String(r.json?.error ?? `HTTP ${r.status}`) }; await sleep(pollMs); continue; } misses = 0; for (const line of r.json.log ?? []) log(line); since = Number.isFinite(r.json.next) ? r.json.next : since + (r.json.log?.length ?? 0); if (Number.isFinite(r.json.nextEvent)) sinceEvent = r.json.nextEvent; if (r.json.state !== "running") return { state: r.json.state, job: id, error: r.json.error ?? null }; await sleep(pollMs); } }