fdb2 1.0.4 → 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 +54 -1
- package/package.json +1 -1
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 文件是否存在,如果存在则说明服务器已经在运行
|
|
@@ -55,6 +96,18 @@ function startProject() {
|
|
|
55
96
|
port = parseInt(commandArgs[portIndex + 1]);
|
|
56
97
|
}
|
|
57
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
|
+
|
|
58
111
|
// 命令和参数
|
|
59
112
|
let cmd, args;
|
|
60
113
|
|