#!/usr/bin/env node "use strict"; const path = require("node:path"); const { spawnSync } = require("node:child_process"); const pkgRoot = path.resolve(__dirname, ".."); const pkg = require(path.join(pkgRoot, "package.json")); function printHelp() { console.log(`ts2lua-unreal v${pkg.version} - TypeScript to UnLua compiler toolchain Usage: ts2lua-unreal build [tstl args...] Compile TS project to UnLua Lua (default: -p tsconfig.json) ts2lua-unreal watch [tstl args...] Watch and incrementally compile for UnLua HotReload ts2lua-unreal info Show installed version and local documentation paths ts2lua-unreal --help Show this help message `); } function printInfo() { console.log( JSON.stringify( { name: pkg.name, version: pkg.version, packageRoot: pkgRoot, tsconfigBase: path.join(pkgRoot, "tsconfig.base.json"), unluaTypes: path.join(pkgRoot, "types", "unlua.d.ts"), docs: { integrationGuide: path.join(pkgRoot, "docs", "integration-guide.md"), spec: path.join(pkgRoot, "docs", "ts-unlua-spec.md"), aiRulesTemplate: path.join(pkgRoot, "docs", "ai-rules-template.md"), changelog: path.join(pkgRoot, "CHANGELOG.md"), }, }, null, 2 ) ); } function runTstl(extraArgs) { const tstlBin = require.resolve("typescript-to-lua/dist/tstl.js"); const hasProjectFlag = extraArgs.some((arg) => arg === "-p" || arg === "--project"); const args = hasProjectFlag ? extraArgs : ["-p", "tsconfig.json", ...extraArgs]; const result = spawnSync(process.execPath, [tstlBin, ...args], { stdio: "inherit", }); process.exit(result.status ?? 1); } const [, , cmd, ...rest] = process.argv; if (!cmd || cmd === "-h" || cmd === "--help" || cmd === "help") { printHelp(); process.exit(0); } if (cmd === "-v" || cmd === "--version") { console.log(pkg.version); process.exit(0); } if (cmd === "info") { printInfo(); process.exit(0); } if (cmd === "build") { runTstl(rest); } else if (cmd === "watch") { runTstl(["--watch", ...rest]); } else { // 直接透传给 tstl runTstl([cmd, ...rest]); }