goatchain-cli 0.0.9-beta.9 → 0.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.
@@ -0,0 +1,183 @@
1
+ #!/usr/bin/env node
2
+ import childProcess from 'node:child_process'
3
+ import fs from 'node:fs'
4
+ import os from 'node:os'
5
+ import path from 'node:path'
6
+ import process from 'node:process'
7
+ import { fileURLToPath, pathToFileURL } from 'node:url'
8
+
9
+ const __filename = fileURLToPath(import.meta.url)
10
+ const __dirname = path.dirname(__filename)
11
+ const isWindows = os.platform() === 'win32'
12
+
13
+ let wrapperVersion = ''
14
+ try {
15
+ const pkg = JSON.parse(
16
+ fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8'),
17
+ )
18
+ wrapperVersion = pkg.version || ''
19
+ }
20
+ catch {}
21
+
22
+ const wrapperDir = path.join(__dirname, '..').replace(/\\/g, '/').toLowerCase()
23
+ const wrapperPm
24
+ = wrapperDir.includes('.bun/') || wrapperDir.includes('bun/install/')
25
+ ? 'bun'
26
+ : 'npm'
27
+
28
+ const args = process.argv.slice(2)
29
+ const routing = resolveRouting(args)
30
+
31
+ if (routing.target === 'opentui') {
32
+ runOpentui(routing.forwardArgs)
33
+ }
34
+ else {
35
+ await runInk()
36
+ }
37
+
38
+ function resolveRouting(argv) {
39
+ // Explicit `goatchain ink ...` forces the Ink TUI.
40
+ if (argv[0] === 'ink')
41
+ return { target: 'ink', forwardArgs: argv.slice(1) }
42
+ // Explicit `goatchain opentui ...` also routes to the opentui binary
43
+ // (kept for backwards compatibility; opentui is now the default).
44
+ if (argv[0] === 'opentui')
45
+ return { target: 'opentui', forwardArgs: argv.slice(1) }
46
+
47
+ // Default: everything goes to the opentui binary.
48
+ return { target: 'opentui', forwardArgs: argv }
49
+ }
50
+
51
+ async function runInk() {
52
+ const inkEntry = path.join(__dirname, '..', 'dist', 'ink.mjs')
53
+ if (!fs.existsSync(inkEntry)) {
54
+ console.error('[goatchain] Ink bundle missing: ' + inkEntry)
55
+ console.error('[goatchain] Try reinstalling goatchain-cli.')
56
+ process.exit(1)
57
+ }
58
+ // Inject wrapper identity so the in-process upgrade check targets the
59
+ // npm wrapper package rather than upstream `dimcode`.
60
+ process.env.DIMCODE_NPM_PACKAGE = 'goatchain-cli'
61
+ process.env.DIMCODE_NPM_PACKAGE_MANAGER = wrapperPm
62
+ // goatchain-cli is a beta-only channel: ignore any stable releases on the
63
+ // registry when deciding whether to upgrade.
64
+ process.env.DIMCODE_NPM_PACKAGE_BETA_ONLY = '1'
65
+ if (wrapperVersion)
66
+ process.env.DIMCODE_NPM_PACKAGE_VERSION = wrapperVersion
67
+ try {
68
+ await import(pathToFileURL(inkEntry).href)
69
+ }
70
+ catch (err) {
71
+ console.error('[goatchain] Failed to start Ink TUI:')
72
+ console.error(err)
73
+ process.exit(1)
74
+ }
75
+ }
76
+
77
+ function runOpentui(restArgs) {
78
+ const envPath = process.env.DIMCODE_BIN_PATH
79
+ if (envPath) {
80
+ if (!fs.existsSync(envPath)) {
81
+ console.error(
82
+ '[goatchain] DIMCODE_BIN_PATH is set but file not found: ' + envPath,
83
+ )
84
+ process.exit(1)
85
+ }
86
+ spawnBinary(envPath, restArgs)
87
+ return
88
+ }
89
+
90
+ const scriptPath = fs.realpathSync(__filename)
91
+ const scriptDir = path.dirname(scriptPath)
92
+
93
+ const platformMap = { darwin: 'darwin', linux: 'linux', win32: 'windows' }
94
+ const archMap = { x64: 'x64', arm64: 'arm64' }
95
+ const platform = platformMap[os.platform()] || os.platform()
96
+ const arch = archMap[os.arch()] || os.arch()
97
+ const base = 'dimcode-' + platform + '-' + arch
98
+ const binary = platform === 'windows' ? 'dimcode.exe' : 'dimcode'
99
+
100
+ if (process.env.DIMCODE_DEBUG) {
101
+ console.error('[goatchain] Wrapper version: ' + wrapperVersion)
102
+ console.error('[goatchain] Platform: ' + platform + ' Arch: ' + arch)
103
+ console.error('[goatchain] Looking for package: ' + base)
104
+ console.error('[goatchain] Script dir: ' + scriptDir)
105
+ }
106
+
107
+ const resolved = findBinary(scriptDir, base, binary)
108
+ if (!resolved) {
109
+ console.error(
110
+ '[goatchain] Could not find the opentui binary for your platform.\n'
111
+ + '\n'
112
+ + ' Platform: ' + os.platform() + ' (' + platform + ')\n'
113
+ + ' Arch: ' + os.arch() + ' (' + arch + ')\n'
114
+ + ' Expected: ' + base + '/bin/' + binary + '\n'
115
+ + ' Searched: ' + scriptDir + ' (and parent directories)\n'
116
+ + '\n'
117
+ + 'Try manually installing the platform package:\n'
118
+ + ' npm install ' + base + '\n',
119
+ )
120
+ process.exit(1)
121
+ }
122
+
123
+ if (process.env.DIMCODE_DEBUG)
124
+ console.error('[goatchain] Resolved binary: ' + resolved)
125
+
126
+ spawnBinary(resolved, restArgs)
127
+ }
128
+
129
+ function findBinary(startDir, base, binary) {
130
+ let current = startDir
131
+ for (;;) {
132
+ const modules = path.join(current, 'node_modules')
133
+ if (fs.existsSync(modules)) {
134
+ const candidate = path.join(modules, base, 'bin', binary)
135
+ if (fs.existsSync(candidate))
136
+ return candidate
137
+ }
138
+ const parent = path.dirname(current)
139
+ if (parent === current)
140
+ return
141
+ current = parent
142
+ }
143
+ }
144
+
145
+ function spawnBinary(target, forwardedArgs) {
146
+ const env = {
147
+ ...process.env,
148
+ DIMCODE_NPM_PACKAGE: 'goatchain-cli',
149
+ DIMCODE_NPM_PACKAGE_MANAGER: wrapperPm,
150
+ DIMCODE_NPM_PACKAGE_BETA_ONLY: '1',
151
+ }
152
+ if (wrapperVersion)
153
+ env.DIMCODE_NPM_PACKAGE_VERSION = wrapperVersion
154
+
155
+ const result = childProcess.spawnSync(target, forwardedArgs, {
156
+ stdio: 'inherit',
157
+ cwd: process.cwd(),
158
+ env,
159
+ windowsHide: false,
160
+ })
161
+ if (result.error) {
162
+ console.error(
163
+ '[goatchain] Failed to start opentui binary: ' + result.error.message,
164
+ )
165
+ if (isWindows) {
166
+ console.error('[goatchain] Binary path: ' + target)
167
+ if (result.error.code === 'ENOENT') {
168
+ console.error(
169
+ '[goatchain] The binary file was not found. Try reinstalling:',
170
+ )
171
+ console.error(
172
+ ' npm uninstall -g goatchain-cli && npm install -g goatchain-cli',
173
+ )
174
+ }
175
+ }
176
+ process.exit(1)
177
+ }
178
+ if (result.status !== 0 && result.status !== null) {
179
+ if (process.env.DIMCODE_DEBUG)
180
+ console.error('[goatchain] Binary exited with code: ' + result.status)
181
+ }
182
+ process.exit(typeof result.status === 'number' ? result.status : 0)
183
+ }