|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { spawn } = require("node:child_process"); |
| 4 | + |
| 5 | +const startedAt = process.hrtime.bigint(); |
| 6 | +const command = ["yarn", "workspaces", "foreach", "-Apt", "run", "build"]; |
| 7 | + |
| 8 | +console.log("Build in progress..."); |
| 9 | + |
| 10 | +const child = spawn(command[0], command.slice(1), { |
| 11 | + cwd: process.cwd(), |
| 12 | + env: process.env, |
| 13 | + stdio: ["ignore", "pipe", "pipe"], |
| 14 | +}); |
| 15 | + |
| 16 | +let output = ""; |
| 17 | + |
| 18 | +child.stdout.setEncoding("utf8"); |
| 19 | +child.stderr.setEncoding("utf8"); |
| 20 | +child.stdout.on("data", (chunk) => { |
| 21 | + output += chunk; |
| 22 | +}); |
| 23 | +child.stderr.on("data", (chunk) => { |
| 24 | + output += chunk; |
| 25 | +}); |
| 26 | + |
| 27 | +const fallbackDuration = () => { |
| 28 | + const durationMs = Number((process.hrtime.bigint() - startedAt) / 1_000_000n); |
| 29 | + const seconds = Math.floor(durationMs / 1000); |
| 30 | + const ms = durationMs % 1000; |
| 31 | + |
| 32 | + return `Done in ${seconds}s ${ms}ms`; |
| 33 | +}; |
| 34 | + |
| 35 | +const finalDoneLine = () => { |
| 36 | + const lines = output |
| 37 | + .split(/\r?\n/) |
| 38 | + .map((line) => line.trim()) |
| 39 | + .filter(Boolean); |
| 40 | + |
| 41 | + return lines.findLast((line) => /^Done in \d/.test(line)) ?? fallbackDuration(); |
| 42 | +}; |
| 43 | + |
| 44 | +child.on("close", (code, signal) => { |
| 45 | + const doneLine = finalDoneLine(); |
| 46 | + |
| 47 | + if (code === 0 && signal === null) { |
| 48 | + console.log(doneLine); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + console.error("Build failed."); |
| 53 | + |
| 54 | + const failureOutput = output |
| 55 | + .split(/\r?\n/) |
| 56 | + .filter((line) => !/^Done in \d/.test(line.trim())) |
| 57 | + .join("\n") |
| 58 | + .trim(); |
| 59 | + |
| 60 | + if (failureOutput) { |
| 61 | + console.error(failureOutput); |
| 62 | + } |
| 63 | + |
| 64 | + console.error(""); |
| 65 | + console.error(doneLine); |
| 66 | + process.exit(typeof code === "number" ? code : 1); |
| 67 | +}); |
| 68 | + |
| 69 | +child.on("error", (error) => { |
| 70 | + console.error("Build failed."); |
| 71 | + console.error(error.message); |
| 72 | + console.error(fallbackDuration()); |
| 73 | + process.exit(1); |
| 74 | +}); |
0 commit comments