// tsus build: src/**/*.{ts,tsus} -> dist/**/*.js (plain ESM) + source maps.// tsus check: the same pipeline without emitting.import fs from "node:fs";import path from "node:path";import { compileTsus, emitJs, relativeSourcePath } from "./compile.js";import { semanticCheck } from "./program.js";const walk = (dir) => { const out = []; for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { const full = path.join(dir, entry.name); if (entry.isDirectory()) { out.push(...walk(full)); } else { out.push(full); } } return out;};export const collectSources = (rootDir) => { const srcDir = path.join(rootDir, "src"); if (!fs.existsSync(srcDir)) { throw new Error(`no src/ directory in ${rootDir}`); } return { srcDir, files: walk(srcDir) };};const isTsFile = (file) => file.endsWith(".ts");// Syntax-level failures make the lowered code meaningless to type-check, so// the semantic pass only runs once every file preprocesses and parses.const BLOCKING = new Set(["TSUS000", "TSUS006"]);// Preprocess + rule-check every .tsus file, then type-check the whole// project. Returns diagnostics (empty = clean) and the per-file compilations.export const checkProject = (rootDir, { semantic = true } = {}) => { const { srcDir, files } = collectSources(rootDir); const diagnostics = []; const compiled = new Map(); for (const file of files) { if (!file.endsWith(".tsus")) { continue; } const rel = path.relative(rootDir, file); const c = compileTsus(fs.readFileSync(file, "utf8"), rel); compiled.set(file, c); diagnostics.push(...c.diagnostics); } if (semantic && !diagnostics.some((d) => BLOCKING.has(d.code))) { diagnostics.push(...semanticCheck({ rootDir, tsFiles: files.filter(isTsFile), compiled })); } diagnostics.sort((a, b) => a.file.localeCompare(b.file) || a.line - b.line || a.col - b.col); return { srcDir, files, diagnostics, compiled };};export const buildProject = (rootDir) => { const { srcDir, files, diagnostics, compiled } = checkProject(rootDir); if (diagnostics.length > 0) { return { diagnostics, emitted: [] }; } const distDir = path.join(rootDir, "dist"); const emitted = []; for (const file of files) { const rel = path.relative(srcDir, file); const isTsus = file.endsWith(".tsus"); const isTs = file.endsWith(".ts") && !file.endsWith(".d.ts"); if (!isTsus && !isTs) { // Asset (data files, etc.): copy through so runtime-relative reads work. const dest = path.join(distDir, rel); fs.mkdirSync(path.dirname(dest), { recursive: true }); fs.copyFileSync(file, dest); emitted.push(dest); continue; } const dest = path.join(distDir, rel.replace(/\.(tsus|ts)$/, ".js")); const sourceText = isTsus ? compiled.get(file).source : fs.readFileSync(file, "utf8"); const { js, map } = emitJs({ code: isTsus ? compiled.get(file).code : sourceText, toSource: isTsus ? compiled.get(file).toSource : (o) => o, sourceText, sourcePath: relativeSourcePath(dest, file), outName: path.basename(dest), }); fs.mkdirSync(path.dirname(dest), { recursive: true }); fs.writeFileSync(dest, js); fs.writeFileSync(`${dest}.map`, `${JSON.stringify(map)}\n`); emitted.push(dest, `${dest}.map`); } return { diagnostics: [], emitted };};