@tina_summer/codepocket 2.1.3 → 2.2.0
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/codepocket.js +87 -22
- package/package.json +1 -1
package/bin/codepocket.js
CHANGED
|
@@ -72,6 +72,65 @@ if (args[0] === 'test') {
|
|
|
72
72
|
return
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
// ── Background mode helpers ──
|
|
76
|
+
|
|
77
|
+
const PID_FILE = path.join(CONFIG_DIR, 'codepocket.pid')
|
|
78
|
+
const LOG_DIR = path.join(CONFIG_DIR, 'logs')
|
|
79
|
+
|
|
80
|
+
function getLogFile() {
|
|
81
|
+
const d = new Date()
|
|
82
|
+
const ts = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
|
|
83
|
+
return path.join(LOG_DIR, `${ts}.log`)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function checkRunning() {
|
|
87
|
+
try {
|
|
88
|
+
const pid = parseInt(fs.readFileSync(PID_FILE, 'utf-8').trim())
|
|
89
|
+
if (pid && process.kill(pid, 0)) return pid
|
|
90
|
+
} catch {}
|
|
91
|
+
try { fs.unlinkSync(PID_FILE) } catch {}
|
|
92
|
+
return 0
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 'stop' subcommand
|
|
96
|
+
if (args[0] === 'stop') {
|
|
97
|
+
const pid = checkRunning()
|
|
98
|
+
if (!pid) {
|
|
99
|
+
console.log('[codepocket] Not running')
|
|
100
|
+
process.exit(0)
|
|
101
|
+
}
|
|
102
|
+
try { process.kill(pid, 'SIGTERM') } catch {}
|
|
103
|
+
try { fs.unlinkSync(PID_FILE) } catch {}
|
|
104
|
+
console.log(`[codepocket] Stopped (PID ${pid})`)
|
|
105
|
+
process.exit(0)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// 'logs' subcommand
|
|
109
|
+
if (args[0] === 'logs') {
|
|
110
|
+
const logFile = getLogFile()
|
|
111
|
+
if (!fs.existsSync(logFile)) {
|
|
112
|
+
console.log('[codepocket] No log file for today')
|
|
113
|
+
process.exit(1)
|
|
114
|
+
}
|
|
115
|
+
const lines = parseInt(args[1]) || 50
|
|
116
|
+
try {
|
|
117
|
+
const { execSync } = require('child_process')
|
|
118
|
+
execSync(`tail -n ${lines} -f "${logFile}"`, { stdio: 'inherit' })
|
|
119
|
+
} catch {}
|
|
120
|
+
process.exit(0)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 'status' subcommand
|
|
124
|
+
if (args[0] === 'status') {
|
|
125
|
+
const pid = checkRunning()
|
|
126
|
+
if (pid) {
|
|
127
|
+
console.log(`[codepocket] Running (PID ${pid})`)
|
|
128
|
+
} else {
|
|
129
|
+
console.log('[codepocket] Not running')
|
|
130
|
+
}
|
|
131
|
+
process.exit(0)
|
|
132
|
+
}
|
|
133
|
+
|
|
75
134
|
// Determine mode
|
|
76
135
|
const CONNECT_ONLY = ['connect', 'c'].includes(args[0])
|
|
77
136
|
const filteredArgs = CONNECT_ONLY ? args.slice(1) : args
|
|
@@ -104,11 +163,37 @@ if (relayKey) {
|
|
|
104
163
|
}
|
|
105
164
|
if (!relayUrl) relayUrl = saved.relayUrl || ''
|
|
106
165
|
|
|
166
|
+
// ── Auto-daemonize: if not already a daemon, fork to background ──
|
|
167
|
+
if (!process.env.CODEPOCKET_DAEMON && relayKey) {
|
|
168
|
+
const pid = checkRunning()
|
|
169
|
+
if (pid) {
|
|
170
|
+
console.log(`[codepocket] Already running (PID ${pid})`)
|
|
171
|
+
process.exit(0)
|
|
172
|
+
}
|
|
173
|
+
fs.mkdirSync(LOG_DIR, { recursive: true })
|
|
174
|
+
const logFile = getLogFile()
|
|
175
|
+
const out = fs.openSync(logFile, 'a')
|
|
176
|
+
const daemonArgs = process.argv.slice(2).filter(a => a !== '--key' && !a.startsWith('--key='))
|
|
177
|
+
const child = spawn(process.argv[0], [process.argv[1], ...daemonArgs], {
|
|
178
|
+
stdio: ['ignore', out, out],
|
|
179
|
+
detached: true,
|
|
180
|
+
env: { ...process.env, CODEPOCKET_DAEMON: '1' },
|
|
181
|
+
})
|
|
182
|
+
child.unref()
|
|
183
|
+
fs.writeFileSync(PID_FILE, String(child.pid))
|
|
184
|
+
console.log(`[codepocket] ✓ 已后台启动 (PID ${child.pid})`)
|
|
185
|
+
console.log(` 日志: ${logFile}`)
|
|
186
|
+
console.log(` codepocket stop 停止`)
|
|
187
|
+
console.log(` codepocket logs 查看日志`)
|
|
188
|
+
process.exit(0)
|
|
189
|
+
}
|
|
190
|
+
|
|
107
191
|
// ── State ──
|
|
108
192
|
|
|
109
193
|
let jwt = ''
|
|
110
194
|
let relayWs = null
|
|
111
195
|
let relayPingInterval = null
|
|
196
|
+
let hubBin = findHubBin()
|
|
112
197
|
|
|
113
198
|
// ── Hub ──
|
|
114
199
|
|
|
@@ -261,10 +346,6 @@ function connectRelay() {
|
|
|
261
346
|
handleListDir(msg.requestId, msg.path)
|
|
262
347
|
break
|
|
263
348
|
}
|
|
264
|
-
case 'list-dir': {
|
|
265
|
-
handleListDir(msg.requestId, msg.path)
|
|
266
|
-
break
|
|
267
|
-
}
|
|
268
349
|
}
|
|
269
350
|
})
|
|
270
351
|
|
|
@@ -413,7 +494,6 @@ async function connectInBackground() {
|
|
|
413
494
|
// ── Main ──
|
|
414
495
|
|
|
415
496
|
async function run() {
|
|
416
|
-
const hubBin = findHubBin()
|
|
417
497
|
|
|
418
498
|
// 1. Key setup (interactive if first time)
|
|
419
499
|
if (!relayKey) {
|
|
@@ -447,25 +527,10 @@ async function run() {
|
|
|
447
527
|
await startHub(hubBin)
|
|
448
528
|
}
|
|
449
529
|
|
|
450
|
-
// 3. Connect relay
|
|
530
|
+
// 3. Connect relay (auth + MCP + SSE + relay)
|
|
451
531
|
connectInBackground()
|
|
452
532
|
|
|
453
|
-
|
|
454
|
-
if (CONNECT_ONLY) {
|
|
455
|
-
console.log('[codepocket] 已连接,等待小程序...')
|
|
456
|
-
return
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
if (!hubBin) {
|
|
460
|
-
console.error('[codepocket] ✗ 找不到 Hub 二进制')
|
|
461
|
-
process.exit(1)
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
const child = spawn(hubBin, passThroughArgs, {
|
|
465
|
-
stdio: 'inherit',
|
|
466
|
-
env: { ...process.env, ...HOME_ENV },
|
|
467
|
-
})
|
|
468
|
-
child.on('exit', (code) => process.exit(code || 0))
|
|
533
|
+
console.log('[codepocket] ✓ 已连接,等待小程序...')
|
|
469
534
|
}
|
|
470
535
|
|
|
471
536
|
run().catch((err) => {
|