Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

JavaScript · 106 lines · 3.3 KiB · at 6dae872

// tsus build: src/**/*.{ts,tsus} -> dist/**/*.js (plain ESM).import fs from "node:fs";import path from "node:path";import ts from "typescript";import { preprocess, PreprocessError } from "./preprocess.js";import { checkSource } from "./check.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;};// Rewrite relative import/export specifiers ending in .tsus or .ts to .js.const rewriteSpecifiers = (code) =>  code.replace(    /(\b(?:from|import)\s*\(?\s*)(["'])(\.{1,2}\/[^"']*?)\.(?:tsus|ts)(\2)/g,    (_m, lead, quote, spec) => `${lead}${quote}${spec}.js${quote}`,  );const RUNTIME_IMPORT = `import { __tsus_match } from "@tsus/runtime";\n`;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) };};// Preprocess + check every .tsus file. Returns diagnostics (empty = clean).export const checkProject = (rootDir) => {  const { srcDir, files } = collectSources(rootDir);  const diagnostics = [];  for (const file of files) {    if (!file.endsWith(".tsus")) {      continue;    }    const rel = path.relative(rootDir, file);    const source = fs.readFileSync(file, "utf8");    try {      const { code } = preprocess(source);      diagnostics.push(...checkSource(rel, code));    } catch (error) {      if (error instanceof PreprocessError) {        diagnostics.push({          code: error.code,          message: `match macro error: ${error.message}`,          hint: "Arms look like: Pattern(binding) => expression, separated by commas.",          file: rel,          line: 1,          col: 1,        });      } else {        throw error;      }    }  }  return { srcDir, files, diagnostics };};export const buildProject = (rootDir) => {  const { srcDir, files, diagnostics } = 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;    }    let tsCode = fs.readFileSync(file, "utf8");    if (isTsus) {      const { code, changed } = preprocess(tsCode);      tsCode = changed ? RUNTIME_IMPORT + code : code;    }    tsCode = rewriteSpecifiers(tsCode);    const output = ts.transpileModule(tsCode, {      compilerOptions: {        module: ts.ModuleKind.ESNext,        target: ts.ScriptTarget.ES2022,      },    }).outputText;    const dest = path.join(distDir, rel.replace(/\.(tsus|ts)$/, ".js"));    fs.mkdirSync(path.dirname(dest), { recursive: true });    fs.writeFileSync(dest, output);    emitted.push(dest);  }  return { diagnostics: [], emitted };};