agent-publish-server 1.0.0
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 +49 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +19 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.js +32 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.js +37 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.js +2 -0
- package/package.json +41 -0
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
## agent-publish-server
|
|
2
|
+
|
|
3
|
+
> 这是一个基于 nodejs+express+http-proxy-middleware 的一款可支持代理的前端服务启动插件
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install agent-publish-server -g
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
agent-publish-server
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 配置
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# 配置文件路径
|
|
21
|
+
agent-publish-server -c ./agent_config.json
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```json
|
|
25
|
+
{
|
|
26
|
+
"port": 8080, // 端口号
|
|
27
|
+
"dir": "./", // 静态文件目录
|
|
28
|
+
"proxy": {
|
|
29
|
+
"/api": {
|
|
30
|
+
// 代理路径
|
|
31
|
+
"target": "URL_ADDRESS", // 代理地址
|
|
32
|
+
"changeOrigin": true, // 是否改变域名
|
|
33
|
+
"pathRewrite": {
|
|
34
|
+
// 路径重写
|
|
35
|
+
"^/api": "" // 重写后路径
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## 说明
|
|
43
|
+
|
|
44
|
+
> 1. 配置文件路径为 agent_config.json
|
|
45
|
+
> 2. 配置文件路径可以通过 -c 来指定
|
|
46
|
+
> 3. 配置文件路径可以通过 --config 来指定
|
|
47
|
+
> 4. 配置文件路径可以通过 --cf 来指定
|
|
48
|
+
> 5. 配置文件路径可以通过 --config-file 来指定
|
|
49
|
+
> 6. 适配 react vue 等前端框架
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const commander_1 = require("commander");
|
|
5
|
+
const config_1 = require("./config");
|
|
6
|
+
const server_1 = require("./server");
|
|
7
|
+
const program = new commander_1.Command();
|
|
8
|
+
program
|
|
9
|
+
.version('1.0.0')
|
|
10
|
+
.option('-c, --config <path>', '配置文件路径')
|
|
11
|
+
.option('--cf <path>', '配置文件路径')
|
|
12
|
+
.option('--config-file <path>', '配置文件路径')
|
|
13
|
+
.parse(process.argv);
|
|
14
|
+
const options = program.opts();
|
|
15
|
+
const config = (0, config_1.loadConfig)(options);
|
|
16
|
+
(0, server_1.startServer)(config).catch((error) => {
|
|
17
|
+
console.error('Server failed to start:', error);
|
|
18
|
+
process.exit(1);
|
|
19
|
+
});
|
package/dist/config.d.ts
ADDED
package/dist/config.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.loadConfig = loadConfig;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const DEFAULT_CONFIG = {
|
|
10
|
+
port: 8080,
|
|
11
|
+
dir: './',
|
|
12
|
+
proxy: {}
|
|
13
|
+
};
|
|
14
|
+
function loadConfig(options) {
|
|
15
|
+
const configPath = options.config ||
|
|
16
|
+
options.c ||
|
|
17
|
+
options.cf ||
|
|
18
|
+
options.configFile ||
|
|
19
|
+
'agent_config.json';
|
|
20
|
+
try {
|
|
21
|
+
const absolutePath = path_1.default.resolve(process.cwd(), configPath);
|
|
22
|
+
if (fs_1.default.existsSync(absolutePath)) {
|
|
23
|
+
const configContent = fs_1.default.readFileSync(absolutePath, 'utf-8');
|
|
24
|
+
const userConfig = JSON.parse(configContent);
|
|
25
|
+
return { ...DEFAULT_CONFIG, ...userConfig };
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
console.warn(`Failed to load config file: ${error}`);
|
|
30
|
+
}
|
|
31
|
+
return DEFAULT_CONFIG;
|
|
32
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadConfig = exports.createServer = exports.startServer = void 0;
|
|
4
|
+
var server_1 = require("./server");
|
|
5
|
+
Object.defineProperty(exports, "startServer", { enumerable: true, get: function () { return server_1.startServer; } });
|
|
6
|
+
Object.defineProperty(exports, "createServer", { enumerable: true, get: function () { return server_1.createServer; } });
|
|
7
|
+
var config_1 = require("./config");
|
|
8
|
+
Object.defineProperty(exports, "loadConfig", { enumerable: true, get: function () { return config_1.loadConfig; } });
|
package/dist/server.d.ts
ADDED
package/dist/server.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createServer = createServer;
|
|
7
|
+
exports.startServer = startServer;
|
|
8
|
+
const express_1 = __importDefault(require("express"));
|
|
9
|
+
const http_proxy_middleware_1 = require("http-proxy-middleware");
|
|
10
|
+
function createServer(config) {
|
|
11
|
+
const app = (0, express_1.default)();
|
|
12
|
+
// 配置静态文件服务
|
|
13
|
+
app.use(express_1.default.static(config.dir || './'));
|
|
14
|
+
// 配置代理
|
|
15
|
+
if (config.proxy) {
|
|
16
|
+
Object.entries(config.proxy).forEach(([path, proxyConfig]) => {
|
|
17
|
+
app.use(path, (0, http_proxy_middleware_1.createProxyMiddleware)(proxyConfig));
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return app;
|
|
21
|
+
}
|
|
22
|
+
function startServer(config) {
|
|
23
|
+
const app = createServer(config);
|
|
24
|
+
const port = config.port || 8080;
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
try {
|
|
27
|
+
app.listen(port, () => {
|
|
28
|
+
console.log(`Server is running at http://localhost:${port}`);
|
|
29
|
+
resolve();
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
console.error('Failed to start server:', error);
|
|
34
|
+
reject(error);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface ProxyConfig {
|
|
2
|
+
target: string;
|
|
3
|
+
changeOrigin?: boolean;
|
|
4
|
+
pathRewrite?: Record<string, string>;
|
|
5
|
+
}
|
|
6
|
+
export interface AgentConfig {
|
|
7
|
+
port?: number;
|
|
8
|
+
dir?: string;
|
|
9
|
+
proxy?: Record<string, ProxyConfig>;
|
|
10
|
+
}
|
|
11
|
+
export interface CommandOptions {
|
|
12
|
+
config?: string;
|
|
13
|
+
c?: string;
|
|
14
|
+
cf?: string;
|
|
15
|
+
configFile?: string;
|
|
16
|
+
}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "agent-publish-server",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "基于 nodejs+express+http-proxy-middleware 的前端服务启动插件",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"bin": {
|
|
8
|
+
"agent-publish-server": "dist/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"start": "node dist/cli.js",
|
|
13
|
+
"dev": "ts-node src/cli.ts",
|
|
14
|
+
"test": "jest"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"express",
|
|
18
|
+
"proxy",
|
|
19
|
+
"server",
|
|
20
|
+
"development"
|
|
21
|
+
],
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "qghs",
|
|
24
|
+
"email": "1556448918@qq.com"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"express": "^4.18.2",
|
|
29
|
+
"http-proxy-middleware": "^2.0.6",
|
|
30
|
+
"commander": "^11.1.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/express": "^4.17.21",
|
|
34
|
+
"@types/node": "^20.10.4",
|
|
35
|
+
"typescript": "^5.3.3",
|
|
36
|
+
"ts-node": "^10.9.2",
|
|
37
|
+
"jest": "^29.7.0",
|
|
38
|
+
"@types/jest": "^29.5.11",
|
|
39
|
+
"ts-jest": "^29.1.1"
|
|
40
|
+
}
|
|
41
|
+
}
|