goatchain-cli 0.0.9-beta.20 → 0.0.9-beta.21

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,167 @@
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 useOpentui = args[0] === 'opentui'
30
+ const forwardArgs = useOpentui ? args.slice(1) : args
31
+
32
+ if (useOpentui) {
33
+ runOpentui(forwardArgs)
34
+ }
35
+ else {
36
+ await runInk()
37
+ }
38
+
39
+ async function runInk() {
40
+ const inkEntry = path.join(__dirname, '..', 'dist', 'ink.mjs')
41
+ if (!fs.existsSync(inkEntry)) {
42
+ console.error('[goatchain] Ink bundle missing: ' + inkEntry)
43
+ console.error('[goatchain] Try reinstalling goatchain-cli.')
44
+ process.exit(1)
45
+ }
46
+ // Inject wrapper identity so the in-process upgrade check targets the
47
+ // npm wrapper package rather than upstream `dimcode`.
48
+ process.env.DIMCODE_NPM_PACKAGE = 'goatchain-cli'
49
+ process.env.DIMCODE_NPM_PACKAGE_MANAGER = wrapperPm
50
+ if (wrapperVersion)
51
+ process.env.DIMCODE_NPM_PACKAGE_VERSION = wrapperVersion
52
+ try {
53
+ await import(pathToFileURL(inkEntry).href)
54
+ }
55
+ catch (err) {
56
+ console.error('[goatchain] Failed to start Ink TUI:')
57
+ console.error(err)
58
+ process.exit(1)
59
+ }
60
+ }
61
+
62
+ function runOpentui(restArgs) {
63
+ const envPath = process.env.DIMCODE_BIN_PATH
64
+ if (envPath) {
65
+ if (!fs.existsSync(envPath)) {
66
+ console.error(
67
+ '[goatchain] DIMCODE_BIN_PATH is set but file not found: ' + envPath,
68
+ )
69
+ process.exit(1)
70
+ }
71
+ spawnBinary(envPath, restArgs)
72
+ return
73
+ }
74
+
75
+ const scriptPath = fs.realpathSync(__filename)
76
+ const scriptDir = path.dirname(scriptPath)
77
+
78
+ const platformMap = { darwin: 'darwin', linux: 'linux', win32: 'windows' }
79
+ const archMap = { x64: 'x64', arm64: 'arm64' }
80
+ const platform = platformMap[os.platform()] || os.platform()
81
+ const arch = archMap[os.arch()] || os.arch()
82
+ const base = 'goatchain-cli-' + platform + '-' + arch
83
+ const binary = platform === 'windows' ? 'goatchain.exe' : 'goatchain'
84
+
85
+ if (process.env.DIMCODE_DEBUG) {
86
+ console.error('[goatchain] Wrapper version: ' + wrapperVersion)
87
+ console.error('[goatchain] Platform: ' + platform + ' Arch: ' + arch)
88
+ console.error('[goatchain] Looking for package: ' + base)
89
+ console.error('[goatchain] Script dir: ' + scriptDir)
90
+ }
91
+
92
+ const resolved = findBinary(scriptDir, base, binary)
93
+ if (!resolved) {
94
+ console.error(
95
+ '[goatchain] Could not find the opentui binary for your platform.\n'
96
+ + '\n'
97
+ + ' Platform: ' + os.platform() + ' (' + platform + ')\n'
98
+ + ' Arch: ' + os.arch() + ' (' + arch + ')\n'
99
+ + ' Expected: ' + base + '/bin/' + binary + '\n'
100
+ + ' Searched: ' + scriptDir + ' (and parent directories)\n'
101
+ + '\n'
102
+ + 'Try manually installing the platform package:\n'
103
+ + ' npm install ' + base + '\n',
104
+ )
105
+ process.exit(1)
106
+ }
107
+
108
+ if (process.env.DIMCODE_DEBUG)
109
+ console.error('[goatchain] Resolved binary: ' + resolved)
110
+
111
+ spawnBinary(resolved, restArgs)
112
+ }
113
+
114
+ function findBinary(startDir, base, binary) {
115
+ let current = startDir
116
+ for (;;) {
117
+ const modules = path.join(current, 'node_modules')
118
+ if (fs.existsSync(modules)) {
119
+ const candidate = path.join(modules, base, 'bin', binary)
120
+ if (fs.existsSync(candidate))
121
+ return candidate
122
+ }
123
+ const parent = path.dirname(current)
124
+ if (parent === current)
125
+ return
126
+ current = parent
127
+ }
128
+ }
129
+
130
+ function spawnBinary(target, forwardedArgs) {
131
+ const env = {
132
+ ...process.env,
133
+ DIMCODE_NPM_PACKAGE: 'goatchain-cli',
134
+ DIMCODE_NPM_PACKAGE_MANAGER: wrapperPm,
135
+ }
136
+ if (wrapperVersion)
137
+ env.DIMCODE_NPM_PACKAGE_VERSION = wrapperVersion
138
+
139
+ const result = childProcess.spawnSync(target, forwardedArgs, {
140
+ stdio: 'inherit',
141
+ cwd: process.cwd(),
142
+ env,
143
+ windowsHide: false,
144
+ })
145
+ if (result.error) {
146
+ console.error(
147
+ '[goatchain] Failed to start opentui binary: ' + result.error.message,
148
+ )
149
+ if (isWindows) {
150
+ console.error('[goatchain] Binary path: ' + target)
151
+ if (result.error.code === 'ENOENT') {
152
+ console.error(
153
+ '[goatchain] The binary file was not found. Try reinstalling:',
154
+ )
155
+ console.error(
156
+ ' npm uninstall -g goatchain-cli && npm install -g goatchain-cli',
157
+ )
158
+ }
159
+ }
160
+ process.exit(1)
161
+ }
162
+ if (result.status !== 0 && result.status !== null) {
163
+ if (process.env.DIMCODE_DEBUG)
164
+ console.error('[goatchain] Binary exited with code: ' + result.status)
165
+ }
166
+ process.exit(typeof result.status === 'number' ? result.status : 0)
167
+ }