@fairys/mocker-cli 0.0.1
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 +36 -0
- package/bin/fairys-mocker +22 -0
- package/esm/base.d.ts +28 -0
- package/esm/base.js +80 -0
- package/esm/ci.d.ts +2 -0
- package/esm/ci.js +49 -0
- package/esm/connect.d.ts +15 -0
- package/esm/connect.js +38 -0
- package/esm/controller/base.d.ts +2 -0
- package/esm/controller/base.js +3 -0
- package/esm/controller/index.d.ts +3 -0
- package/esm/controller/index.js +3 -0
- package/esm/controller/mock.router.d.ts +21 -0
- package/esm/controller/mock.router.js +497 -0
- package/esm/controller/proxy.router.d.ts +18 -0
- package/esm/controller/proxy.router.js +503 -0
- package/esm/index.d.ts +5 -0
- package/esm/index.js +5 -0
- package/esm/main.d.ts +15 -0
- package/esm/main.js +38 -0
- package/esm/plugins/rsbuild.d.ts +2 -0
- package/esm/plugins/rsbuild.js +12 -0
- package/esm/router/base.d.ts +12 -0
- package/esm/router/base.js +23 -0
- package/esm/router/index.d.ts +3 -0
- package/esm/router/index.js +3 -0
- package/esm/router/mock.d.ts +7 -0
- package/esm/router/mock.js +32 -0
- package/esm/router/proxy.d.ts +14 -0
- package/esm/router/proxy.js +67 -0
- package/esm/utils/decorator.d.ts +18 -0
- package/esm/utils/decorator.js +55 -0
- package/esm/utils/index.d.ts +2 -0
- package/esm/utils/index.js +2 -0
- package/esm/utils/mcok.proxy.d.ts +36 -0
- package/esm/utils/mcok.proxy.js +141 -0
- package/esm/utils/utils.d.ts +20 -0
- package/esm/utils/utils.js +31 -0
- package/package.json +52 -0
- package/public/_fairys_mocker/favicon.png +0 -0
- package/public/_fairys_mocker/index.html +1 -0
- package/public/_fairys_mocker/static/css/index.2ba69ff5.css +1 -0
- package/public/_fairys_mocker/static/js/514.950758f1.js +2 -0
- package/public/_fairys_mocker/static/js/514.950758f1.js.LICENSE.txt +5 -0
- package/public/_fairys_mocker/static/js/index.f70ed1dc.js +1 -0
- package/public/_fairys_mocker/static/js/lib-react.2748fa4b.js +2 -0
- package/public/_fairys_mocker/static/js/lib-react.2748fa4b.js.LICENSE.txt +49 -0
- package/src/base.ts +125 -0
- package/src/ci.ts +61 -0
- package/src/connect.ts +62 -0
- package/src/controller/base.ts +4 -0
- package/src/controller/index.ts +3 -0
- package/src/controller/mock.router.ts +175 -0
- package/src/controller/proxy.router.ts +182 -0
- package/src/index.ts +5 -0
- package/src/main.ts +59 -0
- package/src/plugins/rsbuild.ts +14 -0
- package/src/router/base.ts +36 -0
- package/src/router/index.ts +3 -0
- package/src/router/mock.ts +38 -0
- package/src/router/proxy.ts +89 -0
- package/src/utils/decorator.ts +90 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/mcok.proxy.ts +162 -0
- package/src/utils/utils.ts +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# mocker-cli
|
|
2
|
+
|
|
3
|
+
用于界面配置生成mock数据和mock服务
|
|
4
|
+
|
|
5
|
+
[ ] 直接当 mock 服务
|
|
6
|
+
[ ] 生成 mock 数据
|
|
7
|
+
[ ] 服务 + 数据
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @fairys/mocker-cli -D # pnpm install @fairys/mocker-cli -D # yarn add @fairys/mocker-cli -D
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## 参数
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
Usage:
|
|
19
|
+
$ fairys-mocker
|
|
20
|
+
|
|
21
|
+
Options:
|
|
22
|
+
--root 设置根目录路径(默认取环境变量中的`FAIRYS_MOCKER_ROOT_DIR`)
|
|
23
|
+
--dir 设置目录名(默认取环境变量中的`FAIRYS_MOCKER_DIR`)
|
|
24
|
+
--file 设置文件名(默认取环境变量中的`FAIRYS_MOCKER_FILE`)
|
|
25
|
+
--file2 设置文件名2(默认取环境变量中的`FAIRYS_MOCKER_PROXY_FILE`)
|
|
26
|
+
--static 设置静态文件目录
|
|
27
|
+
--static-prefix 设置静态文件路径前缀
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
**参数说明**
|
|
31
|
+
|
|
32
|
+
`FAIRYS_MOCKER_ROOT_DIR`或`root`:未设置时,取当前执行命令目录
|
|
33
|
+
`FAIRYS_MOCKER_DIR`或者`dir`:未设置时,取当前执行命令目录的`mock`文件夹
|
|
34
|
+
`FAIRYS_MOCKER_FILE`或者`file`:未设置时,取当前执行命令目录的`mock`文件夹下的`index.mock.ts`文件
|
|
35
|
+
`FAIRYS_MOCKER_PROXY_FILE`或者`file2`:未设置时,取当前执行命令目录的`mock`文件夹下的`proxy.mock.ts`文件
|
|
36
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import nodeModule from 'node:module';
|
|
3
|
+
|
|
4
|
+
// enable on-disk code caching of all modules loaded by Node.js
|
|
5
|
+
// requires Nodejs >= 22.8.0
|
|
6
|
+
const { enableCompileCache } = nodeModule;
|
|
7
|
+
if (enableCompileCache) {
|
|
8
|
+
try {
|
|
9
|
+
enableCompileCache();
|
|
10
|
+
} catch {
|
|
11
|
+
// ignore errors
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
async function main() {
|
|
15
|
+
const { runExpressCli } = await import('../esm/ci.js');
|
|
16
|
+
try {
|
|
17
|
+
runExpressCli();
|
|
18
|
+
} catch (err) {
|
|
19
|
+
console.error(err);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
main();
|
package/esm/base.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import connect from 'connect';
|
|
3
|
+
import { ClassStruct } from "./utils/decorator.js";
|
|
4
|
+
import http from "node:http";
|
|
5
|
+
export declare class FairysMockerBase {
|
|
6
|
+
/**服务*/
|
|
7
|
+
server: http.Server | undefined;
|
|
8
|
+
/**主路由*/
|
|
9
|
+
router: express.Router | undefined;
|
|
10
|
+
/**内置路由*/
|
|
11
|
+
fairysMockerRouter: express.Router | undefined;
|
|
12
|
+
/**子应用*/
|
|
13
|
+
app: express.Express | undefined;
|
|
14
|
+
/**主应用*/
|
|
15
|
+
mainApp: express.Express | connect.Server | undefined;
|
|
16
|
+
/**类*/
|
|
17
|
+
controller: ClassStruct[];
|
|
18
|
+
/**静态文件服务列表*/
|
|
19
|
+
staticServerList: string[];
|
|
20
|
+
/**静态文件服务*/
|
|
21
|
+
staticServer: (dir: string, prefix?: string, isRegister?: boolean) => void;
|
|
22
|
+
/**初始化 app 服务*/
|
|
23
|
+
initApp: (app: express.Express | connect.Server, cb?: () => void) => express.Express | connect.Server;
|
|
24
|
+
/**启动日志打印服务器地址*/
|
|
25
|
+
logServer: (port: number) => Promise<void>;
|
|
26
|
+
close: () => void;
|
|
27
|
+
}
|
|
28
|
+
export declare const fairysMockerBase: FairysMockerBase;
|
package/esm/base.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import node_fs from "node:fs";
|
|
3
|
+
import cors from "cors";
|
|
4
|
+
import node_path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { MockRouterController } from "./controller/mock.router.js";
|
|
7
|
+
import { registerRoutes } from "./utils/decorator.js";
|
|
8
|
+
import { ProxyRouterController } from "./controller/proxy.router.js";
|
|
9
|
+
import chalk from "chalk";
|
|
10
|
+
const base_filename = fileURLToPath(import.meta.url);
|
|
11
|
+
const base_dirname = node_path.dirname(base_filename);
|
|
12
|
+
class FairysMockerBase {
|
|
13
|
+
server = void 0;
|
|
14
|
+
router = void 0;
|
|
15
|
+
fairysMockerRouter = void 0;
|
|
16
|
+
app = void 0;
|
|
17
|
+
mainApp = void 0;
|
|
18
|
+
controller = [
|
|
19
|
+
MockRouterController,
|
|
20
|
+
ProxyRouterController
|
|
21
|
+
];
|
|
22
|
+
staticServerList = [];
|
|
23
|
+
staticServer = (dir, prefix = '/', isRegister = true)=>{
|
|
24
|
+
if (!this.app) return;
|
|
25
|
+
if (node_fs.existsSync(dir)) {
|
|
26
|
+
const _prefix = /^\/$/.test(prefix) ? prefix : `/${prefix}`;
|
|
27
|
+
this.app.use(_prefix, express["static"](dir));
|
|
28
|
+
if (isRegister) this.staticServerList.push(_prefix);
|
|
29
|
+
} else console.log(chalk.red(`${dir} 目录不存在`));
|
|
30
|
+
};
|
|
31
|
+
initApp = (app, cb)=>{
|
|
32
|
+
if (this.app) {
|
|
33
|
+
console.log('挂载 主应用 服务');
|
|
34
|
+
this.mainApp = app;
|
|
35
|
+
this.mainApp.use(this.app);
|
|
36
|
+
} else {
|
|
37
|
+
console.log('');
|
|
38
|
+
console.log(chalk.hex('#54FF9F')('===fairys-mocker================='));
|
|
39
|
+
console.log('');
|
|
40
|
+
this.mainApp = app;
|
|
41
|
+
this.app = express();
|
|
42
|
+
this.mainApp.use(this.app);
|
|
43
|
+
this.app.use(express.json());
|
|
44
|
+
this.app.use(cors());
|
|
45
|
+
this.router = express.Router();
|
|
46
|
+
this.fairysMockerRouter = express.Router();
|
|
47
|
+
this.router.use(this.fairysMockerRouter);
|
|
48
|
+
const staticDir = node_path.join(base_dirname, '../public/_fairys_mocker');
|
|
49
|
+
this.staticServer(staticDir, '/_fairys_mocker', false);
|
|
50
|
+
for(let index = 0; index < this.controller.length; index++){
|
|
51
|
+
const Controller = this.controller[index];
|
|
52
|
+
const newController = new Controller();
|
|
53
|
+
registerRoutes(newController);
|
|
54
|
+
const start = newController?.start;
|
|
55
|
+
if ('function' == typeof start) start();
|
|
56
|
+
}
|
|
57
|
+
this.app.use('/', this.router);
|
|
58
|
+
cb?.();
|
|
59
|
+
console.log('');
|
|
60
|
+
}
|
|
61
|
+
return app;
|
|
62
|
+
};
|
|
63
|
+
logServer = async (port)=>{
|
|
64
|
+
if (!this.app) return void console.log(chalk.red('请先初始化 app'));
|
|
65
|
+
console.log("");
|
|
66
|
+
console.log(chalk.green(`\tfairys-mocker API地址: http://localhost:${port}`));
|
|
67
|
+
console.log(chalk.green(`\tfairys-mocker UI地址: http://localhost:${port}/_fairys_mocker`));
|
|
68
|
+
console.log("");
|
|
69
|
+
for(let index = 0; index < this.staticServerList.length; index++){
|
|
70
|
+
const element = this.staticServerList[index];
|
|
71
|
+
console.log(chalk.green(`\tfairys-mocker 静态服务 ${chalk.yellow(element)} 地址: http://localhost:${port}${element}`));
|
|
72
|
+
}
|
|
73
|
+
console.log("");
|
|
74
|
+
};
|
|
75
|
+
close = ()=>{
|
|
76
|
+
if (this.server) this.server.close();
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const fairysMockerBase = new FairysMockerBase();
|
|
80
|
+
export { FairysMockerBase, fairysMockerBase };
|
package/esm/ci.d.ts
ADDED
package/esm/ci.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import yargs_parser from "yargs-parser";
|
|
2
|
+
import { fairysMockerExpress } from "./main.js";
|
|
3
|
+
import { fairysMockerConnect } from "./connect.js";
|
|
4
|
+
import { utils } from "./utils/utils.js";
|
|
5
|
+
function help() {
|
|
6
|
+
console.log('\n Usage: \x1b[34;1fairys-mocker\x1b[0m [--help|h|--root|--dir|--file|--file2|--static|--static-prefix]');
|
|
7
|
+
console.log('\n Displays help information.');
|
|
8
|
+
console.log('\n Options:\n');
|
|
9
|
+
console.log(' --root ', '设置根目录路径(默认取环境变量中的`FAIRYS_MOCKER_ROOT_DIR`).');
|
|
10
|
+
console.log(' --dir ', '设置目录名(默认取环境变量中的`FAIRYS_MOCKER_DIR`).');
|
|
11
|
+
console.log(' --file ', '设置文件名(默认取环境变量中的`FAIRYS_MOCKER_FILE`).');
|
|
12
|
+
console.log(' --file2 ', '设置文件名2(默认取环境变量中的`FAIRYS_MOCKER_PROXY_FILE`).');
|
|
13
|
+
console.log(' --static ', '设置静态文件服务目录.');
|
|
14
|
+
console.log(' --static-prefix ', '设置静态文件服务目录访问前缀.');
|
|
15
|
+
console.log('\n Example:\n');
|
|
16
|
+
console.log(' $ \x1b[35mfairys-mocker\x1b[0m --dir mock2');
|
|
17
|
+
console.log(' $ \x1b[35mfairys-mocker\x1b[0m --file index.mock');
|
|
18
|
+
}
|
|
19
|
+
const argv = yargs_parser(process.argv.slice(2), {
|
|
20
|
+
string: [
|
|
21
|
+
"root",
|
|
22
|
+
'dir',
|
|
23
|
+
'file',
|
|
24
|
+
'file2',
|
|
25
|
+
'static',
|
|
26
|
+
'static-prefix'
|
|
27
|
+
]
|
|
28
|
+
});
|
|
29
|
+
const runExpressCli = ()=>{
|
|
30
|
+
if (argv.help | argv.h) return void help();
|
|
31
|
+
utils.setRootDir(argv.root);
|
|
32
|
+
utils.setDir(argv.dir);
|
|
33
|
+
utils.setFile(argv.file);
|
|
34
|
+
utils.setProxyFile(argv.file2);
|
|
35
|
+
fairysMockerExpress.start(()=>{
|
|
36
|
+
if (argv.static) fairysMockerExpress.staticServer(argv.static, argv['static-prefix']);
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
const runConnectCli = ()=>{
|
|
40
|
+
if (argv.help) return void help();
|
|
41
|
+
utils.setRootDir(argv.root);
|
|
42
|
+
utils.setDir(argv.dir);
|
|
43
|
+
utils.setFile(argv.file);
|
|
44
|
+
utils.setProxyFile(argv.file2);
|
|
45
|
+
fairysMockerConnect.start(()=>{
|
|
46
|
+
if (argv.static) fairysMockerConnect.staticServer(argv.static, argv['static-prefix']);
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
export { runConnectCli, runExpressCli };
|
package/esm/connect.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import connect from 'connect';
|
|
2
|
+
export declare class FairysMockerConnect {
|
|
3
|
+
base: import("./base.js").FairysMockerBase;
|
|
4
|
+
/**创建 app*/
|
|
5
|
+
createApp: (cb?: () => void) => import("express").Express | connect.Server;
|
|
6
|
+
/**检测端口,返回实际端口*/
|
|
7
|
+
detectPort: (port?: number) => Promise<unknown>;
|
|
8
|
+
/**启动服务器*/
|
|
9
|
+
listen: (port?: number, callback?: (error?: Error) => void) => Promise<import("node:http").Server<typeof import("node:http").IncomingMessage, typeof import("node:http").ServerResponse> | undefined>;
|
|
10
|
+
staticServer: (dir: string, prefix?: string, isRegister?: boolean) => void;
|
|
11
|
+
/**启动 app*/
|
|
12
|
+
start: (cb?: () => void) => void;
|
|
13
|
+
close: () => void;
|
|
14
|
+
}
|
|
15
|
+
export declare const fairysMockerConnect: FairysMockerConnect;
|
package/esm/connect.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import connect from "connect";
|
|
2
|
+
import chalk from "chalk";
|
|
3
|
+
import { fairysMockerBase } from "./base.js";
|
|
4
|
+
import { detect } from "detect-port";
|
|
5
|
+
class FairysMockerConnect {
|
|
6
|
+
base = fairysMockerBase;
|
|
7
|
+
createApp = (cb)=>fairysMockerBase.initApp(connect(), cb);
|
|
8
|
+
detectPort = (port)=>{
|
|
9
|
+
const PORT = port || process.env.PORT || 6901;
|
|
10
|
+
return new Promise((resolve, reject)=>{
|
|
11
|
+
detect(PORT).then((realPort)=>{
|
|
12
|
+
resolve(realPort);
|
|
13
|
+
}).catch((err)=>{
|
|
14
|
+
reject(err);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
listen = async (port, callback)=>{
|
|
19
|
+
if (!fairysMockerBase.mainApp) return void console.log(chalk.red('请先初始化 app'));
|
|
20
|
+
const app = fairysMockerBase.mainApp;
|
|
21
|
+
const realPort = await this.detectPort(port);
|
|
22
|
+
fairysMockerBase.server = app.listen(realPort, (...rest)=>{
|
|
23
|
+
fairysMockerBase.logServer(realPort);
|
|
24
|
+
callback?.(...rest);
|
|
25
|
+
});
|
|
26
|
+
return fairysMockerBase.server;
|
|
27
|
+
};
|
|
28
|
+
staticServer = fairysMockerBase.staticServer;
|
|
29
|
+
start = (cb)=>{
|
|
30
|
+
this.createApp(cb);
|
|
31
|
+
this.listen();
|
|
32
|
+
};
|
|
33
|
+
close = ()=>{
|
|
34
|
+
if (fairysMockerBase.server) fairysMockerBase.server.close();
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
const fairysMockerConnect = new FairysMockerConnect();
|
|
38
|
+
export { FairysMockerConnect, fairysMockerConnect };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { MockRouter } from "../router/mock.js";
|
|
3
|
+
import { BaseController } from "./base.js";
|
|
4
|
+
/**路由*/
|
|
5
|
+
export declare class MockRouterController extends BaseController {
|
|
6
|
+
/**mock 路由器实例*/
|
|
7
|
+
router: MockRouter | null;
|
|
8
|
+
constructor();
|
|
9
|
+
/**保存 Mock 配置*/
|
|
10
|
+
post_mock(req: express.Request, res: express.Response): void;
|
|
11
|
+
/**读取 Mock 配置*/
|
|
12
|
+
get_mock(req: express.Request, res: express.Response): void;
|
|
13
|
+
/**检查 Mock 配置服务是否启用*/
|
|
14
|
+
get_mock_enabled(req: express.Request, res: express.Response): void;
|
|
15
|
+
/**启动 Mock 配置服务*/
|
|
16
|
+
get_mock_start(req: express.Request, res: express.Response): void;
|
|
17
|
+
/**销毁 Mock 配置服务*/
|
|
18
|
+
get_mock_destroy(req: express.Request, res: express.Response): void;
|
|
19
|
+
/**启动 Mock 配置服务*/
|
|
20
|
+
start: (rootDir?: string, dir?: string, fileName?: string) => void;
|
|
21
|
+
}
|