agent-publish-server 1.0.5 → 1.0.12
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/README.md +20 -1
- package/dist/cli.js +27 -9
- package/dist/config.js +53 -12
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,11 +16,30 @@ agent-publish-server
|
|
|
16
16
|
|
|
17
17
|
## 配置
|
|
18
18
|
|
|
19
|
+
### 配置文件路径
|
|
20
|
+
|
|
21
|
+
你可以通过以下方式指定配置文件路径:
|
|
22
|
+
|
|
23
|
+
1. 使用相对路径(相对于当前工作目录):
|
|
24
|
+
|
|
19
25
|
```bash
|
|
20
|
-
# 配置文件路径
|
|
21
26
|
agent-publish-server -c ./agent_config.json
|
|
22
27
|
```
|
|
23
28
|
|
|
29
|
+
2. 使用绝对路径:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
agent-publish-server -c /path/to/agent_config.json
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
3. 使用初始化命令创建默认配置:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
agent-publish-server init
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 配置文件格式
|
|
42
|
+
|
|
24
43
|
```json
|
|
25
44
|
{
|
|
26
45
|
"port": 8080, // 端口号
|
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,21 +12,62 @@ 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
|
-
|
|
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
|
+
}
|
|
26
54
|
}
|
|
55
|
+
if (!absolutePath) {
|
|
56
|
+
console.log('未找到配置文件,将使用默认配置');
|
|
57
|
+
return DEFAULT_CONFIG;
|
|
58
|
+
}
|
|
59
|
+
const configContent = fs_1.default.readFileSync(absolutePath, 'utf-8');
|
|
60
|
+
const userConfig = JSON.parse(configContent);
|
|
61
|
+
return { ...DEFAULT_CONFIG, ...userConfig };
|
|
27
62
|
}
|
|
28
63
|
catch (error) {
|
|
29
|
-
|
|
64
|
+
if (error instanceof SyntaxError) {
|
|
65
|
+
console.error(`配置文件格式错误: ${configPath}`);
|
|
66
|
+
console.error('请确保配置文件是有效的 JSON 格式');
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
console.error(`加载配置文件失败: ${error.message}`);
|
|
70
|
+
}
|
|
71
|
+
process.exit(1);
|
|
30
72
|
}
|
|
31
|
-
return DEFAULT_CONFIG;
|
|
32
73
|
}
|
package/dist/types.d.ts
CHANGED