| 1 | const { spawnSync } = require("child_process"); |
| 2 | const { getBinaryPath } = require("./install"); |
| 3 | |
| 4 | const pkg = require("../package.json"); |
| 5 | |
| 6 | function isVersionFlag(args = process.argv.slice(2)) { |
| 7 | return args.includes("--version") || args.includes("-V"); |
| 8 | } |
| 9 | |
| 10 | function printVersionFallback(binaryName) { |
| 11 | const binVersion = |
| 12 | process.env.CODEWHALE_VERSION || |
| 13 | process.env.DEEPSEEK_TUI_VERSION || |
| 14 | process.env.DEEPSEEK_VERSION || |
| 15 | pkg.codewhaleBinaryVersion || pkg.deepseekBinaryVersion || pkg.version; |
| 16 | console.log(`${binaryName} (npm wrapper) v${pkg.version}`); |
| 17 | console.log(`binary version: v${binVersion}`); |
| 18 | console.log(`repo: ${pkg.repository?.url || "N/A"}`); |
| 19 | } |
| 20 | |
| 21 | async function run(binaryName, options = {}) { |
| 22 | const args = options.args || process.argv.slice(2); |
| 23 | const resolveBinaryPath = options.getBinaryPath || getBinaryPath; |
| 24 | const spawn = options.spawnSync || spawnSync; |
| 25 | const exit = options.exit || process.exit; |
| 26 | const versionFlag = isVersionFlag(args); |
| 27 | |
| 28 | let binaryPath; |
| 29 | try { |
| 30 | binaryPath = await resolveBinaryPath(binaryName); |
| 31 | } catch (error) { |
| 32 | if (versionFlag) { |
| 33 | printVersionFallback(binaryName); |
| 34 | return exit(0); |
| 35 | } |
| 36 | throw error; |
| 37 | } |
| 38 | |
| 39 | const result = spawn(binaryPath, args, { |
| 40 | stdio: "inherit", |
| 41 | }); |
| 42 | if (result.error) { |
| 43 | if (versionFlag) { |
| 44 | printVersionFallback(binaryName); |
| 45 | return exit(0); |
| 46 | } |
| 47 | throw result.error; |
| 48 | } |
| 49 | return exit(result.status ?? 1); |
| 50 | } |
| 51 | |
| 52 | async function runCodeWhale() { |
| 53 | await run("codewhale"); |
| 54 | } |
| 55 | |
| 56 | async function runCodeWhaleTui() { |
| 57 | // v0.9.5 single-binary: tui is now an alias to codewhale (kept for backwards compat, will warn) |
| 58 | if (!process.env.CODEWHALE_SUPPRESS_TUI_DEPRECATION) { |
| 59 | process.stderr.write("codewhale-tui: deprecated alias to `codewhale` (single binary since v0.9.5). Use `codewhale` instead.\n"); |
| 60 | } |
| 61 | await run("codewhale"); |
| 62 | } |
| 63 | |
| 64 | module.exports = { |
| 65 | run, |
| 66 | runCodeWhale, |
| 67 | runCodeWhaleTui, |
| 68 | _internal: { isVersionFlag, printVersionFallback }, |
| 69 | }; |
| 70 | |
| 71 | if (require.main === module) { |
| 72 | const command = process.argv[1] || ""; |
| 73 | if (command.includes("tui")) { |
| 74 | runCodeWhaleTui(); |
| 75 | } else { |
| 76 | runCodeWhale(); |
| 77 | } |
| 78 | } |
| 79 |