fdb2 1.0.11 → 1.0.13

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/fdb2.js CHANGED
@@ -4,10 +4,43 @@ const { exec, spawn, execSync, spawnSync } = require('child_process');
4
4
  const path = require('path');
5
5
  const fs = require('fs');
6
6
  const net = require('net');
7
+ const os = require('os');
7
8
 
8
9
  // 项目根目录
9
10
  const projectRoot = path.resolve(__dirname, '..');
10
11
 
12
+ // 获取 PID 文件路径
13
+ function getPidFilePath() {
14
+ const dataDir = process.env.DB_TOOL_DATA_DIR || path.join(os.homedir(), '.fdb2');
15
+ if (!fs.existsSync(dataDir)) {
16
+ fs.mkdirSync(dataDir, { recursive: true });
17
+ }
18
+ return path.join(dataDir, 'fdb2.server.pid');
19
+ }
20
+
21
+ // 读取 PID
22
+ function readPid() {
23
+ const pidFilePath = getPidFilePath();
24
+ if (fs.existsSync(pidFilePath)) {
25
+ return parseInt(fs.readFileSync(pidFilePath, 'utf8'));
26
+ }
27
+ return null;
28
+ }
29
+
30
+ // 写入 PID
31
+ function writePid(pid) {
32
+ const pidFilePath = getPidFilePath();
33
+ fs.writeFileSync(pidFilePath, pid.toString());
34
+ }
35
+
36
+ // 删除 PID
37
+ function deletePid() {
38
+ const pidFilePath = getPidFilePath();
39
+ if (fs.existsSync(pidFilePath)) {
40
+ fs.unlinkSync(pidFilePath);
41
+ }
42
+ }
43
+
11
44
  // 解析命令行参数
12
45
  const args = process.argv.slice(2);
13
46
  const command = args[0] || 'help';
@@ -74,17 +107,16 @@ async function startProject() {
74
107
  console.log('Starting FDB2 project...');
75
108
 
76
109
  // 检查 PID 文件是否存在,如果存在则说明服务器已经在运行
77
- const pidFilePath = path.join(projectRoot, 'fdb2.server.pid');
78
- if (fs.existsSync(pidFilePath)) {
110
+ const pid = readPid();
111
+ if (pid) {
79
112
  try {
80
- const pid = parseInt(fs.readFileSync(pidFilePath, 'utf8'));
81
113
  process.kill(pid, 0);
82
114
  console.log('Server is already running with PID:', pid);
83
115
  return;
84
116
  } catch (error) {
85
117
  if (error.code === 'ESRCH') {
86
118
  console.log('Cleaning up stale PID file...');
87
- fs.unlinkSync(pidFilePath);
119
+ deletePid();
88
120
  }
89
121
  }
90
122
  }
@@ -147,11 +179,7 @@ async function startProject() {
147
179
  child.unref();
148
180
 
149
181
  // 保存 PID 到文件
150
- try {
151
- fs.writeFileSync(pidFilePath, child.pid.toString());
152
- } catch (error) {
153
- console.warn(`Failed to write PID file ${pidFilePath}: ${error.message}`);
154
- }
182
+ writePid(child.pid);
155
183
 
156
184
  if (logFileFd) {
157
185
  console.log('Logs are written to:', logFilePath);
@@ -167,17 +195,15 @@ async function startProject() {
167
195
  function stopProject() {
168
196
  console.log('Stopping FDB2 project...');
169
197
 
170
- // 读取 PID 文件
171
- const pidFilePath = path.join(projectRoot, 'fdb2.server.pid');
198
+ // 读取 PID
199
+ const pid = readPid();
172
200
 
173
- if (!fs.existsSync(pidFilePath)) {
201
+ if (!pid) {
174
202
  console.log('No server process found (PID file not exists)');
175
203
  return;
176
204
  }
177
205
 
178
206
  try {
179
- // 读取 PID
180
- const pid = parseInt(fs.readFileSync(pidFilePath, 'utf8'));
181
207
  console.log(`Stopping server process with PID: ${pid}`);
182
208
 
183
209
  // 发送终止信号 - 尝试不同的信号
@@ -240,28 +266,13 @@ function stopProject() {
240
266
  }
241
267
 
242
268
  // 删除 PID 文件
243
- try {
244
- fs.unlinkSync(pidFilePath);
245
- console.log('PID file removed');
246
- } catch (unlinkError) {
247
- console.warn(`Failed to remove PID file: ${unlinkError.message}`);
248
- }
249
-
269
+ deletePid();
250
270
  console.log('Server stopped successfully');
251
271
  } catch (error) {
252
272
  // 如果进程不存在(ESRCH 错误),也删除 PID 文件
253
273
  if (error.code === 'ESRCH') {
254
274
  console.log('Server process not found, cleaning up PID file');
255
- if (fs.existsSync(pidFilePath)) {
256
- try {
257
- fs.unlinkSync(pidFilePath);
258
- } catch (e) {
259
- // 忽略删除失败
260
- }
261
- }
262
- } else if (error.code === 'EPERM') {
263
- // 权限不足的错误已经在上面处理过
264
- console.error('Cannot stop server due to insufficient permissions');
275
+ deletePid();
265
276
  } else {
266
277
  console.error('Failed to stop server:', error.message);
267
278
  }
@@ -285,8 +296,7 @@ function restartProject() {
285
296
  console.log('Waiting for server process to stop...');
286
297
  for (let i = 0; i < 10; i++) {
287
298
  // 检查 PID 文件是否存在
288
- const pidFilePath = path.join(projectRoot, 'fdb2.server.pid');
289
- if (!fs.existsSync(pidFilePath)) {
299
+ if (!readPid()) {
290
300
  break;
291
301
  }
292
302
  // 等待 100 毫秒