@kevisual/cnb 0.0.29 → 0.0.31

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/cnb",
3
- "version": "0.0.29",
3
+ "version": "0.0.31",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -52,6 +52,7 @@
52
52
  "./opencode": "./dist/opencode.js",
53
53
  "./keep": "./dist/keep.js",
54
54
  "./keep.ts": "./src/keep.ts",
55
+ "./keep-file-live.ts": "./src/workspace/keep-file-live.ts",
55
56
  "./routes": "./dist/routes.js",
56
57
  "./src/*": "./src/*",
57
58
  "./agent/*": "./agent/*"
@@ -0,0 +1,137 @@
1
+ import path from 'node:path';
2
+ import fs from 'node:fs';
3
+ import os from 'node:os';
4
+ import { execSync } from 'node:child_process';
5
+
6
+ export type KeepAliveData = {
7
+ wsUrl: string;
8
+ cookie: string;
9
+ repo: string;
10
+ pipelineId: string;
11
+ createdTime: number;
12
+ filePath: string;
13
+ pm2Name: string;
14
+ }
15
+
16
+ type KeepAliveCache = {
17
+ data: KeepAliveData[];
18
+ }
19
+
20
+ const keepAliveFilePath = path.join(os.homedir(), '.cnb/keepAliveCache.json');
21
+
22
+ export const runLive = (filePath: string, pm2Name: string) => {
23
+ // 使用 npx 运行命令
24
+ const cmdArgs = `cnb live -c ${filePath}`;
25
+
26
+ // 先停止已存在的同名 pm2 进程
27
+ const stopCmd = `pm2 delete ${pm2Name} 2>/dev/null || true`;
28
+ console.log('停止已存在的进程:', stopCmd);
29
+ try {
30
+ execSync(stopCmd, { stdio: 'inherit' });
31
+ } catch (error) {
32
+ console.log('停止进程失败或进程不存在:', error);
33
+ }
34
+
35
+ // 使用pm2启动
36
+ const pm2Cmd = `pm2 start ev --name ${pm2Name} --no-autorestart -- ${cmdArgs}`;
37
+ console.log('执行命令:', pm2Cmd);
38
+ try {
39
+ const result = execSync(pm2Cmd, { stdio: 'pipe', encoding: 'utf8' });
40
+ console.log(result);
41
+ } catch (error) {
42
+ console.error("状态码:", error.status);
43
+ console.error("错误详情:", error.stderr.toString()); // 这里会显示 ev 命令报的具体错误
44
+ }
45
+ }
46
+
47
+ export const stopLive = (pm2Name: string): boolean => {
48
+ const stopCmd = `pm2 delete ${pm2Name} 2>/dev/null || true`;
49
+ console.log('停止进程:', stopCmd);
50
+ try {
51
+ execSync(stopCmd, { stdio: 'inherit' });
52
+ console.log(`已停止 ${pm2Name} 的保持存活任务`);
53
+ return true;
54
+ } catch (error) {
55
+ console.error('停止进程失败:', error);
56
+ }
57
+ return false;
58
+ }
59
+
60
+ export function getKeepAliveCache(): KeepAliveCache {
61
+ try {
62
+ if (fs.existsSync(keepAliveFilePath)) {
63
+ const data = fs.readFileSync(keepAliveFilePath, 'utf-8');
64
+ const cache = JSON.parse(data) as KeepAliveCache;
65
+ return cache;
66
+ } else {
67
+ return { data: [] };
68
+ }
69
+ } catch (error) {
70
+ console.error('读取保持存活缓存文件失败:', error);
71
+ return { data: [] };
72
+ }
73
+ }
74
+
75
+ export function addKeepAliveData(data: KeepAliveData): KeepAliveCache {
76
+ const cache = getKeepAliveCache();
77
+ cache.data.push(data);
78
+ runLive(data.filePath, data.pm2Name);
79
+ try {
80
+ if (!fs.existsSync(path.dirname(keepAliveFilePath))) {
81
+ fs.mkdirSync(path.dirname(keepAliveFilePath), { recursive: true });
82
+ }
83
+ fs.writeFileSync(keepAliveFilePath, JSON.stringify(cache, null, 2), 'utf-8');
84
+ return cache;
85
+ } catch (error) {
86
+ console.error('写入保持存活缓存文件失败:', error);
87
+ return { data: [] };
88
+ }
89
+ }
90
+
91
+ export function removeKeepAliveData(repo: string, pipelineId: string): KeepAliveCache {
92
+ const cache = getKeepAliveCache();
93
+ const item = cache.data.find(item => item.repo === repo && item.pipelineId === pipelineId);
94
+ if (item) {
95
+ stopLive(item.pm2Name);
96
+ }
97
+ cache.data = cache.data.filter(item => item.repo !== repo || item.pipelineId !== pipelineId);
98
+ try {
99
+ fs.writeFileSync(keepAliveFilePath, JSON.stringify(cache, null, 2), 'utf-8');
100
+ return cache;
101
+ } catch (error) {
102
+ console.error('写入保持存活缓存文件失败:', error);
103
+ return { data: [] };
104
+ }
105
+ }
106
+
107
+ export const createLiveData = (data: { cookie: string, repo: string, pipelineId: string }): KeepAliveData => {
108
+ const { cookie, repo, pipelineId } = data;
109
+ const createdTime = Date.now();
110
+ const wsUrl = `wss://${pipelineId}.cnb.space:443?skipWebSocketFrames=false`;
111
+ const pm2Name = `${repo}__${pipelineId}`.replace(/\//g, '__');
112
+ const filePath = path.join(os.homedir(), '.cnb', `${pm2Name}.json`);
113
+ const _newData = { wss: wsUrl, wsUrl, cookie, repo, pipelineId, createdTime, filePath, pm2Name };
114
+ if (!fs.existsSync(path.dirname(filePath))) {
115
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
116
+ }
117
+ fs.writeFileSync(filePath, JSON.stringify(_newData, null, 2), 'utf-8');
118
+ return _newData;
119
+ }
120
+
121
+ export class KeepAliveManager {
122
+ static getCache() {
123
+ return getKeepAliveCache();
124
+ }
125
+
126
+ static add(data: KeepAliveData) {
127
+ return addKeepAliveData(data);
128
+ }
129
+
130
+ static createLiveData(data: { cookie: string, repo: string, pipelineId: string }): KeepAliveData {
131
+ return createLiveData(data);
132
+ }
133
+
134
+ static remove(repo: string, pipelineId: string) {
135
+ return removeKeepAliveData(repo, pipelineId);
136
+ }
137
+ }