fdb2 1.0.3 → 1.0.5

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
@@ -3,6 +3,7 @@
3
3
  const { exec, spawn, execSync, spawnSync } = require('child_process');
4
4
  const path = require('path');
5
5
  const fs = require('fs');
6
+ const net = require('net');
6
7
 
7
8
  // 项目根目录
8
9
  const projectRoot = path.resolve(__dirname, '..');
@@ -28,8 +29,48 @@ switch (command) {
28
29
  break;
29
30
  }
30
31
 
32
+ // 检查端口是否可用
33
+ function isPortAvailable(port) {
34
+ return new Promise((resolve) => {
35
+ const server = net.createServer();
36
+
37
+ server.once('error', (err) => {
38
+ if (err.code === 'EADDRINUSE') {
39
+ resolve(false);
40
+ } else {
41
+ resolve(false);
42
+ }
43
+ });
44
+
45
+ server.once('listening', () => {
46
+ server.close();
47
+ resolve(true);
48
+ });
49
+
50
+ server.listen(port);
51
+ });
52
+ }
53
+
54
+ // 查找可用端口
55
+ async function findAvailablePort(startPort) {
56
+ let port = startPort;
57
+ let attempts = 0;
58
+ const maxAttempts = 100;
59
+
60
+ while (attempts < maxAttempts) {
61
+ const available = await isPortAvailable(port);
62
+ if (available) {
63
+ return port;
64
+ }
65
+ attempts++;
66
+ port++;
67
+ }
68
+
69
+ throw new Error(`无法找到可用端口,已尝试 ${maxAttempts} 次`);
70
+ }
71
+
31
72
  // 启动项目
32
- function startProject() {
73
+ async function startProject() {
33
74
  console.log('Starting FDB2 project...');
34
75
 
35
76
  // 检查 PID 文件是否存在,如果存在则说明服务器已经在运行
@@ -48,12 +89,31 @@ function startProject() {
48
89
  }
49
90
  }
50
91
 
92
+ // 解析端口参数
93
+ let port = 9800;
94
+ const portIndex = commandArgs.findIndex(arg => arg === '-p' || arg === '--port');
95
+ if (portIndex !== -1 && commandArgs[portIndex + 1]) {
96
+ port = parseInt(commandArgs[portIndex + 1]);
97
+ }
98
+
99
+ // 查找可用端口
100
+ console.log(`Checking port ${port} availability...`);
101
+ try {
102
+ port = await findAvailablePort(port);
103
+ if (port !== parseInt(commandArgs[portIndex + 1] || 9800)) {
104
+ console.log(`Port ${commandArgs[portIndex + 1] || 9800} is in use, using port ${port} instead`);
105
+ }
106
+ } catch (error) {
107
+ console.error('Error:', error.message);
108
+ process.exit(1);
109
+ }
110
+
51
111
  // 命令和参数
52
112
  let cmd, args;
53
113
 
54
- // 直接使用 node 命令启动服务器
114
+ // 直接使用 node 命令启动服务器,传递端口参数
55
115
  cmd = 'node';
56
- args = ['server.js', ...commandArgs];
116
+ args = ['server.js', '-p', port.toString()];
57
117
 
58
118
  console.log('Executing:', cmd, args);
59
119
 
@@ -77,9 +137,10 @@ function startProject() {
77
137
  // 保存 PID 到文件
78
138
  fs.writeFileSync(pidFilePath, child.pid.toString());
79
139
 
140
+ console.log('Logs are written to:', logFilePath);
80
141
  console.log('Server started successfully with PID:', child.pid);
81
142
  console.log('Server is running in the background');
82
- console.log('Logs are written to:', logFilePath);
143
+ console.log(`Server is running at http://localhost:${port}`);
83
144
  }
84
145
 
85
146
  // 停止项目
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fdb2",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "private": false,
5
5
  "type": "commonjs",
6
6
  "main": "view/index.html",
@@ -16,6 +16,7 @@
16
16
  "icon": "public/favicon.ico"
17
17
  },
18
18
  "scripts": {
19
+ "preinstall": "node scripts/preinstall.js",
19
20
  "start": "pm2 start server.js --name fdb2-server --disable-logs --",
20
21
  "restart": "pm2 restart fdb2-server --disable-logs --",
21
22
  "stop": "pm2 stop fdb2-server --",
@@ -0,0 +1,38 @@
1
+ const { execSync } = require('child_process');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+
5
+ console.log('Checking for running fdb2 instances...');
6
+
7
+ try {
8
+ const projectRoot = path.resolve(__dirname, '..');
9
+ const pidFilePath = path.join(projectRoot, 'server.pid');
10
+
11
+ if (fs.existsSync(pidFilePath)) {
12
+ try {
13
+ const pid = parseInt(fs.readFileSync(pidFilePath, 'utf8'));
14
+ process.kill(pid, 0);
15
+ console.log('Found running fdb2 instance with PID:', pid);
16
+ console.log('Stopping fdb2...');
17
+
18
+ try {
19
+ execSync('node bin/fdb2.js stop', {
20
+ cwd: projectRoot,
21
+ stdio: 'inherit'
22
+ });
23
+ console.log('fdb2 stopped successfully');
24
+ } catch (stopError) {
25
+ console.warn('Failed to stop fdb2:', stopError.message);
26
+ }
27
+ } catch (error) {
28
+ if (error.code === 'ESRCH') {
29
+ console.log('Cleaning up stale PID file...');
30
+ fs.unlinkSync(pidFilePath);
31
+ }
32
+ }
33
+ } else {
34
+ console.log('No running fdb2 instance found');
35
+ }
36
+ } catch (error) {
37
+ console.warn('Pre-install check failed:', error.message);
38
+ }
package/server.pid DELETED
@@ -1 +0,0 @@
1
- 7684