agent-publish-server 1.0.6 → 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/dist/cli.js +27 -9
- package/dist/config.js +42 -13
- package/dist/server.js +28 -2
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -12,7 +12,33 @@ program
|
|
|
12
12
|
.version(packageJson.version, '-v, --version')
|
|
13
13
|
.option('-c, --config <path>', '配置文件路径')
|
|
14
14
|
.option('--cf <path>', '配置文件路径')
|
|
15
|
-
.option('--config-file <path>', '配置文件路径')
|
|
15
|
+
.option('--config-file <path>', '配置文件路径')
|
|
16
|
+
.option('-p, --port <number>', '服务器端口号')
|
|
17
|
+
.option('-d, --dir <path>', '静态文件目录路径');
|
|
18
|
+
// 添加一个默认命令,用于处理没有指定任何子命令的情况
|
|
19
|
+
program
|
|
20
|
+
.action(() => {
|
|
21
|
+
const options = program.opts();
|
|
22
|
+
// 加载配置
|
|
23
|
+
const config = (0, config_1.loadConfig)(options);
|
|
24
|
+
// 命令行参数优先级高于配置文件
|
|
25
|
+
if (options.port) {
|
|
26
|
+
const port = parseInt(options.port, 10);
|
|
27
|
+
if (isNaN(port)) {
|
|
28
|
+
console.error('端口号必须是有效的数字');
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
config.port = port;
|
|
32
|
+
}
|
|
33
|
+
if (options.dir) {
|
|
34
|
+
config.dir = options.dir;
|
|
35
|
+
}
|
|
36
|
+
// 启动服务器
|
|
37
|
+
(0, server_1.startServer)(config).catch((error) => {
|
|
38
|
+
console.error('Server failed to start:', error);
|
|
39
|
+
process.exit(1);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
16
42
|
program
|
|
17
43
|
.command('init')
|
|
18
44
|
.description('初始化配置文件')
|
|
@@ -36,11 +62,3 @@ program
|
|
|
36
62
|
process.exit(0);
|
|
37
63
|
});
|
|
38
64
|
program.parse(process.argv);
|
|
39
|
-
if (!program.args.length) {
|
|
40
|
-
const options = program.opts();
|
|
41
|
-
const config = (0, config_1.loadConfig)(options);
|
|
42
|
-
(0, server_1.startServer)(config).catch((error) => {
|
|
43
|
-
console.error('Server failed to start:', error);
|
|
44
|
-
process.exit(1);
|
|
45
|
-
});
|
|
46
|
-
}
|
package/dist/config.js
CHANGED
|
@@ -12,20 +12,49 @@ const DEFAULT_CONFIG = {
|
|
|
12
12
|
proxy: {}
|
|
13
13
|
};
|
|
14
14
|
function loadConfig(options) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
options.
|
|
18
|
-
options.
|
|
19
|
-
|
|
15
|
+
// 尝试多个可能的配置文件路径
|
|
16
|
+
const configPaths = [
|
|
17
|
+
options.config || '',
|
|
18
|
+
options.c || '',
|
|
19
|
+
options.cf || '',
|
|
20
|
+
options.configFile || '',
|
|
21
|
+
'agent_config.json',
|
|
22
|
+
'.agent_config.json',
|
|
23
|
+
'agent.config.json'
|
|
24
|
+
].filter(Boolean);
|
|
25
|
+
let configPath = '';
|
|
26
|
+
let absolutePath = '';
|
|
20
27
|
try {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
// 遍历所有可能的配置文件路径
|
|
29
|
+
for (const cfgPath of configPaths) {
|
|
30
|
+
if (path_1.default.isAbsolute(cfgPath)) {
|
|
31
|
+
// 如果是绝对路径,直接检查文件是否存在
|
|
32
|
+
if (fs_1.default.existsSync(cfgPath)) {
|
|
33
|
+
absolutePath = cfgPath;
|
|
34
|
+
configPath = cfgPath;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else if (cfgPath) {
|
|
39
|
+
// 如果是相对路径,尝试多个可能的路径
|
|
40
|
+
const possiblePaths = [
|
|
41
|
+
path_1.default.resolve(process.cwd(), cfgPath),
|
|
42
|
+
path_1.default.resolve(__dirname, cfgPath)
|
|
43
|
+
];
|
|
44
|
+
for (const p of possiblePaths) {
|
|
45
|
+
if (fs_1.default.existsSync(p)) {
|
|
46
|
+
absolutePath = p;
|
|
47
|
+
configPath = cfgPath;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (absolutePath)
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!absolutePath) {
|
|
56
|
+
console.log('未找到配置文件,将使用默认配置');
|
|
57
|
+
return DEFAULT_CONFIG;
|
|
29
58
|
}
|
|
30
59
|
const configContent = fs_1.default.readFileSync(absolutePath, 'utf-8');
|
|
31
60
|
const userConfig = JSON.parse(configContent);
|
package/dist/server.js
CHANGED
|
@@ -7,16 +7,42 @@ exports.createServer = createServer;
|
|
|
7
7
|
exports.startServer = startServer;
|
|
8
8
|
const express_1 = __importDefault(require("express"));
|
|
9
9
|
const http_proxy_middleware_1 = require("http-proxy-middleware");
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
11
|
+
const fs_1 = __importDefault(require("fs"));
|
|
10
12
|
function createServer(config) {
|
|
11
13
|
const app = (0, express_1.default)();
|
|
12
|
-
|
|
13
|
-
app.use(express_1.default.static(config.dir || './'));
|
|
14
|
+
const staticDir = config.dir || './';
|
|
14
15
|
// 配置代理
|
|
15
16
|
if (config.proxy) {
|
|
16
17
|
Object.entries(config.proxy).forEach(([path, proxyConfig]) => {
|
|
17
18
|
app.use(path, (0, http_proxy_middleware_1.createProxyMiddleware)(proxyConfig));
|
|
18
19
|
});
|
|
19
20
|
}
|
|
21
|
+
// 配置静态文件服务
|
|
22
|
+
app.use(express_1.default.static(staticDir));
|
|
23
|
+
// 添加SPA应用的回退路由处理
|
|
24
|
+
// 只有当实际文件不存在时才回退到index.html
|
|
25
|
+
app.use((req, res, next) => {
|
|
26
|
+
// 排除API和已处理的代理请求
|
|
27
|
+
if (req.path.startsWith('/api/')) {
|
|
28
|
+
return next();
|
|
29
|
+
}
|
|
30
|
+
const filePath = path_1.default.join(process.cwd(), staticDir, req.path);
|
|
31
|
+
// 检查是否为目录
|
|
32
|
+
if (fs_1.default.existsSync(filePath) && fs_1.default.statSync(filePath).isDirectory()) {
|
|
33
|
+
// 检查目录中是否有index.html
|
|
34
|
+
const indexPath = path_1.default.join(filePath, 'index.html');
|
|
35
|
+
if (fs_1.default.existsSync(indexPath)) {
|
|
36
|
+
return res.sendFile(indexPath);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// 检查完整路径(如果是文件)
|
|
40
|
+
if (fs_1.default.existsSync(filePath) && fs_1.default.statSync(filePath).isFile()) {
|
|
41
|
+
return res.sendFile(filePath);
|
|
42
|
+
}
|
|
43
|
+
// 如果路径不存在,回退到index.html
|
|
44
|
+
res.sendFile(path_1.default.join(process.cwd(), staticDir, 'index.html'));
|
|
45
|
+
});
|
|
20
46
|
return app;
|
|
21
47
|
}
|
|
22
48
|
function startServer(config) {
|
package/dist/types.d.ts
CHANGED