Files
osmWorkflow/scripts/lib/road-compiler-cli.js

48 lines
2.3 KiB
JavaScript

"use strict";
const fs = require("fs");
const path = require("path");
const { spawnSync } = require("child_process");
const { toRoadCompilerInput } = require("./area-config");
function compilerCli() {
const packageFile = require.resolve("@osm-asset/road-compiler/package.json");
const packageRoot = path.dirname(packageFile);
const manifest = require(packageFile);
return path.join(packageRoot, typeof manifest.bin === "string" ? manifest.bin : manifest.bin["road-compiler"]);
}
function writeInput(area) {
const input = toRoadCompilerInput(area);
fs.mkdirSync(area.outputs.pipelineDir, { recursive: true });
const file = path.join(area.outputs.pipelineDir, "road-compiler.input.json");
fs.writeFileSync(file, `${JSON.stringify(input, null, 2)}\n`);
return { file, input };
}
function parseCompletionMarker(stdout, input) {
const lines = String(stdout).split(/\r?\n/).filter((line) => line.startsWith("NATIVE_ROAD_COMPILE_DONE "));
if (lines.length !== 1) throw new Error(`Expected exactly one NATIVE_ROAD_COMPILE_DONE marker, found ${lines.length}`);
let marker;
try { marker = JSON.parse(lines[0].slice("NATIVE_ROAD_COMPILE_DONE ".length)); }
catch (error) { throw new Error(`Invalid NATIVE_ROAD_COMPILE_DONE JSON: ${error.message}`); }
if (!marker || typeof marker !== "object") throw new Error("NATIVE_ROAD_COMPILE_DONE payload must be an object");
if (marker.areaId !== input.areaId) throw new Error(`NATIVE_ROAD_COMPILE_DONE areaId mismatch: expected ${input.areaId}, got ${marker.areaId}`);
if (path.resolve(marker.output || "") !== path.resolve(input.outDir)) throw new Error(`NATIVE_ROAD_COMPILE_DONE output mismatch: expected ${input.outDir}, got ${marker.output}`);
return marker;
}
function compileArea(area) {
const { file, input } = writeInput(area);
const result = spawnSync(process.execPath, [compilerCli(), "--input", file], { encoding: "utf8", stdio: ["ignore", "pipe", "inherit"] });
if (result.stdout) process.stdout.write(result.stdout);
if (result.error) throw result.error;
if (result.status !== 0) {
const signal = result.signal ? ` signal=${result.signal}` : "";
throw new Error(`Native road compiler failed with status=${result.status}${signal}`);
}
return parseCompletionMarker(result.stdout, input);
}
module.exports = { compileArea, compilerCli, parseCompletionMarker, writeInput };