返回 CodeWhale
run.js
根目录 / npm / codewhale / scripts / run.js
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 pkg.codewhaleBinaryVersion || pkg.deepseekBinaryVersion || pkg.version;
13 console.log(`${binaryName} (npm wrapper) v${pkg.version}`);
14 console.log(`binary version: v${binVersion}`);
15 console.log(`repo: ${pkg.repository?.url || "N/A"}`);
16 }
17
18 async function run(binaryName, options = {}) {
19 const args = options.args || process.argv.slice(2);
20 const resolveBinaryPath = options.getBinaryPath || getBinaryPath;
21 const spawn = options.spawnSync || spawnSync;
22 const exit = options.exit || process.exit;
23 const versionFlag = isVersionFlag(args);
24
25 let binaryPath;
26 try {
27 binaryPath = await resolveBinaryPath(binaryName);
28 } catch (error) {
29 if (versionFlag) {
30 printVersionFallback(binaryName);
31 return exit(0);
32 }
33 throw error;
34 }
35
36 const result = spawn(binaryPath, args, {
37 stdio: "inherit",
38 });
39 if (result.error) {
40 if (versionFlag) {
41 printVersionFallback(binaryName);
42 return exit(0);
43 }
44 throw result.error;
45 }
46 return exit(result.status ?? 1);
47 }
48
49 async function runCodeWhale() {
50 await run("codewhale");
51 }
52
53 async function runCodeWhaleTui() {
54 await run("codewhale-tui");
55 }
56
57 module.exports = {
58 run,
59 runCodeWhale,
60 runCodeWhaleTui,
61 _internal: { isVersionFlag, printVersionFallback },
62 };
63
64 if (require.main === module) {
65 const command = process.argv[1] || "";
66 if (command.includes("tui")) {
67 runCodeWhaleTui();
68 } else {
69 runCodeWhale();
70 }
71 }
72
72 lines JAVASCRIPT