bingocode 1.1.201 → 1.1.202

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.
Files changed (2) hide show
  1. package/bin/bingo-win.cjs +218 -215
  2. package/package.json +1 -1
package/bin/bingo-win.cjs CHANGED
@@ -1,215 +1,218 @@
1
- #!/usr/bin/env node
2
-
3
- const { spawn, spawnSync } = require('node:child_process');
4
- const path = require('path');
5
- const os = require('os');
6
- const fs = require('fs');
7
-
8
- process.env.NoDefaultCurrentDirectoryInExePath = '1';
9
-
10
- // ── 首次部署:将默认 bingo 配置复制到 ~/.claude/bingo/ ──
11
- /**
12
- * 更加健壮的根目录定位:
13
- * 1. 如果 preload.ts 在 ../ (当前 bin/ 目录下运行)
14
- * 2. 否则查找同级及上级目录中的 package.json
15
- */
16
- function getProjectRoot() {
17
- let curr = __dirname;
18
- try {
19
- while (curr !== path.dirname(curr)) {
20
- if (fs.existsSync(path.join(curr, 'preload.ts')) || fs.existsSync(path.join(curr, 'package.json'))) {
21
- return curr;
22
- }
23
- const parent = path.dirname(curr);
24
- if (fs.existsSync(path.join(parent, 'preload.ts'))) return parent;
25
- curr = parent;
26
- }
27
- } catch (err) {
28
- // 防止权限拒绝等导致挂死
29
- }
30
- return path.join(__dirname, '..');
31
- }
32
-
33
- const ROOT_DIR = getProjectRoot();
34
-
35
- (function deployBingoDefaults() {
36
- const configDir = process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude');
37
- const bingoDir = path.join(configDir, 'bingo');
38
- const targetSettings = path.join(bingoDir, 'settings.json');
39
-
40
- // 只在 settings.json 不存在时才部署
41
- if (!fs.existsSync(targetSettings)) {
42
- const defaultsDir = path.join(ROOT_DIR, 'config', 'bingo-defaults');
43
- const srcSettings = path.join(defaultsDir, 'settings.json');
44
-
45
- if (fs.existsSync(srcSettings)) {
46
- try {
47
- if (!fs.existsSync(bingoDir)) {
48
- fs.mkdirSync(bingoDir, { recursive: true });
49
- }
50
- fs.copyFileSync(srcSettings, targetSettings);
51
- console.log('[bingo] 首次启动:已部署默认配置到', targetSettings);
52
- } catch (err) {
53
- console.warn('[bingo] 部署默认配置失败:', err.message);
54
- }
55
- }
56
- }
57
- })();
58
-
59
- // 自动定位 bun.exe(纯文件系统查找,无子进程,无 DEP0190 警告)
60
- function resolveBunExe() {
61
- // 1. 用户指定路径
62
- if (process.env.BUN_PATH && fs.existsSync(process.env.BUN_PATH)) {
63
- return process.env.BUN_PATH;
64
- }
65
- const home = os.homedir();
66
- const appData = process.env.APPDATA || path.join(home, 'AppData', 'Roaming');
67
- const candidates = [
68
- // npm install -g bun 的真实 exe(最常见)
69
- path.join(appData, 'npm', 'node_modules', 'bun', 'bin', 'bun.exe'),
70
- // bun 官方安装脚本位置
71
- path.join(home, '.bun', 'bin', 'bun.exe'),
72
- ];
73
- // 遍历 PATH 中每个目录查找 bun.exe
74
- for (const dir of (process.env.PATH || '').split(path.delimiter)) {
75
- candidates.push(path.join(dir, 'bun.exe'));
76
- }
77
- for (const c of candidates) {
78
- try { if (fs.existsSync(c)) return c; } catch (_) {}
79
- }
80
- return null;
81
- }
82
-
83
- // 检查 bun 是否可用
84
- function bunExists() {
85
- return resolveBunExe() !== null;
86
- }
87
-
88
- // 安装 bun(通过 npm install -g bun)
89
- function installBun() {
90
- console.log('[bingocode] bun 未检测到,正在通过 npm install -g bun 安装...');
91
-
92
- try {
93
- const npmResult = spawnSync(
94
- 'npm.cmd',
95
- ['install', '-g', 'bun', '--loglevel', 'error'],
96
- { stdio: 'inherit' }
97
- );
98
- if (npmResult.status !== 0) {
99
- throw new Error(`npm install -g bun 失败,exit code ${npmResult.status}`);
100
- }
101
-
102
- console.log('[bingocode] bun 安装完成,正在启动...');
103
- return true;
104
- } catch (err) {
105
- console.error(`[bingocode] bun 自动安装失败: ${err.message}`);
106
- console.log('[bingocode] 请手动安装 bun: npm install -g bun');
107
- return false;
108
- }
109
- }
110
-
111
- if (!bunExists()) {
112
- if (!installBun()) {
113
- process.exit(1);
114
- }
115
- }
116
-
117
- // 安装完成后重新解析 bun 路径
118
- const bunExe = resolveBunExe();
119
- if (!bunExe) {
120
- console.error('[bingocode] 安装后仍找不到 bun.exe,请重新打开终端后再试,或手动安装 bun: npm install -g bun');
121
- process.exit(1);
122
- }
123
-
124
- // Bingo Manager 入口
125
- const entry = path.join(ROOT_DIR, 'src', 'entrypoints', 'manager.tsx');
126
-
127
- // preload shim
128
- const preload = path.join(ROOT_DIR, 'preload.ts');
129
- if (!fs.existsSync(preload)) {
130
- console.error('[bingocode] 找不到 preload.ts,MACRO 将无法注入:' + preload);
131
- process.exit(1);
132
- }
133
-
134
- // 检查 .env
135
- let envFlag = '';
136
- const envPath = path.join(ROOT_DIR, '.env');
137
- if (fs.existsSync(envPath)) {
138
- envFlag = `--env-file=${envPath}`;
139
- }
140
-
141
- const extraArgs = process.argv.slice(2);
142
-
143
- // ── Start tray daemon if not already running ────────────────────────────────
144
- const RUNTIME_DIR = path.join(os.homedir(), '.claude-cli', 'runtime');
145
- const DAEMON_LOCK_FILE = path.join(RUNTIME_DIR, 'daemon.lock');
146
-
147
- function serverHealthy() {
148
- return new Promise((resolve) => {
149
- const http = require('http');
150
- const req = http.get('http://127.0.0.1:3456/health', { timeout: 1000 }, (res) => {
151
- let data = '';
152
- res.on('data', (c) => (data += c));
153
- res.on('end', () => {
154
- try {
155
- resolve(res.statusCode === 200 && JSON.parse(data).status === 'ok');
156
- } catch {
157
- resolve(false);
158
- }
159
- });
160
- });
161
- req.on('error', () => resolve(false));
162
- req.setTimeout(1000, () => { req.destroy(); resolve(false); });
163
- });
164
- }
165
-
166
- function isDaemonAlive() {
167
- try {
168
- const pid = parseInt(fs.readFileSync(DAEMON_LOCK_FILE, 'utf-8').trim(), 10);
169
- process.kill(pid, 0); // signal 0 == probe only
170
- return true;
171
- } catch {
172
- return false;
173
- }
174
- }
175
-
176
- const CHECK_MS = 3000;
177
-
178
- async function startTrayDaemonIfNeeded() {
179
- // Daemon PID lock prevents race: only one daemon runs across multiple bingo launches
180
- if (isDaemonAlive()) {
181
- console.log('[bingo] Daemon already running, skipping daemon start');
182
- return;
183
- }
184
-
185
- // stale lock cleanup
186
- try { fs.unlinkSync(DAEMON_LOCK_FILE); } catch {}
187
-
188
- console.log('[bingo] Starting tray daemon...');
189
- const trayEntry = path.join(ROOT_DIR, 'src', 'entrypoints', 'tray-only.ts');
190
- const daemon = spawn(bunExe, ['--preload=' + preload, trayEntry], {
191
- detached: true,
192
- stdio: 'ignore',
193
- env: { ...process.env },
194
- });
195
- daemon.unref();
196
-
197
- // Wait for health check
198
- const start = Date.now();
199
- while (Date.now() - start < CHECK_MS) {
200
- if (await serverHealthy()) {
201
- console.log('[bingo] Tray daemon started successfully');
202
- break;
203
- }
204
- await (new Promise((r) => setTimeout(r, 300)));
205
- }
206
- }
207
-
208
- // Start daemon, then launch CLI independently
209
- startTrayDaemonIfNeeded().catch(() => {});
210
-
211
- // ── Launch CLI (connects to existing server) ───────────────────────────────
212
- const args = [`--preload=${preload}`, envFlag, entry, ...extraArgs].filter(Boolean);
213
-
214
- // 用绝对路径 spawn,不依赖 shell 解析 PATH
215
- const child = spawn(bunExe, args, { stdio: 'inherit' });
1
+ #!/usr/bin/env node
2
+
3
+ const { spawn, spawnSync } = require('node:child_process');
4
+ const path = require('path');
5
+ const os = require('os');
6
+ const fs = require('fs');
7
+
8
+ process.env.NoDefaultCurrentDirectoryInExePath = '1';
9
+
10
+ // ── 首次部署:将默认 bingo 配置复制到 ~/.claude/bingo/ ──
11
+ /**
12
+ * 更加健壮的根目录定位:
13
+ * 1. 如果 preload.ts 在 ../ (当前 bin/ 目录下运行)
14
+ * 2. 否则查找同级及上级目录中的 package.json
15
+ */
16
+ function getProjectRoot() {
17
+ let curr = __dirname;
18
+ try {
19
+ while (curr !== path.dirname(curr)) {
20
+ if (fs.existsSync(path.join(curr, 'preload.ts')) || fs.existsSync(path.join(curr, 'package.json'))) {
21
+ return curr;
22
+ }
23
+ const parent = path.dirname(curr);
24
+ if (fs.existsSync(path.join(parent, 'preload.ts'))) return parent;
25
+ curr = parent;
26
+ }
27
+ } catch (err) {
28
+ // 防止权限拒绝等导致挂死
29
+ }
30
+ return path.join(__dirname, '..');
31
+ }
32
+
33
+ const ROOT_DIR = getProjectRoot();
34
+
35
+ (function deployBingoDefaults() {
36
+ const configDir = process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude');
37
+ const bingoDir = path.join(configDir, 'bingo');
38
+ const targetSettings = path.join(bingoDir, 'settings.json');
39
+
40
+ // 只在 settings.json 不存在时才部署
41
+ if (!fs.existsSync(targetSettings)) {
42
+ const defaultsDir = path.join(ROOT_DIR, 'config', 'bingo-defaults');
43
+ const srcSettings = path.join(defaultsDir, 'settings.json');
44
+
45
+ if (fs.existsSync(srcSettings)) {
46
+ try {
47
+ if (!fs.existsSync(bingoDir)) {
48
+ fs.mkdirSync(bingoDir, { recursive: true });
49
+ }
50
+ fs.copyFileSync(srcSettings, targetSettings);
51
+ console.log('[bingo] 首次启动:已部署默认配置到', targetSettings);
52
+ } catch (err) {
53
+ console.warn('[bingo] 部署默认配置失败:', err.message);
54
+ }
55
+ }
56
+ }
57
+ })();
58
+
59
+ // 自动定位 bun.exe(纯文件系统查找,无子进程,无 DEP0190 警告)
60
+ function resolveBunExe() {
61
+ // 1. 用户指定路径
62
+ if (process.env.BUN_PATH && fs.existsSync(process.env.BUN_PATH)) {
63
+ return process.env.BUN_PATH;
64
+ }
65
+ const home = os.homedir();
66
+ const appData = process.env.APPDATA || path.join(home, 'AppData', 'Roaming');
67
+ const candidates = [
68
+ // npm install -g bun 的真实 exe(最常见)
69
+ path.join(appData, 'npm', 'node_modules', 'bun', 'bin', 'bun.exe'),
70
+ // bun 官方安装脚本位置
71
+ path.join(home, '.bun', 'bin', 'bun.exe'),
72
+ ];
73
+ // 遍历 PATH 中每个目录查找 bun.exe
74
+ for (const dir of (process.env.PATH || '').split(path.delimiter)) {
75
+ candidates.push(path.join(dir, 'bun.exe'));
76
+ }
77
+ for (const c of candidates) {
78
+ try { if (fs.existsSync(c)) return c; } catch (_) {}
79
+ }
80
+ return null;
81
+ }
82
+
83
+ // 检查 bun 是否可用
84
+ function bunExists() {
85
+ return resolveBunExe() !== null;
86
+ }
87
+
88
+ // 安装 bun(通过 npm install -g bun)
89
+ function installBun() {
90
+ console.log('[bingocode] bun 未检测到,正在通过 npm install -g bun 安装...');
91
+
92
+ try {
93
+ const installArgs = ['install', '-g', 'bun', '--allow-scripts=bun', '--loglevel', 'error'];
94
+ const npmResult = process.platform === 'win32'
95
+ ? spawnSync(
96
+ process.env.ComSpec || 'cmd.exe',
97
+ ['/d', '/s', '/c', 'npm ' + installArgs.join(' ')],
98
+ { stdio: 'inherit' }
99
+ )
100
+ : spawnSync('npm', installArgs, { stdio: 'inherit' });
101
+ if (npmResult.error || npmResult.status !== 0) {
102
+ throw new Error(`npm install -g bun 失败,exit code ${npmResult.status ?? npmResult.error?.message}`);
103
+ }
104
+
105
+ console.log('[bingocode] bun 安装完成,正在启动...');
106
+ return true;
107
+ } catch (err) {
108
+ console.error(`[bingocode] bun 自动安装失败: ${err.message}`);
109
+ console.log('[bingocode] 请手动安装 bun: npm install -g bun');
110
+ return false;
111
+ }
112
+ }
113
+
114
+ if (!bunExists()) {
115
+ if (!installBun()) {
116
+ process.exit(1);
117
+ }
118
+ }
119
+
120
+ // 安装完成后重新解析 bun 路径
121
+ const bunExe = resolveBunExe();
122
+ if (!bunExe) {
123
+ console.error('[bingocode] 安装后仍找不到 bun.exe,请重新打开终端后再试,或手动安装 bun: npm install -g bun');
124
+ process.exit(1);
125
+ }
126
+
127
+ // Bingo Manager 入口
128
+ const entry = path.join(ROOT_DIR, 'src', 'entrypoints', 'manager.tsx');
129
+
130
+ // preload shim
131
+ const preload = path.join(ROOT_DIR, 'preload.ts');
132
+ if (!fs.existsSync(preload)) {
133
+ console.error('[bingocode] 找不到 preload.ts,MACRO 将无法注入:' + preload);
134
+ process.exit(1);
135
+ }
136
+
137
+ // 检查 .env
138
+ let envFlag = '';
139
+ const envPath = path.join(ROOT_DIR, '.env');
140
+ if (fs.existsSync(envPath)) {
141
+ envFlag = `--env-file=${envPath}`;
142
+ }
143
+
144
+ const extraArgs = process.argv.slice(2);
145
+
146
+ // ── Start tray daemon if not already running ────────────────────────────────
147
+ const RUNTIME_DIR = path.join(os.homedir(), '.claude-cli', 'runtime');
148
+ const DAEMON_LOCK_FILE = path.join(RUNTIME_DIR, 'daemon.lock');
149
+
150
+ function serverHealthy() {
151
+ return new Promise((resolve) => {
152
+ const http = require('http');
153
+ const req = http.get('http://127.0.0.1:3456/health', { timeout: 1000 }, (res) => {
154
+ let data = '';
155
+ res.on('data', (c) => (data += c));
156
+ res.on('end', () => {
157
+ try {
158
+ resolve(res.statusCode === 200 && JSON.parse(data).status === 'ok');
159
+ } catch {
160
+ resolve(false);
161
+ }
162
+ });
163
+ });
164
+ req.on('error', () => resolve(false));
165
+ req.setTimeout(1000, () => { req.destroy(); resolve(false); });
166
+ });
167
+ }
168
+
169
+ function isDaemonAlive() {
170
+ try {
171
+ const pid = parseInt(fs.readFileSync(DAEMON_LOCK_FILE, 'utf-8').trim(), 10);
172
+ process.kill(pid, 0); // signal 0 == probe only
173
+ return true;
174
+ } catch {
175
+ return false;
176
+ }
177
+ }
178
+
179
+ const CHECK_MS = 3000;
180
+
181
+ async function startTrayDaemonIfNeeded() {
182
+ // Daemon PID lock prevents race: only one daemon runs across multiple bingo launches
183
+ if (isDaemonAlive()) {
184
+ console.log('[bingo] Daemon already running, skipping daemon start');
185
+ return;
186
+ }
187
+
188
+ // stale lock cleanup
189
+ try { fs.unlinkSync(DAEMON_LOCK_FILE); } catch {}
190
+
191
+ console.log('[bingo] Starting tray daemon...');
192
+ const trayEntry = path.join(ROOT_DIR, 'src', 'entrypoints', 'tray-only.ts');
193
+ const daemon = spawn(bunExe, ['--preload=' + preload, trayEntry], {
194
+ detached: true,
195
+ stdio: 'ignore',
196
+ env: { ...process.env },
197
+ });
198
+ daemon.unref();
199
+
200
+ // Wait for health check
201
+ const start = Date.now();
202
+ while (Date.now() - start < CHECK_MS) {
203
+ if (await serverHealthy()) {
204
+ console.log('[bingo] Tray daemon started successfully');
205
+ break;
206
+ }
207
+ await (new Promise((r) => setTimeout(r, 300)));
208
+ }
209
+ }
210
+
211
+ // Start daemon, then launch CLI independently
212
+ startTrayDaemonIfNeeded().catch(() => {});
213
+
214
+ // ── Launch CLI (connects to existing server) ───────────────────────────────
215
+ const args = [`--preload=${preload}`, envFlag, entry, ...extraArgs].filter(Boolean);
216
+
217
+ // 用绝对路径 spawn,不依赖 shell 解析 PATH
218
+ const child = spawn(bunExe, args, { stdio: 'inherit' });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bingocode",
3
- "version": "1.1.201",
3
+ "version": "1.1.202",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "claude": "bin/claude-win.cjs",