@fairys/mocker-cli 0.0.1 → 0.0.2
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 +127 -2
- package/bin/fairys-mocker +2 -2
- package/esm/base.js +2 -1
- package/esm/ci.d.ts +4 -2
- package/esm/ci.js +18 -14
- package/esm/utils/mcok.proxy.js +10 -6
- package/esm/utils/utils.d.ts +8 -0
- package/esm/utils/utils.js +8 -0
- package/package.json +2 -2
- package/src/base.ts +5 -2
- package/src/ci.ts +19 -21
- package/src/utils/mcok.proxy.ts +12 -9
- package/src/utils/utils.ts +19 -0
package/README.md
CHANGED
|
@@ -31,6 +31,131 @@ Options:
|
|
|
31
31
|
|
|
32
32
|
`FAIRYS_MOCKER_ROOT_DIR`或`root`:未设置时,取当前执行命令目录
|
|
33
33
|
`FAIRYS_MOCKER_DIR`或者`dir`:未设置时,取当前执行命令目录的`mock`文件夹
|
|
34
|
-
`FAIRYS_MOCKER_FILE`或者`file`:未设置时,取当前执行命令目录的`mock`文件夹下的`index.mock.
|
|
35
|
-
`FAIRYS_MOCKER_PROXY_FILE`或者`file2`:未设置时,取当前执行命令目录的`mock`文件夹下的`proxy.
|
|
34
|
+
`FAIRYS_MOCKER_FILE`或者`file`:未设置时,取当前执行命令目录的`mock`文件夹下的`index.mock.cache.json`文件
|
|
35
|
+
`FAIRYS_MOCKER_PROXY_FILE`或者`file2`:未设置时,取当前执行命令目录的`mock`文件夹下的`proxy.cache.json`文件
|
|
36
|
+
|
|
37
|
+
在使用的过程中会生成4个文件
|
|
38
|
+
|
|
39
|
+
1. `index.mock.cache.json`: 生成mock数据的原始配置(页面渲染取值)
|
|
40
|
+
|
|
41
|
+
```json
|
|
42
|
+
{
|
|
43
|
+
"mockList": [
|
|
44
|
+
{
|
|
45
|
+
"url": "/api/test",
|
|
46
|
+
"method": "POST",
|
|
47
|
+
"status": "200",
|
|
48
|
+
"delay": 0,
|
|
49
|
+
"body": {
|
|
50
|
+
"code": 200,
|
|
51
|
+
"data": {
|
|
52
|
+
"id": "@id",
|
|
53
|
+
"name": "@name",
|
|
54
|
+
"email": "@email"
|
|
55
|
+
},
|
|
56
|
+
"message": "success"
|
|
57
|
+
},
|
|
58
|
+
"listCount": 20
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
"rootDir": "...",
|
|
62
|
+
"dir": "mock",
|
|
63
|
+
"fileName": "index.mock",
|
|
64
|
+
"cache": "index.mock.cache.json"
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
2. `index.mock.ts`: 生成的mock数据
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
// Mock 配置文件
|
|
72
|
+
export interface MockerItem {
|
|
73
|
+
/**该接口允许的 请求方法,默认同时支持 GET 和 POST*/
|
|
74
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
|
|
75
|
+
/**状态码*/
|
|
76
|
+
status: string;
|
|
77
|
+
//配置响应延迟时间, 如果传入的是一个数组,则代表延迟时间的范围
|
|
78
|
+
delay: number | [number, number];
|
|
79
|
+
/**响应体(可以自定义返回格式)*/
|
|
80
|
+
body: any;
|
|
81
|
+
/**接口地址*/
|
|
82
|
+
url: string;
|
|
83
|
+
/**列表数据条数(仅 list 格式有效)*/
|
|
84
|
+
listCount?: number;
|
|
85
|
+
}
|
|
86
|
+
/**mock配置 列表*/
|
|
87
|
+
export type DefineMockList = MockerItem[];
|
|
88
|
+
export const mockList: DefineMockList = [
|
|
89
|
+
{
|
|
90
|
+
"url": "/api/test",
|
|
91
|
+
"method": "POST",
|
|
92
|
+
"status": "200",
|
|
93
|
+
"delay": 0,
|
|
94
|
+
"body": {
|
|
95
|
+
"code": 200,
|
|
96
|
+
"message": "success",
|
|
97
|
+
"data": {
|
|
98
|
+
"id": "450000200805311843",
|
|
99
|
+
"name": "Sarah Lee",
|
|
100
|
+
"email": "u.azfjyqt@uscf.pe"
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
"listCount": 20
|
|
104
|
+
},
|
|
105
|
+
];
|
|
106
|
+
export default mockList;
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
3. `proxy.cache.json`: 生成代理数据原始配置(页面渲染取值)
|
|
111
|
+
|
|
112
|
+
```json
|
|
113
|
+
{
|
|
114
|
+
"proxyList": [
|
|
115
|
+
{
|
|
116
|
+
"path": "^/docks",
|
|
117
|
+
"target": "http://localhost:9009"
|
|
118
|
+
}
|
|
119
|
+
],
|
|
120
|
+
"rootDir": "...",
|
|
121
|
+
"dir": "mock",
|
|
122
|
+
"fileName": "proxy",
|
|
123
|
+
"cache": "proxy.cache.json"
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
4. `proxy.ts`: 生成的代理服务数据
|
|
128
|
+
|
|
129
|
+
```tsx
|
|
130
|
+
// 代理配置文件
|
|
131
|
+
/**
|
|
132
|
+
* 代理配置参数
|
|
133
|
+
*/
|
|
134
|
+
export type ProxyItem = Record<string,{
|
|
135
|
+
/**转发地址*/
|
|
136
|
+
target: string,
|
|
137
|
+
/**路径重写*/
|
|
138
|
+
pathRewrite?: Record<string, string>,
|
|
139
|
+
/**是否开启ws*/
|
|
140
|
+
ws?: boolean
|
|
141
|
+
}>
|
|
142
|
+
|
|
143
|
+
export const proxyConfig: ProxyItem = {
|
|
144
|
+
"^/tsx": {
|
|
145
|
+
"target": "http://localhost:9009"
|
|
146
|
+
},
|
|
147
|
+
"^/wsdse": {
|
|
148
|
+
"target": "http://localhost:8982",
|
|
149
|
+
"pathRewrite": {
|
|
150
|
+
"^/wsdse": ""
|
|
151
|
+
},
|
|
152
|
+
"ws": true
|
|
153
|
+
},
|
|
154
|
+
"^/docks": {
|
|
155
|
+
"target": "http://localhost:9009"
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
export default proxyConfig;
|
|
159
|
+
|
|
160
|
+
```
|
|
36
161
|
|
package/bin/fairys-mocker
CHANGED
|
@@ -12,9 +12,9 @@ if (enableCompileCache) {
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
async function main() {
|
|
15
|
-
const {
|
|
15
|
+
const { runCli } = await import('../esm/ci.js');
|
|
16
16
|
try {
|
|
17
|
-
|
|
17
|
+
runCli();
|
|
18
18
|
} catch (err) {
|
|
19
19
|
console.error(err);
|
|
20
20
|
}
|
package/esm/base.js
CHANGED
|
@@ -23,7 +23,8 @@ class FairysMockerBase {
|
|
|
23
23
|
staticServer = (dir, prefix = '/', isRegister = true)=>{
|
|
24
24
|
if (!this.app) return;
|
|
25
25
|
if (node_fs.existsSync(dir)) {
|
|
26
|
-
|
|
26
|
+
let _prefix = prefix;
|
|
27
|
+
if (!/^\//.test(prefix)) _prefix = "/" + prefix;
|
|
27
28
|
this.app.use(_prefix, express["static"](dir));
|
|
28
29
|
if (isRegister) this.staticServerList.push(_prefix);
|
|
29
30
|
} else console.log(chalk.red(`${dir} 目录不存在`));
|
package/esm/ci.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { FairysMockerExpress } from "./main.js";
|
|
2
|
+
import { FairysMockerConnect } from "./connect.js";
|
|
3
|
+
export declare const CI: (app: FairysMockerExpress | FairysMockerConnect) => void;
|
|
4
|
+
export declare const runCli: () => void;
|
package/esm/ci.js
CHANGED
|
@@ -3,7 +3,7 @@ import { fairysMockerExpress } from "./main.js";
|
|
|
3
3
|
import { fairysMockerConnect } from "./connect.js";
|
|
4
4
|
import { utils } from "./utils/utils.js";
|
|
5
5
|
function help() {
|
|
6
|
-
console.log('\n Usage: \x1b[34;1fairys-mocker\x1b[0m [--help|h|--root|--dir|--file|--file2|--static|--static-prefix]');
|
|
6
|
+
console.log('\n Usage: \x1b[34;1fairys-mocker\x1b[0m [--help|h|--root|--dir|--file|--file2|--static|--static-prefix|--is-mock-file|--is-proxy-file]');
|
|
7
7
|
console.log('\n Displays help information.');
|
|
8
8
|
console.log('\n Options:\n');
|
|
9
9
|
console.log(' --root ', '设置根目录路径(默认取环境变量中的`FAIRYS_MOCKER_ROOT_DIR`).');
|
|
@@ -12,6 +12,9 @@ function help() {
|
|
|
12
12
|
console.log(' --file2 ', '设置文件名2(默认取环境变量中的`FAIRYS_MOCKER_PROXY_FILE`).');
|
|
13
13
|
console.log(' --static ', '设置静态文件服务目录.');
|
|
14
14
|
console.log(' --static-prefix ', '设置静态文件服务目录访问前缀.');
|
|
15
|
+
console.log(' --is-mock-file ', '是否生成mock数据文件,默认生成.');
|
|
16
|
+
console.log(' --is-proxy-file ', '是否生成proxy数据文件,默认生成.');
|
|
17
|
+
console.log(' --is-connect ', '是否是connect服务.默认express.');
|
|
15
18
|
console.log('\n Example:\n');
|
|
16
19
|
console.log(' $ \x1b[35mfairys-mocker\x1b[0m --dir mock2');
|
|
17
20
|
console.log(' $ \x1b[35mfairys-mocker\x1b[0m --file index.mock');
|
|
@@ -24,26 +27,27 @@ const argv = yargs_parser(process.argv.slice(2), {
|
|
|
24
27
|
'file2',
|
|
25
28
|
'static',
|
|
26
29
|
'static-prefix'
|
|
30
|
+
],
|
|
31
|
+
boolean: [
|
|
32
|
+
'is-mock-file',
|
|
33
|
+
"is-proxy-file",
|
|
34
|
+
'is-connect'
|
|
27
35
|
]
|
|
28
36
|
});
|
|
29
|
-
const
|
|
37
|
+
const CI = (app)=>{
|
|
30
38
|
if (argv.help | argv.h) return void help();
|
|
31
39
|
utils.setRootDir(argv.root);
|
|
32
40
|
utils.setDir(argv.dir);
|
|
33
41
|
utils.setFile(argv.file);
|
|
34
42
|
utils.setProxyFile(argv.file2);
|
|
35
|
-
|
|
36
|
-
|
|
43
|
+
utils.setIsCreateMockDataFile(argv['is-mock-file']);
|
|
44
|
+
utils.setIsCreateProxyDataFile(argv['is-proxy-file']);
|
|
45
|
+
app.start(()=>{
|
|
46
|
+
if (!argv.static) return;
|
|
47
|
+
app.staticServer(argv.static, argv['static-prefix']);
|
|
37
48
|
});
|
|
38
49
|
};
|
|
39
|
-
const
|
|
40
|
-
|
|
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
|
-
});
|
|
50
|
+
const runCli = ()=>{
|
|
51
|
+
argv['is-connect'] ? CI(fairysMockerConnect) : CI(fairysMockerExpress);
|
|
48
52
|
};
|
|
49
|
-
export {
|
|
53
|
+
export { CI, runCli };
|
package/esm/utils/mcok.proxy.js
CHANGED
|
@@ -30,8 +30,9 @@ const createProxyFile = (proxyList, rootDir, dir, fileName)=>{
|
|
|
30
30
|
recursive: true
|
|
31
31
|
});
|
|
32
32
|
const proxyConfig = createProxyData(proxyList);
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
if (utils.isCreateProxyDataFile) {
|
|
34
|
+
const proxyFilePath = node_path.join(proxyDir, `${_fileName}.ts`);
|
|
35
|
+
const proxyFileContent = `// 代理配置文件
|
|
35
36
|
// 自动生成于 ${new Date().toISOString()}
|
|
36
37
|
|
|
37
38
|
/**
|
|
@@ -49,7 +50,8 @@ export type ProxyItem = Record<string,{
|
|
|
49
50
|
export const proxyConfig: ProxyItem = ${JSON.stringify(proxyConfig, null, 2)};
|
|
50
51
|
export default proxyConfig;
|
|
51
52
|
`;
|
|
52
|
-
|
|
53
|
+
node_fs.writeFileSync(proxyFilePath, proxyFileContent);
|
|
54
|
+
}
|
|
53
55
|
const cacheFilePath = node_path.join(proxyDir, _fileName + '.cache.json');
|
|
54
56
|
const cacheFileContent = JSON.stringify({
|
|
55
57
|
proxyList,
|
|
@@ -95,8 +97,9 @@ const createMockFile = (mockList, rootDir, dir, fileName)=>{
|
|
|
95
97
|
recursive: true
|
|
96
98
|
});
|
|
97
99
|
const mockConfig = createMockData(mockList);
|
|
98
|
-
|
|
99
|
-
|
|
100
|
+
if (utils.isCreateMockDataFile) {
|
|
101
|
+
const mockFilePath = node_path.join(mockDir, `${_fileName}.ts`);
|
|
102
|
+
const mockFileContent = `// Mock 配置文件
|
|
100
103
|
// 自动生成于 ${new Date().toISOString()}
|
|
101
104
|
|
|
102
105
|
export interface MockerItem {
|
|
@@ -120,7 +123,8 @@ export type DefineMockList = MockerItem[];
|
|
|
120
123
|
export const mockList: DefineMockList = ${JSON.stringify(mockConfig, null, 2)};
|
|
121
124
|
export default mockList;
|
|
122
125
|
`;
|
|
123
|
-
|
|
126
|
+
node_fs.writeFileSync(mockFilePath, mockFileContent);
|
|
127
|
+
}
|
|
124
128
|
const cacheFilePath = node_path.join(mockDir, _fileName + '.cache.json');
|
|
125
129
|
const cacheFileContent = JSON.stringify({
|
|
126
130
|
mockList,
|
package/esm/utils/utils.d.ts
CHANGED
|
@@ -7,6 +7,10 @@ declare class Utils {
|
|
|
7
7
|
file: string;
|
|
8
8
|
/**代理文件名*/
|
|
9
9
|
proxyFile: string;
|
|
10
|
+
/**是否生成mock数据文件*/
|
|
11
|
+
isCreateMockDataFile: boolean;
|
|
12
|
+
/**是否生成proxy数据文件*/
|
|
13
|
+
isCreateProxyDataFile: boolean;
|
|
10
14
|
/**设置根目录*/
|
|
11
15
|
setRootDir: (value?: string) => void;
|
|
12
16
|
/**设置目录名*/
|
|
@@ -15,6 +19,10 @@ declare class Utils {
|
|
|
15
19
|
setFile: (value?: string) => void;
|
|
16
20
|
/**设置代理文件名*/
|
|
17
21
|
setProxyFile: (value?: string) => void;
|
|
22
|
+
/**设置 是否生成mock数据文件*/
|
|
23
|
+
setIsCreateMockDataFile: (fig?: boolean) => void;
|
|
24
|
+
/**设置 是否生成proxy数据文件*/
|
|
25
|
+
setIsCreateProxyDataFile: (fig?: boolean) => void;
|
|
18
26
|
}
|
|
19
27
|
export declare const utils: Utils;
|
|
20
28
|
export {};
|
package/esm/utils/utils.js
CHANGED
|
@@ -6,6 +6,8 @@ class Utils {
|
|
|
6
6
|
dir = 'mock';
|
|
7
7
|
file = 'index.mock';
|
|
8
8
|
proxyFile = 'proxy';
|
|
9
|
+
isCreateMockDataFile = true;
|
|
10
|
+
isCreateProxyDataFile = true;
|
|
9
11
|
setRootDir = (value)=>{
|
|
10
12
|
if (value && node_fs.existsSync(value)) this.rootDir = value;
|
|
11
13
|
else {
|
|
@@ -26,6 +28,12 @@ class Utils {
|
|
|
26
28
|
setProxyFile = (value)=>{
|
|
27
29
|
this.proxyFile = value || process.env.FAIRYS_MOCKER_PROXY_FILE || 'proxy';
|
|
28
30
|
};
|
|
31
|
+
setIsCreateMockDataFile = (fig)=>{
|
|
32
|
+
if ("boolean" == typeof fig) this.isCreateMockDataFile = fig;
|
|
33
|
+
};
|
|
34
|
+
setIsCreateProxyDataFile = (fig)=>{
|
|
35
|
+
if ("boolean" == typeof fig) this.isCreateProxyDataFile = fig;
|
|
36
|
+
};
|
|
29
37
|
}
|
|
30
38
|
const utils = new Utils();
|
|
31
39
|
export { utils };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fairys/mocker-cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Fairys Mocker CLI for mock data generation",
|
|
5
5
|
"author": "SunLxy <1011771396@qq.com>",
|
|
6
6
|
"homepage": "https://github.com/autumn-fairy-tales/fairys-mocker",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"fairys-mocker": "bin/fairys-mocker"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@fairys/create-mock-data": "^0.0.
|
|
29
|
+
"@fairys/create-mock-data": "^0.0.2",
|
|
30
30
|
"body-parser": "2.2.2",
|
|
31
31
|
"chalk": "^5.6.2",
|
|
32
32
|
"connect": "^3.7.0",
|
package/src/base.ts
CHANGED
|
@@ -38,9 +38,12 @@ export class FairysMockerBase {
|
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
40
40
|
if (fs.existsSync(dir)) {
|
|
41
|
-
|
|
41
|
+
let _prefix = prefix;
|
|
42
|
+
if (!/^\//.test(prefix)) {
|
|
43
|
+
_prefix = "/" + prefix
|
|
44
|
+
}
|
|
42
45
|
this.app.use(_prefix, express.static(dir));
|
|
43
|
-
// console.log(chalk.green(`静态文件服务:${dir}`))
|
|
46
|
+
// console.log(chalk.green(`静态文件服务:${_prefix}\t${dir}`))
|
|
44
47
|
if (isRegister) {
|
|
45
48
|
this.staticServerList.push(_prefix)
|
|
46
49
|
}
|
package/src/ci.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import parser, { Arguments } from 'yargs-parser'
|
|
2
|
-
import { fairysMockerExpress } from "./main.js"
|
|
3
|
-
import { fairysMockerConnect } from "./connect.js"
|
|
2
|
+
import { fairysMockerExpress, FairysMockerExpress } from "./main.js"
|
|
3
|
+
import { fairysMockerConnect, FairysMockerConnect } from "./connect.js"
|
|
4
4
|
import { utils } from "./utils/utils.js"
|
|
5
5
|
|
|
6
6
|
function help() {
|
|
7
|
-
console.log('\n Usage: \x1b[34;1fairys-mocker\x1b[0m [--help|h|--root|--dir|--file|--file2|--static|--static-prefix]');
|
|
7
|
+
console.log('\n Usage: \x1b[34;1fairys-mocker\x1b[0m [--help|h|--root|--dir|--file|--file2|--static|--static-prefix|--is-mock-file|--is-proxy-file]');
|
|
8
8
|
console.log('\n Displays help information.');
|
|
9
9
|
console.log('\n Options:\n');
|
|
10
10
|
console.log(' --root ', '设置根目录路径(默认取环境变量中的`FAIRYS_MOCKER_ROOT_DIR`).');
|
|
@@ -13,6 +13,9 @@ function help() {
|
|
|
13
13
|
console.log(' --file2 ', '设置文件名2(默认取环境变量中的`FAIRYS_MOCKER_PROXY_FILE`).');
|
|
14
14
|
console.log(' --static ', '设置静态文件服务目录.');
|
|
15
15
|
console.log(' --static-prefix ', '设置静态文件服务目录访问前缀.');
|
|
16
|
+
console.log(' --is-mock-file ', '是否生成mock数据文件,默认生成.');
|
|
17
|
+
console.log(' --is-proxy-file ', '是否生成proxy数据文件,默认生成.');
|
|
18
|
+
console.log(' --is-connect ', '是否是connect服务.默认express.');
|
|
16
19
|
|
|
17
20
|
console.log('\n Example:\n');
|
|
18
21
|
console.log(' $ \x1b[35mfairys-mocker\x1b[0m --dir mock2');
|
|
@@ -21,9 +24,10 @@ function help() {
|
|
|
21
24
|
|
|
22
25
|
const argv: Partial<Arguments> = parser(process.argv.slice(2), {
|
|
23
26
|
string: ["root", 'dir', 'file', 'file2', 'static', 'static-prefix'],
|
|
27
|
+
boolean: ['is-mock-file', "is-proxy-file", 'is-connect']
|
|
24
28
|
})
|
|
25
29
|
|
|
26
|
-
export const
|
|
30
|
+
export const CI = (app: FairysMockerExpress | FairysMockerConnect) => {
|
|
27
31
|
if (argv.help | argv.h) {
|
|
28
32
|
help();
|
|
29
33
|
return;
|
|
@@ -32,30 +36,24 @@ export const runExpressCli = () => {
|
|
|
32
36
|
utils.setDir(argv.dir);
|
|
33
37
|
utils.setFile(argv.file);
|
|
34
38
|
utils.setProxyFile(argv.file2);
|
|
39
|
+
utils.setIsCreateMockDataFile(argv['is-mock-file'])
|
|
40
|
+
utils.setIsCreateProxyDataFile(argv['is-proxy-file'])
|
|
35
41
|
// 1. 直接当 mock 服务,
|
|
36
42
|
// 2. 生成 mock 数据
|
|
37
43
|
// 3. mock 服务 + 数据
|
|
38
|
-
|
|
39
|
-
if (argv.static) {
|
|
40
|
-
|
|
44
|
+
app.start(() => {
|
|
45
|
+
if (!argv.static) {
|
|
46
|
+
return;
|
|
41
47
|
}
|
|
48
|
+
app.staticServer(argv.static, argv['static-prefix']);
|
|
42
49
|
});
|
|
43
50
|
}
|
|
44
51
|
}
|
|
45
52
|
|
|
46
|
-
export const
|
|
47
|
-
if (argv
|
|
48
|
-
|
|
49
|
-
return;
|
|
53
|
+
export const runCli = () => {
|
|
54
|
+
if (argv['is-connect']) {
|
|
55
|
+
CI(fairysMockerConnect)
|
|
50
56
|
} else {
|
|
51
|
-
|
|
52
|
-
utils.setDir(argv.dir);
|
|
53
|
-
utils.setFile(argv.file);
|
|
54
|
-
utils.setProxyFile(argv.file2);
|
|
55
|
-
fairysMockerConnect.start(() => {
|
|
56
|
-
if (argv.static) {
|
|
57
|
-
fairysMockerConnect.staticServer(argv.static, argv['static-prefix']);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
57
|
+
CI(fairysMockerExpress)
|
|
60
58
|
}
|
|
61
|
-
}
|
|
59
|
+
}
|
package/src/utils/mcok.proxy.ts
CHANGED
|
@@ -38,9 +38,9 @@ export const createProxyFile = (proxyList: ProxyList, rootDir?: string, dir?: st
|
|
|
38
38
|
fs.mkdirSync(proxyDir, { recursive: true });
|
|
39
39
|
}
|
|
40
40
|
const proxyConfig = createProxyData(proxyList)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
if (utils.isCreateProxyDataFile) {
|
|
42
|
+
const proxyFilePath = nodePath.join(proxyDir, `${_fileName}.ts`);
|
|
43
|
+
const proxyFileContent = `// 代理配置文件
|
|
44
44
|
// 自动生成于 ${new Date().toISOString()}
|
|
45
45
|
|
|
46
46
|
/**
|
|
@@ -58,10 +58,11 @@ export type ProxyItem = Record<string,{
|
|
|
58
58
|
export const proxyConfig: ProxyItem = ${JSON.stringify(proxyConfig, null, 2)};
|
|
59
59
|
export default proxyConfig;
|
|
60
60
|
`;
|
|
61
|
-
|
|
61
|
+
fs.writeFileSync(proxyFilePath, proxyFileContent);
|
|
62
|
+
}
|
|
63
|
+
|
|
62
64
|
// 存储原始的 proxyConfig 到 .cache.json 文件
|
|
63
65
|
const cacheFilePath = nodePath.join(proxyDir, _fileName + '.cache.json');
|
|
64
|
-
|
|
65
66
|
const cacheFileContent = JSON.stringify({
|
|
66
67
|
proxyList,
|
|
67
68
|
rootDir: _rootDir,
|
|
@@ -113,9 +114,9 @@ export const createMockFile = (mockList: DefineMockList, rootDir?: string, dir?:
|
|
|
113
114
|
fs.mkdirSync(mockDir, { recursive: true });
|
|
114
115
|
}
|
|
115
116
|
const mockConfig = createMockData(mockList)
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
if (utils.isCreateMockDataFile) {
|
|
118
|
+
const mockFilePath = nodePath.join(mockDir, `${_fileName}.ts`);
|
|
119
|
+
const mockFileContent = `// Mock 配置文件
|
|
119
120
|
// 自动生成于 ${new Date().toISOString()}
|
|
120
121
|
|
|
121
122
|
export interface MockerItem {
|
|
@@ -139,7 +140,9 @@ export type DefineMockList = MockerItem[];
|
|
|
139
140
|
export const mockList: DefineMockList = ${JSON.stringify(mockConfig, null, 2)};
|
|
140
141
|
export default mockList;
|
|
141
142
|
`;
|
|
142
|
-
|
|
143
|
+
fs.writeFileSync(mockFilePath, mockFileContent);
|
|
144
|
+
}
|
|
145
|
+
|
|
143
146
|
// 存储原始的 mockConfig 到 .cache.json 文件
|
|
144
147
|
const cacheFilePath = nodePath.join(mockDir, _fileName + '.cache.json');
|
|
145
148
|
|
package/src/utils/utils.ts
CHANGED
|
@@ -12,6 +12,10 @@ class Utils {
|
|
|
12
12
|
public file = 'index.mock';
|
|
13
13
|
/**代理文件名*/
|
|
14
14
|
public proxyFile = 'proxy';
|
|
15
|
+
/**是否生成mock数据文件*/
|
|
16
|
+
public isCreateMockDataFile = true;
|
|
17
|
+
/**是否生成proxy数据文件*/
|
|
18
|
+
public isCreateProxyDataFile = true;
|
|
15
19
|
|
|
16
20
|
/**设置根目录*/
|
|
17
21
|
setRootDir = (value?: string) => {
|
|
@@ -41,6 +45,21 @@ class Utils {
|
|
|
41
45
|
setProxyFile = (value?: string) => {
|
|
42
46
|
this.proxyFile = value || process.env.FAIRYS_MOCKER_PROXY_FILE || 'proxy';
|
|
43
47
|
}
|
|
48
|
+
|
|
49
|
+
/**设置 是否生成mock数据文件*/
|
|
50
|
+
setIsCreateMockDataFile = (fig?: boolean) => {
|
|
51
|
+
if (typeof fig === "boolean") {
|
|
52
|
+
this.isCreateMockDataFile = fig
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**设置 是否生成proxy数据文件*/
|
|
57
|
+
setIsCreateProxyDataFile = (fig?: boolean) => {
|
|
58
|
+
if (typeof fig === "boolean") {
|
|
59
|
+
this.isCreateProxyDataFile = fig
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
44
63
|
}
|
|
45
64
|
|
|
46
65
|
export const utils = new Utils()
|