@chatcode/chatcode-cli 2.0.4 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/cli.js ADDED
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env node
2
+ import { spawnSync } from 'node:child_process'
3
+ import { existsSync, readFileSync } from 'node:fs'
4
+ import { createRequire } from 'node:module'
5
+ import { dirname, join } from 'node:path'
6
+ import { fileURLToPath } from 'node:url'
7
+
8
+ const require = createRequire(import.meta.url)
9
+ const here = dirname(fileURLToPath(import.meta.url))
10
+ const root = join(here, '..')
11
+ const args = process.argv.slice(2)
12
+ const platformKey = `${process.platform}-${process.arch}`
13
+ const executableName = process.platform === 'win32' ? 'chatcode-cli.exe' : 'chatcode-cli'
14
+ const mainPackageName = JSON.parse(
15
+ readFileSync(join(root, 'package.json'), 'utf8'),
16
+ ).name
17
+
18
+ function resolveOptionalBinary() {
19
+ if (process.env.CHATCODE_CLI_FORCE_NODE === '1') return null
20
+
21
+ try {
22
+ return require.resolve(`${mainPackageName}-${platformKey}/${executableName}`)
23
+ } catch (_) {
24
+ return null
25
+ }
26
+ }
27
+
28
+ function resolveLocalBinary() {
29
+ if (process.env.CHATCODE_CLI_FORCE_NODE === '1') return null
30
+
31
+ const localBinary = join(root, 'dist', 'native', platformKey, executableName)
32
+ return existsSync(localBinary) ? localBinary : null
33
+ }
34
+
35
+ function run(command, commandArgs) {
36
+ const result = spawnSync(command, commandArgs, {
37
+ stdio: 'inherit',
38
+ windowsHide: false,
39
+ })
40
+
41
+ if (result.error) return result.error
42
+ if (result.signal) {
43
+ process.kill(process.pid, result.signal)
44
+ process.exit(1)
45
+ }
46
+ process.exit(result.status ?? 1)
47
+ }
48
+
49
+ const nativeBinary = resolveLocalBinary() ?? resolveOptionalBinary()
50
+ if (nativeBinary) {
51
+ const error = run(nativeBinary, args)
52
+ if (error && error.code !== 'ENOENT') {
53
+ console.error(`Failed to run native ChatCode CLI binary: ${error.message}`)
54
+ process.exit(1)
55
+ }
56
+ }
57
+
58
+ const fallbackCli = join(root, 'dist', 'cli.js')
59
+ if (!existsSync(fallbackCli)) {
60
+ console.error(
61
+ `ChatCode CLI is not installed correctly: missing ${fallbackCli}`,
62
+ )
63
+ process.exit(1)
64
+ }
65
+
66
+ const error = run(process.execPath, [fallbackCli, ...args])
67
+ if (error) {
68
+ console.error(`Failed to run ChatCode CLI: ${error.message}`)
69
+ process.exit(1)
70
+ }