import { Buffer } from "node:buffer"; import fs from "node:fs"; import path from "node:path"; import { pathToFileURL } from "node:url"; import { inspect } from "node:util"; import { checkView } from "./frontends/view.ts"; import { checkLeanView } from "./lean/frontends/view.ts"; import { compileLean } from "./lean/pipeline.ts"; import { extractFacts } from "./lean/run.ts"; import { compile } from "./pipeline.ts"; import { check, messageOf } from "./run.ts"; const [mode, stage, ...rest] = process.argv.slice(2); try { const draft = path.join(stage, "draft"); let exit: number; if (mode === "lean") { const [leanFile, theorem, tacticsFlag, viewFile] = rest; const view = checkLeanView( viewFile ? await import(pathToFileURL(path.resolve(viewFile)).href) : {}, inspect, ); const facts = await extractFacts({ file: leanFile, theorem }); const compiled = compileLean(facts, { view, tactics: tacticsFlag === "1" }); exit = compiled.exit; fs.mkdirSync(draft); for (const [name, content] of compiled.files) { const fd = fs.openSync(path.join(draft, name), "wx"); try { fs.writeSync(fd, Buffer.from(content)); fs.writeSync(fd, "\n"); } finally { fs.closeSync(fd); } } } else { const [specFile, configFile, viewFile] = rest; const view = checkView( viewFile ? await import(pathToFileURL(path.resolve(viewFile)).href) : {}, inspect, ); const compiled = compile(await check(specFile, configFile), view); exit = compiled.exit; fs.mkdirSync(draft); for (const [name, parts] of compiled.files) { const fd = fs.openSync(path.join(draft, name), "wx"); try { for (const part of parts) fs.writeSync(fd, typeof part === "string" ? Buffer.from(part) : part); fs.writeSync(fd, "\n"); } finally { fs.closeSync(fd); } } } fs.renameSync(draft, path.join(stage, "done")); process.exit(exit); } catch (error) { console.error(messageOf(error)); process.exit(2); }