@chatcode/chatcode-cli-test 1.0.8 → 1.0.10

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 CHANGED
@@ -1,59 +1,70 @@
1
1
  #!/usr/bin/env node
2
- /**
3
- * Launcher shim (main package `bin`).
4
- *
5
- * Resolves the prebuilt native binary for the current platform from the
6
- * matching optional dependency (`<mainPkgName>-<os>-<arch>`) and execs it.
7
- * Those binaries are `bun build --compile` standalone executables, so the
8
- * runtime is bun — which (unlike node) delivers mouse events on Windows, so
9
- * fullscreen mouse works there.
10
- *
11
- * If no platform package is installed (unsupported platform, or
12
- * optionalDependencies skipped), it falls back to the bundled node build
13
- * (dist/cli.js). That works everywhere but, on Windows, has no fullscreen
14
- * mouse — same limitation as before.
15
- */
16
2
  import { spawnSync } from 'node:child_process'
17
- import { createRequire } from 'node:module'
18
3
  import { existsSync, readFileSync } from 'node:fs'
4
+ import { createRequire } from 'node:module'
19
5
  import { dirname, join } from 'node:path'
20
- import { fileURLToPath, pathToFileURL } from 'node:url'
6
+ import { fileURLToPath } from 'node:url'
21
7
 
22
8
  const require = createRequire(import.meta.url)
23
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
24
20
 
25
- function resolveNativeBinary() {
26
- const key = `${process.platform}-${process.arch}`
27
- const exeName = process.platform === 'win32' ? 'chatcode-cli.exe' : 'chatcode-cli'
28
- let mainName
29
21
  try {
30
- mainName = JSON.parse(readFileSync(join(here, '..', 'package.json'), 'utf8')).name
31
- } catch {
22
+ return require.resolve(`${mainPackageName}-${platformKey}/${executableName}`)
23
+ } catch (_) {
32
24
  return null
33
25
  }
34
- const platformPkg = `${mainName}-${key}`
35
- try {
36
- // Resolve via the platform package's package.json (always resolvable),
37
- // then join the binary name next to it.
38
- const pkgDir = dirname(require.resolve(`${platformPkg}/package.json`))
39
- const bin = join(pkgDir, exeName)
40
- return existsSync(bin) ? bin : null
41
- } catch {
42
- return null
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)
43
45
  }
46
+ process.exit(result.status ?? 1)
44
47
  }
45
48
 
46
- const bin = resolveNativeBinary()
47
- if (bin) {
48
- // stdio:'inherit' hands the console straight to the bun binary, so it reads
49
- // mouse input itself. This node launcher never touches stdin.
50
- const res = spawnSync(bin, process.argv.slice(2), { stdio: 'inherit' })
51
- if (res.error) {
52
- console.error(`Failed to launch native binary: ${res.error.message}`)
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}`)
53
54
  process.exit(1)
54
55
  }
55
- process.exit(res.status == null ? 1 : res.status)
56
- } else {
57
- // Fallback: bundled node build. Works everywhere; no Windows fullscreen mouse.
58
- await import(pathToFileURL(join(here, '..', 'dist', 'cli.js')).href)
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)
59
70
  }