Sync public tree to cba0ec1

This commit is contained in:
Michel Paulissen 2026-07-04 17:47:40 +02:00
parent 1d72f20cce
commit 2b291e12b7
2 changed files with 34 additions and 18 deletions

View file

@ -76,12 +76,31 @@ function run(command, args, options = {}) {
function runJson(command, args, options = {}) {
const output = run(command, args, options);
const line = output
.trim()
.split(/\r?\n/)
.filter(Boolean)
.at(-1);
return JSON.parse(line);
return parseJsonOutput(output);
}
function parseJsonOutput(output) {
const trimmed = output.trim();
if (!trimmed) {
throw new Error("expected JSON output, got empty stdout");
}
try {
return JSON.parse(trimmed);
} catch (_) {
// Commands may print progress before their final JSON report. Try each
// trailing block so both compact and pretty-printed JSON are accepted.
}
const lines = trimmed.split(/\r?\n/).filter(Boolean);
for (let index = lines.length - 1; index >= 0; index -= 1) {
try {
return JSON.parse(lines.slice(index).join("\n"));
} catch (_) {
// Keep scanning for the start of the trailing JSON value.
}
}
throw new Error(`could not parse JSON output:\n${trimmed}`);
}
function executable(root, name) {
@ -120,16 +139,13 @@ function waitForJsonLine(child, label) {
}, 120000);
child.stdout.on("data", (chunk) => {
stdout += chunk.toString();
for (const line of stdout.split(/\r?\n/)) {
if (!line.trim()) continue;
try {
const parsed = JSON.parse(line);
clearTimeout(timer);
resolve(parsed);
return;
} catch (_) {
// keep waiting
}
try {
const parsed = parseJsonOutput(stdout);
clearTimeout(timer);
resolve(parsed);
return;
} catch (_) {
// Keep waiting for a complete JSON value.
}
});
child.stderr.on("data", (chunk) => {