scripts/main.ts
1.4 KB| 1 | |
| 2 | import path from "node:path" |
| 3 | import process from "node:process" |
| 4 | import { runChunkCli } from "./chunk.js" |
| 5 | |
| 6 | function formatScriptCommand(fallback: string): string { |
| 7 | const raw = process.argv[1] |
| 8 | const displayPath = raw |
| 9 | ? (() => { |
| 10 | const relative = path.relative(process.cwd(), raw) |
| 11 | return relative && !relative.startsWith("..") ? relative : raw |
| 12 | })() |
| 13 | : fallback |
| 14 | |
| 15 | const quotedPath = displayPath.includes(" ") |
| 16 | ? `"${displayPath.replace(/"/g, '\\"')}"` |
| 17 | : displayPath |
| 18 | |
| 19 | return `npx -y bun ${quotedPath}` |
| 20 | } |
| 21 | |
| 22 | function printUsage(exitCode: number): never { |
| 23 | const cmd = formatScriptCommand("scripts/main.ts") |
| 24 | console.log(`Baoyu Translate CLI |
| 25 | |
| 26 | Usage: |
| 27 | ${cmd} <file> [--max-words 5000] [--output-dir <dir>] |
| 28 | ${cmd} chunk <file> [--max-words 5000] [--output-dir <dir>] |
| 29 | |
| 30 | Commands: |
| 31 | chunk Split markdown into chunks |
| 32 | |
| 33 | Options: |
| 34 | --max-words <n> Maximum words per chunk (default: 5000) |
| 35 | --output-dir <dir> Write chunks into <dir>/chunks/ |
| 36 | -h, --help Show help |
| 37 | `) |
| 38 | process.exit(exitCode) |
| 39 | } |
| 40 | |
| 41 | const args = process.argv.slice(2) |
| 42 | |
| 43 | if (args.length === 0) { |
| 44 | printUsage(1) |
| 45 | } |
| 46 | |
| 47 | if (args[0] === "-h" || args[0] === "--help") { |
| 48 | printUsage(0) |
| 49 | } |
| 50 | |
| 51 | if (args[0] === "chunk") { |
| 52 | process.exit(runChunkCli(args.slice(1), `${formatScriptCommand("scripts/main.ts")} chunk`)) |
| 53 | } |
| 54 | |
| 55 | process.exit(runChunkCli(args, formatScriptCommand("scripts/main.ts"))) |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

