|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "path"; |
| 3 | +import { ComponentsType } from "./meta.ts"; |
| 4 | +import tf from "type-flat"; |
| 5 | + |
| 6 | +const __dirname = path.dirname(new URL(import.meta.url).pathname); |
| 7 | +const Root_Dir = path.resolve(__dirname, "."); |
| 8 | +// 存放生成的类型 |
| 9 | +const Type_Output_Dir = path.join(Root_Dir, "./types"); |
| 10 | +// 读取echarts的类型定义文件位置 |
| 11 | +const Echarts_Type_Dir = path.join( |
| 12 | + Root_Dir, |
| 13 | + "node_modules/echarts/types/dist/shared.d.ts" |
| 14 | +); |
| 15 | + |
| 16 | +// async function run() { |
| 17 | +// // 清理types目录 |
| 18 | +// if (fs.existsSync(Type_Output_Dir)) { |
| 19 | +// fs.rmSync(Type_Output_Dir, { recursive: true, force: true }); |
| 20 | +// } |
| 21 | +// fs.mkdirSync(Type_Output_Dir); |
| 22 | +// // 初始化 ts-morph 项目 |
| 23 | +// const tsMorph = new Project({ |
| 24 | +// tsConfigFilePath: path.resolve(Root_Dir, "tsconfig.json"), |
| 25 | +// skipFileDependencyResolution: true, |
| 26 | +// }); |
| 27 | +// // 读取所有的echarts类型定义 |
| 28 | +// let sourceFile = tsMorph.addSourceFileAtPath(Echarts_Type_Dir); |
| 29 | + |
| 30 | +// // 按照需要生成的组件类型进行扁平化处理 |
| 31 | +// for (const comp of ComponentsType) { |
| 32 | +// // 生成类型文件 |
| 33 | +// const file_path = path.join(Type_Output_Dir, comp.name); |
| 34 | +// output_file = tsMorph.createSourceFile(file_path + ".ts", ""); |
| 35 | +// visited_types.clear(); |
| 36 | + |
| 37 | +// const type_define = |
| 38 | +// sourceFile.getInterface(comp.name) || |
| 39 | +// sourceFile.getTypeAlias(comp.name) || |
| 40 | +// sourceFile.getEnum(comp.name); |
| 41 | + |
| 42 | +// if (!type_define) { |
| 43 | +// console.log(`❌ 组件类型 ${comp.name} 未找到`); |
| 44 | +// continue; |
| 45 | +// } |
| 46 | +// // 处理类型 |
| 47 | +// const result = flattenTypeDefine(type_define); |
| 48 | +// // 写入文件 |
| 49 | + |
| 50 | +// output_file.addStatements(result); |
| 51 | +// // 格式化代码 |
| 52 | +// output_file.formatText({ |
| 53 | +// ensureNewLineAtEndOfFile: true, |
| 54 | +// indentSize: 2, |
| 55 | +// convertTabsToSpaces: true, |
| 56 | +// }); |
| 57 | + |
| 58 | +// await output_file.saveSync(); |
| 59 | +// } |
| 60 | + |
| 61 | +// console.log(`✅ 类型已扁平化生成到: ${Type_Output_Dir}`); |
| 62 | +// } |
| 63 | + |
| 64 | +async function run() { |
| 65 | + // 清理types目录 |
| 66 | + if (fs.existsSync(Type_Output_Dir)) { |
| 67 | + fs.rmSync(Type_Output_Dir, { recursive: true, force: true }); |
| 68 | + } |
| 69 | + fs.mkdirSync(Type_Output_Dir); |
| 70 | + |
| 71 | + // 读取所有的echarts类型定义 |
| 72 | + let sourceFile = fs.readFileSync(Echarts_Type_Dir, { encoding: "utf-8" }); |
| 73 | + |
| 74 | + // 按照需要生成的组件类型进行扁平化处理 |
| 75 | + for (const comp of ComponentsType) { |
| 76 | + // 生成类型文件 |
| 77 | + const file_path = path.join(Type_Output_Dir, comp.name); |
| 78 | + |
| 79 | + // 处理类型 |
| 80 | + const result = tf.flatten(sourceFile, comp.name); |
| 81 | + // 写入文件 |
| 82 | + fs.writeFileSync(file_path + ".ts", result); |
| 83 | + } |
| 84 | + |
| 85 | + console.log(`✅ 类型已扁平化生成到: ${Type_Output_Dir}`); |
| 86 | +} |
| 87 | + |
| 88 | +run(); |
0 commit comments