#!/usr/bin/env node import { Command } from "commander"; import { serveCommand } from "./commands/serve.js"; import { compileCommand } from "./commands/compile.js"; import { checkCommand } from "./commands/check.js"; import { mcpCommand } from "./commands/mcp.js"; import type { ServeOptions, CompileOptions } from "./types.js"; const program = new Command(); program .name("tttk") .description( "TTRPG 内容预览服务器。不带参数运行时默认启动当前目录的预览服务。", ) .version("0.0.1"); program .command("serve") .description("启动 web 服务器预览目录中的内容,并实时监听文件更新") .argument("[dir]", "要预览的目录", ".") .option("-p, --port ", "HTTP 端口号", "3000") .option("--host ", "主机地址", "0.0.0.0") .action(serveCommand); program .command("check") .alias("lint") .description("校验目录内的 tttk 文档,输出结构化诊断") .argument("[dir]", "要校验的目录", ".") .option("--json", "以 JSON 输出诊断", false) .option("--strict", "将警告视为错误", false) .option("--quiet", "只报告错误", false) .action(checkCommand); program .command("compile") .description("[未实现] 存根命令:不会生成任何输出。预览内容请使用 tttk serve") .argument("[dir]", "要编译的目录", ".") .option("-o, --output ", "输出目录", "./dist/output") .action(compileCommand); program.addCommand(mcpCommand); // 无子命令时默认执行 serve(即 `npx tttk` 等同于 `npx tttk serve .`) const args = process.argv.slice(2); if (args.length > 0) { const first = args[0]; const isKnownCommand = !first.startsWith("-") && program.commands.some( (c) => c.name() === first || c.aliases().includes(first), ); if (!isKnownCommand) { args.unshift("serve"); } } else { args.unshift("serve"); } program.parse([process.argv[0], process.argv[1], ...args]);