json2dart-generator 1.1.6 → 1.1.7
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/core/json2dart.js +215 -1
- package/dist/core/mock_generator.js +191 -1
- package/dist/index.js +64 -1
- package/package.json +1 -1
package/dist/core/json2dart.js
CHANGED
|
@@ -1 +1,215 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.jsonToDart = jsonToDart;
|
|
4
|
+
const js_utils_plus_1 = require("js-utils-plus");
|
|
5
|
+
// 数据类型构造器
|
|
6
|
+
function modelTypeGenerator(key, value, className, suffix, nullSafety, numparse) {
|
|
7
|
+
if ((0, js_utils_plus_1.isBoolean)(value)) {
|
|
8
|
+
return {
|
|
9
|
+
type: 'bool',
|
|
10
|
+
typeTo: (key) => key,
|
|
11
|
+
typeFrom: (key) => `${key} as bool?`,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
if ((0, js_utils_plus_1.isString)(value)) {
|
|
15
|
+
return {
|
|
16
|
+
type: 'String',
|
|
17
|
+
typeTo: (key) => key,
|
|
18
|
+
typeFrom: (key) => `${key}?.toString()`,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if ((0, js_utils_plus_1.isNumber)(value)) {
|
|
22
|
+
if (numparse) {
|
|
23
|
+
return {
|
|
24
|
+
type: 'num',
|
|
25
|
+
typeTo: (key) => key,
|
|
26
|
+
typeFrom: (key) => `num.tryParse(${key}?.toString() ?? '')`,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return (0, js_utils_plus_1.isInt)(value)
|
|
30
|
+
? {
|
|
31
|
+
type: 'int',
|
|
32
|
+
typeTo: (key) => key,
|
|
33
|
+
typeFrom: (key) => `int.tryParse(${key}?.toString() ?? '')`,
|
|
34
|
+
}
|
|
35
|
+
: {
|
|
36
|
+
type: 'double',
|
|
37
|
+
typeTo: (key) => key,
|
|
38
|
+
typeFrom: (key) => `double.tryParse(${key}?.toString() ?? '')`,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
if ((0, js_utils_plus_1.isObject)(value)) {
|
|
42
|
+
const result = jsonToDart(value, className, suffix, nullSafety, numparse, false);
|
|
43
|
+
className = result.className || className;
|
|
44
|
+
return {
|
|
45
|
+
type: `${className}${suffix}`,
|
|
46
|
+
typeTo: (key) => `${key}?.toJson()`,
|
|
47
|
+
// typeFrom: (key: string) => `(${key} != null && ${key} is Map<String, dynamic>) ? ${className}.fromJson(${key}) : null`,
|
|
48
|
+
typeFrom: (key) => `${className}${suffix}.fromJson(${key} as Map<String, dynamic>?)`,
|
|
49
|
+
subLines: result.context,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if ((0, js_utils_plus_1.isArray)(value)) {
|
|
53
|
+
const firstChild = value[0];
|
|
54
|
+
const { type, typeTo, typeFrom, subLines } = modelTypeGenerator(key, firstChild, className, suffix, nullSafety, numparse);
|
|
55
|
+
return {
|
|
56
|
+
type: `List<${type}${nullSafety ? '?' : ''}>`,
|
|
57
|
+
typeTo: (key) => `${key}?.map((e) => ${typeTo('e')}).toList()`,
|
|
58
|
+
// typeFrom: (key: string) => `(${key} != null && ${key} is List) ? ${key}.map((e) => ${typeFrom('e')})?.toList() : null`,
|
|
59
|
+
typeFrom: (key) => `(${key} as List<dynamic>?)?.map((e) => ${typeFrom('e')}).toList()`,
|
|
60
|
+
subLines: subLines,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
console.log(`- 警告!:”${key}” 值包含为"空"的属性值或列表项,将被默认视为”String“类型`);
|
|
64
|
+
return {
|
|
65
|
+
type: 'String',
|
|
66
|
+
typeTo: (key) => key,
|
|
67
|
+
typeFrom: (key) => `${key}?.toString()`,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// 关键字和数字开头预处理
|
|
71
|
+
function modelKeywordDefence(key) {
|
|
72
|
+
let reservedKeywords = [
|
|
73
|
+
'num',
|
|
74
|
+
'double',
|
|
75
|
+
'int',
|
|
76
|
+
'String',
|
|
77
|
+
'bool',
|
|
78
|
+
'List',
|
|
79
|
+
'abstract',
|
|
80
|
+
'dynamic',
|
|
81
|
+
'implements',
|
|
82
|
+
'show',
|
|
83
|
+
'as',
|
|
84
|
+
'else',
|
|
85
|
+
'import',
|
|
86
|
+
'static',
|
|
87
|
+
'assert',
|
|
88
|
+
'enum',
|
|
89
|
+
'in',
|
|
90
|
+
'super',
|
|
91
|
+
'async',
|
|
92
|
+
'export',
|
|
93
|
+
'interface',
|
|
94
|
+
'switch',
|
|
95
|
+
'await',
|
|
96
|
+
'extends',
|
|
97
|
+
'is',
|
|
98
|
+
'sync',
|
|
99
|
+
'break',
|
|
100
|
+
'external',
|
|
101
|
+
'library',
|
|
102
|
+
'this',
|
|
103
|
+
'case',
|
|
104
|
+
'factory',
|
|
105
|
+
'mixin',
|
|
106
|
+
'throw',
|
|
107
|
+
'catch',
|
|
108
|
+
'false',
|
|
109
|
+
'new',
|
|
110
|
+
'true',
|
|
111
|
+
'class',
|
|
112
|
+
'final',
|
|
113
|
+
'null',
|
|
114
|
+
'try',
|
|
115
|
+
'const',
|
|
116
|
+
'finally',
|
|
117
|
+
'on',
|
|
118
|
+
'typedef',
|
|
119
|
+
'continue',
|
|
120
|
+
'for',
|
|
121
|
+
'operator',
|
|
122
|
+
'var',
|
|
123
|
+
'covariant',
|
|
124
|
+
'Function',
|
|
125
|
+
'part',
|
|
126
|
+
'void',
|
|
127
|
+
'default',
|
|
128
|
+
'get',
|
|
129
|
+
'rethrow',
|
|
130
|
+
'while',
|
|
131
|
+
'deferred',
|
|
132
|
+
'hide',
|
|
133
|
+
'return',
|
|
134
|
+
'with',
|
|
135
|
+
'do',
|
|
136
|
+
'if',
|
|
137
|
+
'set',
|
|
138
|
+
'yield',
|
|
139
|
+
];
|
|
140
|
+
if (reservedKeywords.includes(key) || /^\d/.test(key)) {
|
|
141
|
+
return `the${(0, js_utils_plus_1.uppercaseFirst)(key)}`;
|
|
142
|
+
}
|
|
143
|
+
return (0, js_utils_plus_1.snakeToCamel)(key);
|
|
144
|
+
}
|
|
145
|
+
// 解析类名冲突(避免重复生成相同结构的类)
|
|
146
|
+
function resolveClassName(className, value, classMap) {
|
|
147
|
+
// 生成当前类的属性签名(属性名+原始类型)
|
|
148
|
+
const signature = Object.entries(value)
|
|
149
|
+
.map(([k, v]) => `${k}=${typeof v}`)
|
|
150
|
+
.join('&');
|
|
151
|
+
// 无冲突,使用当前类名
|
|
152
|
+
if (!classMap[className]) {
|
|
153
|
+
classMap[className] = signature;
|
|
154
|
+
return className;
|
|
155
|
+
}
|
|
156
|
+
// 已有完全相同的类,直接忽略
|
|
157
|
+
if (classMap[className] && classMap[className] == signature) {
|
|
158
|
+
return '';
|
|
159
|
+
}
|
|
160
|
+
// 冲突且结构不同,尝试添加数字后缀
|
|
161
|
+
let idx = 2;
|
|
162
|
+
let newName = `${className}${idx}`;
|
|
163
|
+
while (classMap[newName] && classMap[newName] !== signature) {
|
|
164
|
+
idx++;
|
|
165
|
+
newName = `${className}${idx}`;
|
|
166
|
+
}
|
|
167
|
+
if (!classMap[newName])
|
|
168
|
+
classMap[newName] = signature;
|
|
169
|
+
return newName;
|
|
170
|
+
}
|
|
171
|
+
// json转dart模型
|
|
172
|
+
function jsonToDart(value, className, suffix, nullSafety, numparse, isRoot = true) {
|
|
173
|
+
// 使用闭包管理类映射,避免全局污染
|
|
174
|
+
const classMap = isRoot ? {} : jsonToDart._classMap;
|
|
175
|
+
if (isRoot)
|
|
176
|
+
jsonToDart._classMap = classMap;
|
|
177
|
+
// 规范化类名并解决类名冲突
|
|
178
|
+
const baseClassName = (0, js_utils_plus_1.uppercaseFirst)((0, js_utils_plus_1.snakeToCamel)(className));
|
|
179
|
+
className = resolveClassName(baseClassName, value, classMap);
|
|
180
|
+
if (!className)
|
|
181
|
+
return { context: '' };
|
|
182
|
+
let classLines = [];
|
|
183
|
+
let propsLines = [];
|
|
184
|
+
let constructorLines = [];
|
|
185
|
+
let fromJsonLines = [];
|
|
186
|
+
let toJsonLines = [];
|
|
187
|
+
let subClassLines = [];
|
|
188
|
+
classLines.push(`class ${className}${suffix} {`);
|
|
189
|
+
constructorLines.push(` ${className}${suffix}({\n`);
|
|
190
|
+
fromJsonLines.push(` ${className}${suffix}.fromJson(Map<String, dynamic>? json) {\n`);
|
|
191
|
+
toJsonLines.push(` Map<String, dynamic> toJson() => {\n`);
|
|
192
|
+
for (const [k, v] of Object.entries(value)) {
|
|
193
|
+
let jsonKey = `'${k}'`;
|
|
194
|
+
let propKey = modelKeywordDefence(k);
|
|
195
|
+
const subClassName = (0, js_utils_plus_1.uppercaseFirst)((0, js_utils_plus_1.snakeToCamel)(k));
|
|
196
|
+
const { type, typeTo, typeFrom, subLines } = modelTypeGenerator(k, v, subClassName, suffix, nullSafety, numparse);
|
|
197
|
+
if (subLines)
|
|
198
|
+
subClassLines.push(subLines);
|
|
199
|
+
constructorLines.push(` this.${propKey},\n`);
|
|
200
|
+
propsLines.push(` ${type}${nullSafety ? '?' : ''} ${propKey};\n`);
|
|
201
|
+
fromJsonLines.push(` ${propKey} = ${typeFrom(`json?[${jsonKey}]`)};\n`);
|
|
202
|
+
toJsonLines.push(` ${jsonKey}: ${typeTo(propKey)},\n`);
|
|
203
|
+
}
|
|
204
|
+
constructorLines.push(` });`);
|
|
205
|
+
fromJsonLines.push(` }`);
|
|
206
|
+
toJsonLines.push(` };`);
|
|
207
|
+
classLines.push(propsLines.join(''));
|
|
208
|
+
classLines.push(constructorLines.join(''));
|
|
209
|
+
classLines.push(fromJsonLines.join(''));
|
|
210
|
+
classLines.push(toJsonLines.join(''));
|
|
211
|
+
classLines.push(`}\n`);
|
|
212
|
+
classLines.push(subClassLines.join(''));
|
|
213
|
+
const context = isRoot ? classLines.join('\n').replace(/[\n\r]?$/g, '') : classLines.join('\n');
|
|
214
|
+
return { context, className };
|
|
215
|
+
}
|
|
@@ -1 +1,191 @@
|
|
|
1
|
-
'use strict';function a1_0x13ab(_0x469432,_0x377f3f){_0x469432=_0x469432-0x165;const _0x457408=a1_0x4574();let _0x13ab30=_0x457408[_0x469432];return _0x13ab30;}const a1_0x3793ac=a1_0x13ab;(function(_0x315f78,_0x59648b){const _0x273ba7=a1_0x13ab,_0x21eacc=_0x315f78();while(!![]){try{const _0xce9bb0=-parseInt(_0x273ba7(0x16d))/0x1*(-parseInt(_0x273ba7(0x16f))/0x2)+-parseInt(_0x273ba7(0x169))/0x3+parseInt(_0x273ba7(0x167))/0x4*(-parseInt(_0x273ba7(0x172))/0x5)+-parseInt(_0x273ba7(0x166))/0x6*(-parseInt(_0x273ba7(0x16b))/0x7)+-parseInt(_0x273ba7(0x165))/0x8*(-parseInt(_0x273ba7(0x173))/0x9)+-parseInt(_0x273ba7(0x168))/0xa*(-parseInt(_0x273ba7(0x17a))/0xb)+parseInt(_0x273ba7(0x170))/0xc*(-parseInt(_0x273ba7(0x175))/0xd);if(_0xce9bb0===_0x59648b)break;else _0x21eacc['push'](_0x21eacc['shift']());}catch(_0x1dd1ae){_0x21eacc['push'](_0x21eacc['shift']());}}}(a1_0x4574,0x54982));Object['defineProperty'](exports,a1_0x3793ac(0x177),{'value':!![]}),exports[a1_0x3793ac(0x16c)]=generateMockDart;const js_utils_plus_1=require(a1_0x3793ac(0x16e));function a1_0x4574(){const _0x10a9e7=['4842770ByFCoy','725739oUsHux','objectToContent','14laxMxU','generateMockDart','7059ixBqdt','js-utils-plus','78wLsxjO','6828KNaoAW','\x20数据\x0a\x20\x20static\x20Map<String,\x20dynamic>\x20','10LMxMqq','17361mSmYjS',');\x0a\x20\x20}\x0a','7631ibnNkn','import\x20\x27dart:math\x27;\x0a\x0a///\x20提供各类随机数据的生成方法\x0aclass\x20MockDataHelper\x20{\x0a\x20\x20static\x20final\x20Random\x20_random\x20=\x20Random();\x0a\x0a\x20\x20///\x20随机整数\x20[min,\x20max]\x0a\x20\x20static\x20int\x20randomInt({int\x20min\x20=\x200,\x20int\x20max\x20=\x20100})\x20=>\x20min\x20+\x20_random.nextInt(max\x20-\x20min\x20+\x201);\x0a\x0a\x20\x20///\x20随机浮点数\x20[min,\x20max)\x0a\x20\x20static\x20double\x20randomDouble({double\x20min\x20=\x200.0,\x20double\x20max\x20=\x20100.0})\x20=>\x20min\x20+\x20_random.nextDouble()\x20*\x20(max\x20-\x20min);\x0a\x0a\x20\x20///\x20随机布尔值\x0a\x20\x20static\x20bool\x20randomBool()\x20=>\x20_random.nextBool();\x0a\x0a\x20\x20///\x20随机枚举值(传入枚举值列表)\x0a\x20\x20static\x20T\x20randomEnum<T>(List<T>\x20values)\x20{\x0a\x20\x20\x20\x20return\x20values[randomInt(min:\x200,\x20max:\x20values.length\x20-\x201)];\x0a\x20\x20}\x0a\x0a\x20\x20///\x20随机字符串(默认长度\x205-15)\x0a\x20\x20static\x20String\x20randomString({int\x20minLength\x20=\x205,\x20int\x20maxLength\x20=\x2015})\x20{\x0a\x20\x20\x20\x20const\x20chars\x20=\x20\x27abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x27;\x0a\x20\x20\x20\x20final\x20length\x20=\x20randomInt(min:\x20minLength,\x20max:\x20maxLength);\x0a\x20\x20\x20\x20return\x20String.fromCharCodes(Iterable.generate(length,\x20(_)\x20=>\x20chars.codeUnitAt(_random.nextInt(chars.length))));\x0a\x20\x20}\x0a\x0a\x20\x20///\x20从列表中随机取一项\x0a\x20\x20static\x20T\x20randomItem<T>(List<T>\x20items)\x20{\x0a\x20\x20\x20\x20if\x20(items.isEmpty)\x20throw\x20Exception(\x27Cannot\x20pick\x20from\x20empty\x20list\x27);\x0a\x20\x20\x20\x20return\x20items[randomInt(min:\x200,\x20max:\x20items.length\x20-\x201)];\x0a\x20\x20}\x0a\x0a\x20\x20///\x20随机生成图片\x20URL(使用占位图服务)\x0a\x20\x20static\x20String\x20randomImageUrl({int\x20width\x20=\x20200,\x20int\x20height\x20=\x20200})\x20{\x0a\x20\x20\x20\x20return\x20\x27https://picsum.photos/$width/$height?random=${_random.nextInt(10000)}\x27;\x0a\x20\x20}\x0a\x0a\x20\x20///\x20随机生成邮箱\x0a\x20\x20static\x20String\x20randomEmail()\x20{\x0a\x20\x20\x20\x20return\x20\x27${randomString(minLength:\x203,\x20maxLength:\x2010)}@example.com\x27;\x0a\x20\x20}\x0a\x0a\x20\x20///\x20随机生成手机号\x0a\x20\x20static\x20String\x20randomPhone()\x20{\x0a\x20\x20\x20\x20return\x20\x2713${List.generate(10,\x20(_)\x20=>\x20_random.nextInt(9)).join()}\x27;\x0a\x20\x20}\x0a\x0a\x20\x20///\x20随机生成Json\x0a\x20\x20static\x20Map<String,\x20dynamic>\x20randomJsonFromTemplate(Map<String,\x20dynamic>\x20template)\x20{\x0a\x20\x20\x20\x20final\x20result\x20=\x20<String,\x20dynamic>{};\x0a\x20\x20\x20\x20for\x20(final\x20entry\x20in\x20template.entries)\x20{\x0a\x20\x20\x20\x20\x20\x20final\x20key\x20=\x20entry.key;\x0a\x20\x20\x20\x20\x20\x20final\x20value\x20=\x20entry.value;\x0a\x20\x20\x20\x20\x20\x20if\x20(value\x20is\x20int)\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20result[key]\x20=\x20value\x20==\x200\x20?\x20_mockIntForKey(key)\x20:\x20value;\x0a\x20\x20\x20\x20\x20\x20}\x20else\x20if\x20(value\x20is\x20double)\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20result[key]\x20=\x20value\x20==\x200.0\x20||\x20value\x20==\x200.01\x20?\x20_mockDoubleForKey(key)\x20:\x20value;\x0a\x20\x20\x20\x20\x20\x20}\x20else\x20if\x20(value\x20is\x20bool)\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20result[key]\x20=\x20value;\x0a\x20\x20\x20\x20\x20\x20}\x20else\x20if\x20(value\x20is\x20String)\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20result[key]\x20=\x20_mockStringForKey(key,\x20value);\x0a\x20\x20\x20\x20\x20\x20}\x20else\x20if\x20(value\x20is\x20List)\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20final\x20itemTemplate\x20=\x20value.isNotEmpty\x20?\x20value.first\x20:\x20null;\x0a\x20\x20\x20\x20\x20\x20\x20\x20final\x20length\x20=\x20randomInt(min:\x202,\x20max:\x205);\x0a\x20\x20\x20\x20\x20\x20\x20\x20result[key]\x20=\x20List.generate(length,\x20(_)\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20if\x20(itemTemplate\x20is\x20Map)\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20return\x20randomJsonFromTemplate(itemTemplate\x20as\x20Map<String,\x20dynamic>);\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20}\x20else\x20if\x20(itemTemplate\x20!=\x20null)\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20return\x20_randomValueFromType(itemTemplate.runtimeType);\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20}\x20else\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20return\x20randomString();\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20}\x0a\x20\x20\x20\x20\x20\x20\x20\x20});\x0a\x20\x20\x20\x20\x20\x20}\x20else\x20if\x20(value\x20is\x20Map)\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20result[key]\x20=\x20randomJsonFromTemplate(value\x20as\x20Map<String,\x20dynamic>);\x0a\x20\x20\x20\x20\x20\x20}\x20else\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20result[key]\x20=\x20_randomValueFromType(value.runtimeType);\x0a\x20\x20\x20\x20\x20\x20}\x0a\x20\x20\x20\x20}\x0a\x20\x20\x20\x20return\x20result;\x0a\x20\x20}\x0a\x0a\x20\x20///\x20根据键名推断整型\x20Mock\x20值\x0a\x20\x20static\x20int\x20_mockIntForKey(String\x20key)\x20{\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27id\x27,\x20\x27Id\x27]))\x20return\x20randomInt(min:\x20100000,\x20max:\x20999999);\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27state\x27,\x20\x27State\x27,\x20\x27status\x27,\x20\x27Status\x27,\x20\x27type\x27,\x20\x27Type\x27,\x20\x27class\x27,\x20\x27Class\x27]))\x20return\x20randomInt(min:\x200,\x20max:\x205);\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27count\x27,\x20\x27Count\x27,\x20\x27total\x27,\x20\x27Total\x27,\x20\x27num\x27,\x20\x27Num\x27,\x20\x27days\x27,\x20\x27Days\x27]))\x20return\x20randomInt(min:\x200,\x20max:\x20200);\x0a\x20\x20\x20\x20if\x20(key.startsWith(\x27is\x27))\x20return\x20randomInt(min:\x200,\x20max:\x201);\x0a\x20\x20\x20\x20return\x20randomInt();\x0a\x20\x20}\x0a\x0a\x20\x20///\x20根据键名推断\x20double\x20Mock\x20值\x0a\x20\x20static\x20double\x20_mockDoubleForKey(String\x20key)\x20{\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27lat\x27,\x20\x27Lat\x27,\x20\x27lng\x27,\x20\x27Lng\x27]))\x20return\x20randomDouble(min:\x2020.0,\x20max:\x2050.0);\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27percent\x27,\x20\x27Percent\x27,\x20\x27rate\x27,\x20\x27Rate\x27]))\x20return\x20randomDouble(min:\x200,\x20max:\x20100);\x0a\x20\x20\x20\x20return\x20randomDouble();\x0a\x20\x20}\x0a\x0a\x20\x20///\x20根据键名推断字符串\x20Mock\x20值。保留有意义的样本数据(中文、非空、非占位符)\x0a\x20\x20static\x20String\x20_mockStringForKey(String\x20key,\x20String\x20original)\x20{\x0a\x20\x20\x20\x20//\x20保留包含中文或有意义内容的样本值(非空且非\x22string\x22占位符)\x0a\x20\x20\x20\x20if\x20(original.isNotEmpty\x20&&\x20original\x20!=\x20\x27string\x27)\x20{\x0a\x20\x20\x20\x20\x20\x20return\x20original;\x0a\x20\x20\x20\x20}\x0a\x20\x20\x20\x20if\x20(key.endsWith(\x27Date\x27))\x20return\x20_randomDateString();\x0a\x20\x20\x20\x20if\x20(key.endsWith(\x27Time\x27)\x20&&\x20!key.endsWith(\x27DateTime\x27))\x20return\x20_randomTimeString();\x0a\x20\x20\x20\x20if\x20(key.endsWith(\x27DateTime\x27))\x20return\x20\x27${_randomDateString()}\x20${_randomTimeString()}\x27;\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27email\x27]))\x20return\x20randomEmail();\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27phone\x27,\x20\x27mobile\x27]))\x20return\x20randomPhone();\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27sn\x27]))\x20return\x20\x27SN${DateTime.now().year}${randomInt(min:\x20100000,\x20max:\x20999999)}\x27;\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27no\x27,\x20\x27num\x27]))\x20return\x20\x27${DateTime.now().year}${randomInt(min:\x20100000,\x20max:\x20999999)}\x27;\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27addr\x27]))\x20return\x20\x27${randomItem(_cities)}${randomItem(_districts)}${randomItem(_streets)}${randomInt(min:\x201,\x20max:\x20200)}号\x27;\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27version\x27]))\x20return\x20\x27${randomInt(min:\x201,\x20max:\x205)}.${randomInt(min:\x200,\x20max:\x209)}.${randomInt(min:\x200,\x20max:\x2099)}\x27;\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27desc\x27,\x20\x27remark\x27,\x20\x27content\x27]))\x20return\x20\x27${randomItem(_sentences)},${randomItem(_sentences)}。\x27;\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27url\x27,\x20\x27image\x27,\x20\x27avatar\x27]))\x20return\x20randomImageUrl();\x0a\x20\x20\x20\x20if\x20(_keyContains(key,\x20[\x27name\x27,\x20\x27title\x27]))\x20return\x20randomItem(_names);\x0a\x20\x20\x20\x20return\x20randomString();\x0a\x20\x20}\x0a\x0a\x20\x20static\x20const\x20_names\x20=\x20[\x27张三\x27,\x20\x27李四\x27,\x20\x27王五\x27,\x20\x27赵六\x27,\x20\x27孙七\x27,\x20\x27周八\x27,\x20\x27吴九\x27,\x20\x27郑十\x27,\x20\x27设备A\x27,\x20\x27项目B\x27,\x20\x27订单C\x27];\x0a\x20\x20static\x20const\x20_sentences\x20=\x20[\x27设备运行正常\x27,\x20\x27已完成例行保养\x27,\x20\x27等待配件更换\x27,\x20\x27需要现场技术支持\x27,\x20\x27客户已确认验收\x27,\x20\x27审批流程进行中\x27,\x20\x27待发货状态\x27];\x0a\x20\x20static\x20const\x20_cities\x20=\x20[\x27北京市\x27,\x20\x27上海市\x27,\x20\x27广州市\x27,\x20\x27深圳市\x27,\x20\x27杭州市\x27,\x20\x27成都市\x27,\x20\x27武汉市\x27,\x20\x27南京市\x27];\x0a\x20\x20static\x20const\x20_districts\x20=\x20[\x27朝阳区\x27,\x20\x27浦东新区\x27,\x20\x27天河区\x27,\x20\x27南山区\x27,\x20\x27西湖区\x27,\x20\x27武侯区\x27,\x20\x27洪山区\x27,\x20\x27鼓楼区\x27];\x0a\x20\x20static\x20const\x20_streets\x20=\x20[\x27中山路\x27,\x20\x27人民路\x27,\x20\x27建设路\x27,\x20\x27解放路\x27,\x20\x27长安街\x27,\x20\x27科技路\x27,\x20\x27高新大道\x27,\x20\x27滨江路\x27];\x0a\x0a\x20\x20///\x20随机日期时间\x0a\x20\x20static\x20DateTime\x20_randomDateTime({DateTime?\x20start,\x20DateTime?\x20end})\x20{\x0a\x20\x20\x20\x20final\x20now\x20=\x20DateTime.now();\x0a\x20\x20\x20\x20final\x20startDate\x20=\x20start\x20??\x20DateTime(now.year\x20-\x203,\x201,\x201);\x0a\x20\x20\x20\x20final\x20endDate\x20=\x20end\x20??\x20now;\x0a\x20\x20\x20\x20final\x20difference\x20=\x20endDate.difference(startDate).inMilliseconds;\x0a\x20\x20\x20\x20final\x20randomMilli\x20=\x20randomInt(min:\x200,\x20max:\x20difference);\x0a\x20\x20\x20\x20return\x20startDate.add(Duration(milliseconds:\x20randomMilli));\x0a\x20\x20}\x0a\x0a\x20\x20///\x20YYYY-MM-DD\x20格式的随机日期字符串\x0a\x20\x20static\x20String\x20_randomDateString()\x20{\x0a\x20\x20\x20\x20final\x20date\x20=\x20_randomDateTime();\x0a\x20\x20\x20\x20return\x20\x27${date.year}-${date.month.toString().padLeft(2,\x20\x270\x27)}-${date.day.toString().padLeft(2,\x20\x270\x27)}\x27;\x0a\x20\x20}\x0a\x0a\x20\x20///\x20HH:mm:ss\x20格式的随机时间字符串\x0a\x20\x20static\x20String\x20_randomTimeString()\x20{\x0a\x20\x20\x20\x20final\x20date\x20=\x20_randomDateTime();\x0a\x20\x20\x20\x20return\x20\x27${date.hour.toString().padLeft(2,\x20\x270\x27)}:${date.minute.toString().padLeft(2,\x20\x270\x27)}:${date.second.toString().padLeft(2,\x20\x270\x27)}\x27;\x0a\x20\x20}\x0a\x0a\x20\x20///\x20键名是否包含任意关键字(忽略大小写)\x0a\x20\x20static\x20bool\x20_keyContains(String\x20key,\x20List<String>\x20keywords)\x20{\x0a\x20\x20\x20\x20final\x20lower\x20=\x20key.toLowerCase();\x0a\x20\x20\x20\x20return\x20keywords.any((k)\x20=>\x20lower.contains(k.toLowerCase()));\x0a\x20\x20}\x0a\x0a\x20\x20static\x20dynamic\x20_randomValueFromType(Type\x20type)\x20{\x0a\x20\x20\x20\x20if\x20(type\x20==\x20int)\x20return\x20randomInt();\x0a\x20\x20\x20\x20if\x20(type\x20==\x20double)\x20return\x20randomDouble();\x0a\x20\x20\x20\x20if\x20(type\x20==\x20bool)\x20return\x20randomBool();\x0a\x20\x20\x20\x20if\x20(type\x20==\x20String)\x20return\x20randomString();\x0a\x20\x20\x20\x20return\x20null;\x0a\x20\x20}\x0a}\x0a\x0a///\x20生成各类模型的随机\x20Mock\x20数据\x0aclass\x20Mock\x20{\x0a\x0a\x20\x20///\x20生成指定数量的\x20Mock\x20数组\x0a\x20\x20static\x20List<Map<String,\x20dynamic>>\x20list(int\x20count,\x20Map<String,\x20dynamic>\x20Function()\x20generator)\x20{\x0a\x20\x20\x20\x20return\x20List.generate(count,\x20(_)\x20=>\x20generator());\x0a\x20\x20}\x0a','__esModule','()\x20{\x0a\x20\x20\x20\x20return\x20MockDataHelper.randomJsonFromTemplate(','\x0a\x20\x20///\x20生成随机\x20','11IaCIzi','496pFwZfp','1095372MRAVSJ','643772duIbVD'];a1_0x4574=function(){return _0x10a9e7;};return a1_0x4574();}function generateMockDart(_0x375013,_0x1866a0){const _0x4a6239=a1_0x3793ac;let _0x4479c1='';for(const {name:_0xca0bb3,json:_0x439e5e}of _0x375013){const _0x128103=(0x0,js_utils_plus_1[_0x4a6239(0x16a)])(_0x439e5e,'',0x3);_0x4479c1+=_0x4a6239(0x179)+_0xca0bb3+_0x1866a0+_0x4a6239(0x171)+_0xca0bb3+_0x4a6239(0x178)+_0x128103+_0x4a6239(0x174);}return _0x4a6239(0x176)+_0x4479c1+'}\x0a';}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateMockDart = generateMockDart;
|
|
4
|
+
const js_utils_plus_1 = require("js-utils-plus");
|
|
5
|
+
/// 生成 Mock 类的完整 dart 文件内容
|
|
6
|
+
function generateMockDart(mockMethods, suffix) {
|
|
7
|
+
let mockClassMethods = '';
|
|
8
|
+
for (const { name, json } of mockMethods) {
|
|
9
|
+
const dartMap = (0, js_utils_plus_1.objectToContent)(json, '', 3);
|
|
10
|
+
mockClassMethods += `
|
|
11
|
+
/// 生成随机 ${(0, js_utils_plus_1.snakeToCamel)(name)}${suffix} 数据
|
|
12
|
+
static Map<String, dynamic> ${(0, js_utils_plus_1.snakeToCamel)(name)}${suffix}() {
|
|
13
|
+
return MockDataHelper.randomJsonFromTemplate(${dartMap});
|
|
14
|
+
}
|
|
15
|
+
`;
|
|
16
|
+
}
|
|
17
|
+
return `import 'dart:math';
|
|
18
|
+
|
|
19
|
+
/// 提供各类随机数据的生成方法
|
|
20
|
+
class MockDataHelper {
|
|
21
|
+
static final Random _random = Random();
|
|
22
|
+
|
|
23
|
+
/// 随机整数 [min, max]
|
|
24
|
+
static int randomInt({int min = 0, int max = 100}) => min + _random.nextInt(max - min + 1);
|
|
25
|
+
|
|
26
|
+
/// 随机浮点数 [min, max)
|
|
27
|
+
static double randomDouble({double min = 0.0, double max = 100.0}) => min + _random.nextDouble() * (max - min);
|
|
28
|
+
|
|
29
|
+
/// 随机布尔值
|
|
30
|
+
static bool randomBool() => _random.nextBool();
|
|
31
|
+
|
|
32
|
+
/// 随机枚举值(传入枚举值列表)
|
|
33
|
+
static T randomEnum<T>(List<T> values) {
|
|
34
|
+
return values[randomInt(min: 0, max: values.length - 1)];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// 随机字符串(默认长度 5-15)
|
|
38
|
+
static String randomString({int minLength = 5, int maxLength = 15}) {
|
|
39
|
+
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
40
|
+
final length = randomInt(min: minLength, max: maxLength);
|
|
41
|
+
return String.fromCharCodes(Iterable.generate(length, (_) => chars.codeUnitAt(_random.nextInt(chars.length))));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// 从列表中随机取一项
|
|
45
|
+
static T randomItem<T>(List<T> items) {
|
|
46
|
+
if (items.isEmpty) throw Exception('Cannot pick from empty list');
|
|
47
|
+
return items[randomInt(min: 0, max: items.length - 1)];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/// 随机生成图片 URL(使用占位图服务)
|
|
51
|
+
static String randomImageUrl({int width = 200, int height = 200}) {
|
|
52
|
+
return 'https://picsum.photos/\$width/\$height?random=\${_random.nextInt(10000)}';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// 随机生成邮箱
|
|
56
|
+
static String randomEmail() {
|
|
57
|
+
return '\${randomString(minLength: 3, maxLength: 10)}@example.com';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// 随机生成手机号
|
|
61
|
+
static String randomPhone() {
|
|
62
|
+
return '13\${List.generate(10, (_) => _random.nextInt(9)).join()}';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// 随机生成Json
|
|
66
|
+
static Map<String, dynamic> randomJsonFromTemplate(Map<String, dynamic> template) {
|
|
67
|
+
final result = <String, dynamic>{};
|
|
68
|
+
for (final entry in template.entries) {
|
|
69
|
+
final key = entry.key;
|
|
70
|
+
final value = entry.value;
|
|
71
|
+
if (value is int) {
|
|
72
|
+
result[key] = value == 0 ? _mockIntForKey(key) : value;
|
|
73
|
+
} else if (value is double) {
|
|
74
|
+
result[key] = value == 0.0 || value == 0.01 ? _mockDoubleForKey(key) : value;
|
|
75
|
+
} else if (value is bool) {
|
|
76
|
+
result[key] = value;
|
|
77
|
+
} else if (value is String) {
|
|
78
|
+
result[key] = _mockStringForKey(key, value);
|
|
79
|
+
} else if (value is List) {
|
|
80
|
+
final itemTemplate = value.isNotEmpty ? value.first : null;
|
|
81
|
+
final length = randomInt(min: 2, max: 12);
|
|
82
|
+
result[key] = List.generate(length, (_) {
|
|
83
|
+
if (itemTemplate is Map) {
|
|
84
|
+
return randomJsonFromTemplate(itemTemplate as Map<String, dynamic>);
|
|
85
|
+
} else if (itemTemplate != null) {
|
|
86
|
+
return _randomValueFromType(itemTemplate.runtimeType);
|
|
87
|
+
} else {
|
|
88
|
+
return randomString();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
} else if (value is Map) {
|
|
92
|
+
result[key] = randomJsonFromTemplate(value as Map<String, dynamic>);
|
|
93
|
+
} else {
|
|
94
|
+
result[key] = _randomValueFromType(value.runtimeType);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/// 根据键名推断整型 Mock 值
|
|
101
|
+
static int _mockIntForKey(String key) {
|
|
102
|
+
if (_keyContains(key, ['id', 'Id'])) return randomInt(min: 100000, max: 999999);
|
|
103
|
+
if (_keyContains(key, ['state', 'State', 'status', 'Status', 'type', 'Type', 'class', 'Class'])) return randomInt(min: 0, max: 5);
|
|
104
|
+
if (_keyContains(key, ['count', 'Count', 'total', 'Total', 'num', 'Num', 'days', 'Days'])) return randomInt(min: 0, max: 200);
|
|
105
|
+
if (key.startsWith('is')) return randomInt(min: 0, max: 1);
|
|
106
|
+
return randomInt();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// 根据键名推断 double Mock 值
|
|
110
|
+
static double _mockDoubleForKey(String key) {
|
|
111
|
+
if (_keyContains(key, ['lat', 'Lat', 'lng', 'Lng'])) return randomDouble(min: 20.0, max: 50.0);
|
|
112
|
+
if (_keyContains(key, ['percent', 'Percent', 'rate', 'Rate'])) return randomDouble(min: 0, max: 100);
|
|
113
|
+
return randomDouble();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/// 根据键名推断字符串 Mock 值。保留有意义的样本数据(中文、非空、非占位符)
|
|
117
|
+
static String _mockStringForKey(String key, String original) {
|
|
118
|
+
// 保留包含中文或有意义内容的样本值(非空且非"string"占位符)
|
|
119
|
+
if (original.isNotEmpty && original != 'string') {
|
|
120
|
+
return original;
|
|
121
|
+
}
|
|
122
|
+
if (key.endsWith('Date')) return _randomDateString();
|
|
123
|
+
if (key.endsWith('Time') && !key.endsWith('DateTime')) return _randomTimeString();
|
|
124
|
+
if (key.endsWith('DateTime')) return '\${_randomDateString()} \${_randomTimeString()}';
|
|
125
|
+
if (_keyContains(key, ['email'])) return randomEmail();
|
|
126
|
+
if (_keyContains(key, ['phone', 'mobile'])) return randomPhone();
|
|
127
|
+
if (_keyContains(key, ['sn'])) return 'SN\${DateTime.now().year}\${randomInt(min: 100000, max: 999999)}';
|
|
128
|
+
if (_keyContains(key, ['no', 'num'])) return '\${DateTime.now().year}\${randomInt(min: 100000, max: 999999)}';
|
|
129
|
+
if (_keyContains(key, ['addr'])) return '\${randomItem(_cities)}\${randomItem(_districts)}\${randomItem(_streets)}\${randomInt(min: 1, max: 200)}号';
|
|
130
|
+
if (_keyContains(key, ['version'])) return '\${randomInt(min: 1, max: 5)}.\${randomInt(min: 0, max: 9)}.\${randomInt(min: 0, max: 99)}';
|
|
131
|
+
if (_keyContains(key, ['desc', 'remark', 'content'])) return '\${randomItem(_sentences)},\${randomItem(_sentences)}。';
|
|
132
|
+
if (_keyContains(key, ['url', 'image', 'avatar'])) return randomImageUrl();
|
|
133
|
+
if (_keyContains(key, ['name', 'title'])) return randomItem(_names);
|
|
134
|
+
return randomString();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static const _names = ['张三', '李四', '王五', '赵六', '孙七', '周八', '吴九', '郑十', '设备A', '项目B', '订单C'];
|
|
138
|
+
static const _sentences = ['设备运行正常', '已完成例行保养', '等待配件更换', '需要现场技术支持', '客户已确认验收', '审批流程进行中', '待发货状态'];
|
|
139
|
+
static const _cities = ['北京市', '上海市', '广州市', '深圳市', '杭州市', '成都市', '武汉市', '南京市'];
|
|
140
|
+
static const _districts = ['朝阳区', '浦东新区', '天河区', '南山区', '西湖区', '武侯区', '洪山区', '鼓楼区'];
|
|
141
|
+
static const _streets = ['中山路', '人民路', '建设路', '解放路', '长安街', '科技路', '高新大道', '滨江路'];
|
|
142
|
+
|
|
143
|
+
/// 随机日期时间(以小时为精度)
|
|
144
|
+
static DateTime _randomDateTime({DateTime? start, DateTime? end}) {
|
|
145
|
+
final now = DateTime.now();
|
|
146
|
+
final startDate = start ?? DateTime(now.year - 3, 1, 1);
|
|
147
|
+
final endDate = end ?? now;
|
|
148
|
+
final hours = endDate.difference(startDate).inHours;
|
|
149
|
+
final randomHours = randomInt(min: 0, max: hours);
|
|
150
|
+
return startDate.add(Duration(hours: randomHours));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/// YYYY-MM-DD 格式的随机日期字符串
|
|
154
|
+
static String _randomDateString() {
|
|
155
|
+
final date = _randomDateTime();
|
|
156
|
+
return '\${date.year}-\${date.month.toString().padLeft(2, '0')}-\${date.day.toString().padLeft(2, '0')}';
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/// HH:mm:ss 格式的随机时间字符串
|
|
160
|
+
static String _randomTimeString() {
|
|
161
|
+
final h = randomInt(min: 0, max: 23).toString().padLeft(2, '0');
|
|
162
|
+
final m = randomInt(min: 0, max: 59).toString().padLeft(2, '0');
|
|
163
|
+
final s = randomInt(min: 0, max: 59).toString().padLeft(2, '0');
|
|
164
|
+
return '$h:$m:$s';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/// 键名是否包含任意关键字(忽略大小写)
|
|
168
|
+
static bool _keyContains(String key, List<String> keywords) {
|
|
169
|
+
final lower = key.toLowerCase();
|
|
170
|
+
return keywords.any((k) => lower.contains(k.toLowerCase()));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
static dynamic _randomValueFromType(Type type) {
|
|
174
|
+
if (type == int) return randomInt();
|
|
175
|
+
if (type == double) return randomDouble();
|
|
176
|
+
if (type == bool) return randomBool();
|
|
177
|
+
if (type == String) return randomString();
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/// 生成各类模型的随机 Mock 数据
|
|
183
|
+
class Mock {
|
|
184
|
+
|
|
185
|
+
/// 生成指定数量的 Mock 数组
|
|
186
|
+
static List<Map<String, dynamic>> list(int count, Map<String, dynamic> Function() generator) {
|
|
187
|
+
return List.generate(count, (_) => generator());
|
|
188
|
+
}
|
|
189
|
+
${mockClassMethods}}
|
|
190
|
+
`;
|
|
191
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,65 @@
|
|
|
1
1
|
#! /usr/bin/env node
|
|
2
|
-
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const json2dart_1 = require("./core/json2dart");
|
|
8
|
+
const mock_generator_1 = require("./core/mock_generator");
|
|
9
|
+
const js_utils_plus_1 = require("js-utils-plus");
|
|
10
|
+
const commander_1 = require("commander");
|
|
11
|
+
const path_1 = __importDefault(require("path"));
|
|
12
|
+
const fs_1 = __importDefault(require("fs"));
|
|
13
|
+
const program = new commander_1.Command();
|
|
14
|
+
const _path = process.cwd();
|
|
15
|
+
// 获取命令行参数
|
|
16
|
+
program
|
|
17
|
+
.option('-d, --dir <dir>', 'json文件夹路径,默认 .json_models/')
|
|
18
|
+
.option('-o, --output <dir>', 'dart模型输出路径,默认 lib/models')
|
|
19
|
+
.option('-s, --suffix <value>', '自定义dart模型类名后缀, 如 Entity、Model')
|
|
20
|
+
.option('-e, --excludes <value...>', '需要排除的文件或文件夹,可指定多个文件')
|
|
21
|
+
.option('--numparse', '使用num数据类型而不是double/int')
|
|
22
|
+
.option('--nullsafety', '是否启用空安全')
|
|
23
|
+
.option('--mock', '是否生成Mock类');
|
|
24
|
+
program.parse();
|
|
25
|
+
const options = Object.assign({ dir: '.json_models/', output: 'lib/models', suffix: '' }, program.opts());
|
|
26
|
+
// 执行函数
|
|
27
|
+
function run() {
|
|
28
|
+
const mockMethods = []; // 收集 Mock 数据
|
|
29
|
+
const dirPath = path_1.default.join(_path, options.dir);
|
|
30
|
+
const jsonFiles = (0, js_utils_plus_1.listFilesRecursively)(dirPath, 'json', options.excludes || []);
|
|
31
|
+
console.log(new Array(80).join('-'));
|
|
32
|
+
jsonFiles.forEach(function (item) {
|
|
33
|
+
const json = (0, js_utils_plus_1.readFileSync)(item, true);
|
|
34
|
+
if (!json) {
|
|
35
|
+
console.error(`- 执行文件:${item}`);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
console.log(`- 执行文件:${item}`);
|
|
39
|
+
const parsedPath = path_1.default.parse(item);
|
|
40
|
+
let className = json['__className'] || parsedPath.name;
|
|
41
|
+
if (json['__className'])
|
|
42
|
+
delete json['__className'];
|
|
43
|
+
if (options.mock)
|
|
44
|
+
mockMethods.push({ name: className, json: json }); // 收集 Mock 数据
|
|
45
|
+
const { context } = (0, json2dart_1.jsonToDart)(json, className, options.suffix, options.nullsafety, options.numparse);
|
|
46
|
+
const outputPath = path_1.default.join(_path, options.output, `${parsedPath.name}.dart`);
|
|
47
|
+
(0, js_utils_plus_1.writeFileSync)(outputPath, context);
|
|
48
|
+
});
|
|
49
|
+
// 生成 mock.dart
|
|
50
|
+
if (options.mock && mockMethods.length > 0) {
|
|
51
|
+
const mockContent = (0, mock_generator_1.generateMockDart)(mockMethods, options.suffix);
|
|
52
|
+
const mockOutputPath = path_1.default.join(_path, options.output, 'mock.dart');
|
|
53
|
+
(0, js_utils_plus_1.writeFileSync)(mockOutputPath, mockContent);
|
|
54
|
+
console.log(`- Mock 文件已生成:${mockOutputPath}`);
|
|
55
|
+
}
|
|
56
|
+
else if (!options.mock) {
|
|
57
|
+
// 当 --mock 为 false 时,删除已存在的 mock.dart
|
|
58
|
+
const mockOutputPath = path_1.default.join(_path, options.output, 'mock.dart');
|
|
59
|
+
if (fs_1.default.existsSync(mockOutputPath)) {
|
|
60
|
+
fs_1.default.unlinkSync(mockOutputPath);
|
|
61
|
+
console.log(`- Mock 文件已删除:${mockOutputPath}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
run();
|