fdb2 1.0.0 → 1.0.1
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 +40 -23
- package/package.json +1 -1
- package/server.js +23 -1
- package/vite.config.ts +1 -1
package/bin/fdb2.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
const { exec, spawn, execSync, spawnSync } = require('child_process');
|
|
4
4
|
const path = require('path');
|
|
@@ -32,37 +32,54 @@ switch (command) {
|
|
|
32
32
|
function startProject() {
|
|
33
33
|
console.log('Starting FDB2 project...');
|
|
34
34
|
|
|
35
|
+
// 检查 PID 文件是否存在,如果存在则说明服务器已经在运行
|
|
36
|
+
const pidFilePath = path.join(projectRoot, 'server.pid');
|
|
37
|
+
if (fs.existsSync(pidFilePath)) {
|
|
38
|
+
try {
|
|
39
|
+
const pid = parseInt(fs.readFileSync(pidFilePath, 'utf8'));
|
|
40
|
+
process.kill(pid, 0);
|
|
41
|
+
console.log('Server is already running with PID:', pid);
|
|
42
|
+
return;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (error.code === 'ESRCH') {
|
|
45
|
+
console.log('Cleaning up stale PID file...');
|
|
46
|
+
fs.unlinkSync(pidFilePath);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
35
51
|
// 命令和参数
|
|
36
52
|
let cmd, args;
|
|
37
53
|
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
cmd = 'cmd.exe';
|
|
42
|
-
args = ['/c', 'node', 'server.js', ...commandArgs];
|
|
43
|
-
} else {
|
|
44
|
-
// Linux/macOS 系统
|
|
45
|
-
cmd = 'node';
|
|
46
|
-
args = ['server.js', ...commandArgs];
|
|
47
|
-
}
|
|
54
|
+
// 直接使用 node 命令启动服务器
|
|
55
|
+
cmd = 'node';
|
|
56
|
+
args = ['server.js', ...commandArgs];
|
|
48
57
|
|
|
49
58
|
console.log('Executing:', cmd, args);
|
|
50
59
|
|
|
51
|
-
//
|
|
52
|
-
const
|
|
60
|
+
// 日志文件路径
|
|
61
|
+
const logFilePath = path.join(projectRoot, 'server.log');
|
|
62
|
+
|
|
63
|
+
// 创建日志文件的写入流
|
|
64
|
+
const out = fs.openSync(logFilePath, 'a');
|
|
65
|
+
const err = fs.openSync(logFilePath, 'a');
|
|
66
|
+
|
|
67
|
+
// 使用 node 命令启动服务器(异步,后台运行)
|
|
68
|
+
const child = spawn(cmd, args, {
|
|
53
69
|
cwd: projectRoot,
|
|
54
|
-
|
|
70
|
+
detached: true,
|
|
71
|
+
stdio: ['ignore', out, err]
|
|
55
72
|
});
|
|
56
73
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
// 解除父子进程关联,让子进程在后台独立运行
|
|
75
|
+
child.unref();
|
|
76
|
+
|
|
77
|
+
// 保存 PID 到文件
|
|
78
|
+
fs.writeFileSync(pidFilePath, child.pid.toString());
|
|
79
|
+
|
|
80
|
+
console.log('Server started successfully with PID:', child.pid);
|
|
81
|
+
console.log('Server is running in the background');
|
|
82
|
+
console.log('Logs are written to:', logFilePath);
|
|
66
83
|
}
|
|
67
84
|
|
|
68
85
|
// 停止项目
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -4,6 +4,28 @@ const express = require('express');
|
|
|
4
4
|
const path = require('path');
|
|
5
5
|
const fs = require('fs');
|
|
6
6
|
|
|
7
|
+
// 预加载 sqlite3 模块,确保原生绑定文件能够正确加载
|
|
8
|
+
// 并将其缓存到全局模块缓存中,以便 dist/server/index.js 可以使用
|
|
9
|
+
try {
|
|
10
|
+
const sqlite3 = require('sqlite3');
|
|
11
|
+
console.log('SQLite3 module preloaded successfully');
|
|
12
|
+
|
|
13
|
+
// 将 sqlite3 模块添加到全局 require.cache 中
|
|
14
|
+
const Module = require('module');
|
|
15
|
+
const sqlite3Path = require.resolve('sqlite3');
|
|
16
|
+
|
|
17
|
+
// 修改模块解析函数,确保 sqlite3 从正确的路径加载
|
|
18
|
+
const originalResolveFilename = Module._resolveFilename;
|
|
19
|
+
Module._resolveFilename = function(request, parent) {
|
|
20
|
+
if (request === 'sqlite3') {
|
|
21
|
+
return sqlite3Path;
|
|
22
|
+
}
|
|
23
|
+
return originalResolveFilename.call(this, request, parent);
|
|
24
|
+
};
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.error('Warning: Failed to preload sqlite3 module:', error.message);
|
|
27
|
+
}
|
|
28
|
+
|
|
7
29
|
// 日志文件路径 - 使用绝对路径
|
|
8
30
|
const logFilePath = path.resolve(__dirname, 'server.log');
|
|
9
31
|
|
|
@@ -96,7 +118,7 @@ for (let i = 0; i < process.argv.length; i++) {
|
|
|
96
118
|
}
|
|
97
119
|
|
|
98
120
|
// 启动服务器
|
|
99
|
-
const PORT = portFromArgs || process.env.PORT ||
|
|
121
|
+
const PORT = portFromArgs || process.env.PORT || 9800;
|
|
100
122
|
app.listen(PORT, () => {
|
|
101
123
|
|
|
102
124
|
// 将 PID 写入 PID 文件
|