dzkcc-mflow 0.0.25 → 0.0.26
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 +2 -3
- package/dist/mflow-tools.zip +0 -0
- package/package.json +3 -5
- package/scripts/generate-type-map.js +0 -300
package/README.md
CHANGED
|
@@ -182,10 +182,9 @@ userModel.name; // ✅ 有完整的代码补全
|
|
|
182
182
|
|
|
183
183
|
**维护类型映射**:
|
|
184
184
|
|
|
185
|
-
|
|
185
|
+
框架提供了自动类型生成工具,在 Cocos Creator 编辑器中使用:
|
|
186
186
|
|
|
187
|
-
|
|
188
|
-
2. **命令行方式**:`node node_modules/dzkcc-mflow/scripts/generate-type-map.js`
|
|
187
|
+
**编辑器菜单**:**mflow-tools -> generate-types**
|
|
189
188
|
|
|
190
189
|
📖 **查看文档**: [类型自动推断](./docs/TYPE_INFERENCE.md) | [类型生成工具](./docs/TYPE_GENERATION.md)
|
|
191
190
|
|
package/dist/mflow-tools.zip
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dzkcc-mflow",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.26",
|
|
4
4
|
"description": "A modular design and process management framework developed for the cocos engine, suitable for decoupling and dependency injection.",
|
|
5
5
|
"author": "duanzhk",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,15 +23,13 @@
|
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
25
|
"dist",
|
|
26
|
-
"scripts/postinstall.js"
|
|
27
|
-
"scripts/generate-type-map.js"
|
|
26
|
+
"scripts/postinstall.js"
|
|
28
27
|
],
|
|
29
28
|
"scripts": {
|
|
30
29
|
"watch": "tsc -w",
|
|
31
30
|
"build": "rm -rf dist && rollup -c && node ./scripts/build-tools.js",
|
|
32
31
|
"pub-beta": "npm version patch && npm publish --tag beta",
|
|
33
|
-
"postinstall": "node ./scripts/postinstall.js"
|
|
34
|
-
"generate:types": "node ./scripts/generate-type-map.js"
|
|
32
|
+
"postinstall": "node ./scripts/postinstall.js"
|
|
35
33
|
},
|
|
36
34
|
"devDependencies": {
|
|
37
35
|
"@cocos/creator-types": "^3.8.3",
|
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 自动生成类型映射文件
|
|
3
|
-
* 使用方法:
|
|
4
|
-
* 1. 在框架开发中: node scripts/generate-type-map.js
|
|
5
|
-
* 2. 在用户项目中: node node_modules/dzkcc-mflow/scripts/generate-type-map.js
|
|
6
|
-
* 3. 在用户项目中(推荐): npx dzkcc-mflow-typegen
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const fs = require('fs');
|
|
10
|
-
const path = require('path');
|
|
11
|
-
|
|
12
|
-
// 从配置文件加载配置
|
|
13
|
-
function loadConfig(projectPath) {
|
|
14
|
-
// 尝试从 package.json 读取配置
|
|
15
|
-
const packageJsonPath = path.join(projectPath, 'package.json');
|
|
16
|
-
if (fs.existsSync(packageJsonPath)) {
|
|
17
|
-
try {
|
|
18
|
-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
|
|
19
|
-
if (packageJson.mflowTypeGen) {
|
|
20
|
-
const config = packageJson.mflowTypeGen;
|
|
21
|
-
return {
|
|
22
|
-
modelDir: path.resolve(projectPath, config.modelDir || 'assets/src/models'),
|
|
23
|
-
managerDir: path.resolve(projectPath, config.managerDir || 'assets/src/managers'),
|
|
24
|
-
outputFile: path.resolve(projectPath, config.outputFile || 'assets/types/core-types.d.ts'),
|
|
25
|
-
moduleImportPath: config.moduleImportPath || 'dzkcc-mflow/core'
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
} catch (error) {
|
|
29
|
-
console.warn('⚠️ 无法读取 package.json 配置');
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 尝试从单独的配置文件读取
|
|
34
|
-
const configPath = path.join(projectPath, 'mflow.config.json');
|
|
35
|
-
if (fs.existsSync(configPath)) {
|
|
36
|
-
try {
|
|
37
|
-
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
38
|
-
return {
|
|
39
|
-
modelDir: path.resolve(projectPath, config.modelDir || 'assets/src/models'),
|
|
40
|
-
managerDir: path.resolve(projectPath, config.managerDir || 'assets/src/managers'),
|
|
41
|
-
outputFile: path.resolve(projectPath, config.outputFile || 'assets/types/core-types.d.ts'),
|
|
42
|
-
moduleImportPath: config.moduleImportPath || 'dzkcc-mflow/core'
|
|
43
|
-
};
|
|
44
|
-
} catch (error) {
|
|
45
|
-
console.warn('⚠️ 无法读取 mflow.config.json 配置');
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// 检测是否在框架开发目录中
|
|
50
|
-
const srcModelsPath = path.join(projectPath, 'src/models');
|
|
51
|
-
const srcManagersPath = path.join(projectPath, 'src/managers');
|
|
52
|
-
if (fs.existsSync(srcModelsPath) || fs.existsSync(srcManagersPath)) {
|
|
53
|
-
// 框架开发模式
|
|
54
|
-
return {
|
|
55
|
-
modelDir: srcModelsPath,
|
|
56
|
-
managerDir: srcManagersPath,
|
|
57
|
-
outputFile: path.join(projectPath, 'types/core-types.d.ts'),
|
|
58
|
-
moduleImportPath: 'dzkcc-mflow/core'
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// 使用默认配置(用户项目)
|
|
63
|
-
return {
|
|
64
|
-
modelDir: path.resolve(projectPath, 'assets/src/models'),
|
|
65
|
-
managerDir: path.resolve(projectPath, 'assets/src/managers'),
|
|
66
|
-
outputFile: path.resolve(projectPath, 'assets/types/core-types.d.ts'),
|
|
67
|
-
moduleImportPath: 'dzkcc-mflow/core'
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 确定项目路径
|
|
72
|
-
const projectPath = process.cwd();
|
|
73
|
-
console.log(`📁 项目路径: ${projectPath}\n`);
|
|
74
|
-
|
|
75
|
-
// 配置
|
|
76
|
-
const config = loadConfig(projectPath);
|
|
77
|
-
|
|
78
|
-
// 扫描目录获取所有 .ts 文件
|
|
79
|
-
function scanDirectory(dir) {
|
|
80
|
-
if (!fs.existsSync(dir)) {
|
|
81
|
-
console.warn(`⚠️ 目录不存在: ${dir}`);
|
|
82
|
-
return [];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const files = [];
|
|
86
|
-
const items = fs.readdirSync(dir);
|
|
87
|
-
|
|
88
|
-
for (const item of items) {
|
|
89
|
-
const fullPath = path.join(dir, item);
|
|
90
|
-
const stat = fs.statSync(fullPath);
|
|
91
|
-
|
|
92
|
-
if (stat.isDirectory()) {
|
|
93
|
-
files.push(...scanDirectory(fullPath));
|
|
94
|
-
} else if (item.endsWith('.ts') && !item.endsWith('.d.ts')) {
|
|
95
|
-
files.push(fullPath);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return files;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// 解析文件获取装饰器信息
|
|
103
|
-
function parseFile(filePath) {
|
|
104
|
-
const content = fs.readFileSync(filePath, 'utf-8');
|
|
105
|
-
const fileName = path.basename(filePath, '.ts');
|
|
106
|
-
|
|
107
|
-
// 匹配 @model('Name') 或 @model()
|
|
108
|
-
const modelMatch = content.match(/@model\s*\(\s*['"](\w+)['"]\s*\)/);
|
|
109
|
-
if (modelMatch) {
|
|
110
|
-
return {
|
|
111
|
-
type: 'model',
|
|
112
|
-
decoratorName: modelMatch[1],
|
|
113
|
-
className: fileName,
|
|
114
|
-
filePath: filePath
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// 匹配 @manager('Name') 或 @manager()
|
|
119
|
-
const managerMatch = content.match(/@manager\s*\(\s*['"](\w+)['"]\s*\)/);
|
|
120
|
-
if (managerMatch) {
|
|
121
|
-
return {
|
|
122
|
-
type: 'manager',
|
|
123
|
-
decoratorName: managerMatch[1],
|
|
124
|
-
className: fileName,
|
|
125
|
-
filePath: filePath
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// 如果没有指定名称,使用类名
|
|
130
|
-
if (content.includes('@model()')) {
|
|
131
|
-
return {
|
|
132
|
-
type: 'model',
|
|
133
|
-
decoratorName: fileName,
|
|
134
|
-
className: fileName,
|
|
135
|
-
filePath: filePath
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (content.includes('@manager()')) {
|
|
140
|
-
return {
|
|
141
|
-
type: 'manager',
|
|
142
|
-
decoratorName: fileName,
|
|
143
|
-
className: fileName,
|
|
144
|
-
filePath: filePath
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return null;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// 生成类型映射代码
|
|
152
|
-
function generateTypeMap(models, managers, outputFile) {
|
|
153
|
-
const lines = [];
|
|
154
|
-
|
|
155
|
-
// 文件头注释
|
|
156
|
-
lines.push('/**');
|
|
157
|
-
lines.push(' * 自动生成的类型映射文件');
|
|
158
|
-
lines.push(' * ⚠️ 请勿手动修改此文件!');
|
|
159
|
-
lines.push(' * 重新生成方法:');
|
|
160
|
-
lines.push(' * - 在 Cocos Creator 编辑器中:项目菜单 -> 生成类型映射');
|
|
161
|
-
lines.push(' * - 命令行:node node_modules/dzkcc-mflow/scripts/generate-type-map.js');
|
|
162
|
-
lines.push(' */');
|
|
163
|
-
lines.push('');
|
|
164
|
-
|
|
165
|
-
// 导入 Model
|
|
166
|
-
if (models.length > 0) {
|
|
167
|
-
lines.push('// Model 导入');
|
|
168
|
-
for (const model of models) {
|
|
169
|
-
const relativePath = path.relative(
|
|
170
|
-
path.dirname(outputFile),
|
|
171
|
-
model.filePath
|
|
172
|
-
).replace(/\\/g, '/').replace('.ts', '');
|
|
173
|
-
lines.push(`import { ${model.className} } from '${relativePath}';`);
|
|
174
|
-
}
|
|
175
|
-
lines.push('');
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// 导入 Manager
|
|
179
|
-
if (managers.length > 0) {
|
|
180
|
-
lines.push('// Manager 导入');
|
|
181
|
-
for (const manager of managers) {
|
|
182
|
-
const relativePath = path.relative(
|
|
183
|
-
path.dirname(outputFile),
|
|
184
|
-
manager.filePath
|
|
185
|
-
).replace(/\\/g, '/').replace('.ts', '');
|
|
186
|
-
lines.push(`import { ${manager.className} } from '${relativePath}';`);
|
|
187
|
-
}
|
|
188
|
-
lines.push('');
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// 导入 Names
|
|
192
|
-
lines.push('// Names 导入');
|
|
193
|
-
lines.push(`import { ModelNames, ManagerNames } from '${config.moduleImportPath}';`);
|
|
194
|
-
lines.push('');
|
|
195
|
-
|
|
196
|
-
// 声明模块
|
|
197
|
-
lines.push(`declare module '${config.moduleImportPath}' {`);
|
|
198
|
-
|
|
199
|
-
// Model 类型映射
|
|
200
|
-
if (models.length > 0) {
|
|
201
|
-
lines.push(' interface ModelTypeMap {');
|
|
202
|
-
for (const model of models) {
|
|
203
|
-
lines.push(` [ModelNames.${model.decoratorName}]: ${model.className};`);
|
|
204
|
-
}
|
|
205
|
-
lines.push(' }');
|
|
206
|
-
lines.push('');
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// Manager 类型映射
|
|
210
|
-
if (managers.length > 0) {
|
|
211
|
-
lines.push(' interface ManagerTypeMap {');
|
|
212
|
-
for (const manager of managers) {
|
|
213
|
-
lines.push(` [ManagerNames.${manager.decoratorName}]: ${manager.className};`);
|
|
214
|
-
}
|
|
215
|
-
lines.push(' }');
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
lines.push('}');
|
|
219
|
-
lines.push('');
|
|
220
|
-
|
|
221
|
-
return lines.join('\n');
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// 主函数
|
|
225
|
-
function main() {
|
|
226
|
-
console.log('🚀 开始生成类型映射文件...\n');
|
|
227
|
-
|
|
228
|
-
// 输出配置信息
|
|
229
|
-
console.log('⚙️ 使用配置:');
|
|
230
|
-
console.log(` Model 目录: ${config.modelDir}`);
|
|
231
|
-
console.log(` Manager 目录: ${config.managerDir}`);
|
|
232
|
-
console.log(` 输出文件: ${config.outputFile}`);
|
|
233
|
-
console.log(` 模块路径: ${config.moduleImportPath}\n`);
|
|
234
|
-
|
|
235
|
-
// 扫描 Model 目录
|
|
236
|
-
console.log(`📂 扫描 Model 目录: ${config.modelDir}`);
|
|
237
|
-
const modelFiles = scanDirectory(config.modelDir);
|
|
238
|
-
const models = modelFiles
|
|
239
|
-
.map(parseFile)
|
|
240
|
-
.filter(item => item && item.type === 'model');
|
|
241
|
-
console.log(` 找到 ${models.length} 个 Model\n`);
|
|
242
|
-
|
|
243
|
-
// 扫描 Manager 目录
|
|
244
|
-
console.log(`📂 扫描 Manager 目录: ${config.managerDir}`);
|
|
245
|
-
const managerFiles = scanDirectory(config.managerDir);
|
|
246
|
-
const managers = managerFiles
|
|
247
|
-
.map(parseFile)
|
|
248
|
-
.filter(item => item && item.type === 'manager');
|
|
249
|
-
console.log(` 找到 ${managers.length} 个 Manager\n`);
|
|
250
|
-
|
|
251
|
-
if (models.length === 0 && managers.length === 0) {
|
|
252
|
-
console.log('⚠️ 未找到任何 Model 或 Manager,跳过生成');
|
|
253
|
-
console.log('\n💡 提示:');
|
|
254
|
-
console.log(' 1. 确保在 Model/Manager 类上使用了 @model() 或 @manager() 装饰器');
|
|
255
|
-
console.log(' 2. 确保目录路径配置正确');
|
|
256
|
-
console.log(' 3. 可以在 package.json 中添加 mflowTypeGen 配置或创建 mflow.config.json 文件');
|
|
257
|
-
console.log('\n配置示例 (package.json):');
|
|
258
|
-
console.log(' "mflowTypeGen": {');
|
|
259
|
-
console.log(' "modelDir": "assets/src/models",');
|
|
260
|
-
console.log(' "managerDir": "assets/src/managers",');
|
|
261
|
-
console.log(' "outputFile": "assets/types/core-types.d.ts",');
|
|
262
|
-
console.log(' "moduleImportPath": "dzkcc-mflow/core"');
|
|
263
|
-
console.log(' }');
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// 生成类型映射
|
|
268
|
-
const content = generateTypeMap(models, managers, config.outputFile);
|
|
269
|
-
|
|
270
|
-
// 确保输出目录存在
|
|
271
|
-
const outputDir = path.dirname(config.outputFile);
|
|
272
|
-
if (!fs.existsSync(outputDir)) {
|
|
273
|
-
fs.mkdirSync(outputDir, { recursive: true });
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
// 写入文件
|
|
277
|
-
fs.writeFileSync(config.outputFile, content, 'utf-8');
|
|
278
|
-
|
|
279
|
-
console.log(`✅ 类型映射文件已生成: ${config.outputFile}\n`);
|
|
280
|
-
console.log('📋 生成的映射:');
|
|
281
|
-
if (models.length > 0) {
|
|
282
|
-
console.log(' Models:');
|
|
283
|
-
models.forEach(m => console.log(` - ${m.decoratorName} → ${m.className}`));
|
|
284
|
-
}
|
|
285
|
-
if (managers.length > 0) {
|
|
286
|
-
console.log(' Managers:');
|
|
287
|
-
managers.forEach(m => console.log(` - ${m.decoratorName} → ${m.className}`));
|
|
288
|
-
}
|
|
289
|
-
console.log('');
|
|
290
|
-
console.log('🎉 完成!');
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// 执行
|
|
294
|
-
try {
|
|
295
|
-
main();
|
|
296
|
-
} catch (error) {
|
|
297
|
-
console.error('❌ 生成失败:', error.message);
|
|
298
|
-
process.exit(1);
|
|
299
|
-
}
|
|
300
|
-
|