Skip to content

Commit 197d8ec

Browse files
committed
feat: enhance main function execution logic with path normalization
- Added realpathSync and fileURLToPath imports to handle path normalization when executing the main function. - Updated the main function execution logic to ensure it runs correctly when invoked via npx or global bin symlinks, improving reliability. - Added error handling to maintain existing functionality while enhancing path comparison accuracy.
1 parent f19dc46 commit 197d8ec

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

src/server.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import {
3939
} from './tools/handshake-tools.js';
4040
import { ulid } from 'ulid';
4141
import * as path from 'path';
42+
import { realpathSync } from 'fs';
43+
import { fileURLToPath } from 'url';
4244

4345
/**
4446
* WaveForge MCP 服务器类
@@ -1060,9 +1062,25 @@ process.on('unhandledRejection', (reason, promise) => {
10601062
});
10611063

10621064
// 启动主函数
1063-
if (import.meta.url === `file://${process.argv[1]}`) {
1064-
main().catch((error) => {
1065-
console.error('[WaveForge] 主函数执行失败:', error);
1066-
process.exit(1);
1067-
});
1068-
}
1065+
// 说明:当通过 npx 或全局 bin 符号链接启动时,process.argv[1] 可能与实际脚本不同。
1066+
// 使用 realpath 同步规范化路径进行对比,避免主程序不运行。
1067+
(() => {
1068+
try {
1069+
const metaPath = realpathSync(fileURLToPath(import.meta.url));
1070+
const argv1 = process.argv[1];
1071+
const argvPath = argv1 ? realpathSync(argv1) : '';
1072+
if (metaPath === argvPath) {
1073+
void main().catch((error) => {
1074+
console.error('[WaveForge] 主函数执行失败:', error);
1075+
process.exit(1);
1076+
});
1077+
}
1078+
} catch (_e) {
1079+
if (import.meta.url === `file://${process.argv[1]}`) {
1080+
void main().catch((error) => {
1081+
console.error('[WaveForge] 主函数执行失败:', error);
1082+
process.exit(1);
1083+
});
1084+
}
1085+
}
1086+
})();

0 commit comments

Comments
 (0)