diff --git a/build/cool/lib/eps/config.ts b/build/cool/lib/eps/config.ts new file mode 100644 index 0000000..a50f974 --- /dev/null +++ b/build/cool/lib/eps/config.ts @@ -0,0 +1,36 @@ +export default { + // 实体生成 + entity: { + // 是否生成 + enable: true, + mapping: [ + { + // 自定义匹配 + custom: ({ entityName, propertyName, type }) => { + // status原本是tinyint,如果是1的话,== true是可以的,但是不能 === true,请谨慎使用 + if (propertyName === "status" && type == "tinyint") return "boolean"; + //如果没有,返回null或者不返回,则继续遍历其他匹配规则 + return null; + } + }, + { + // 返回类型 + type: "string", + // 匹配列类型 + includes: ["varchar", "text"] + }, + { + type: "Date", + includes: ["datetime", "date"] + }, + { + type: "number", + includes: ["tinyint", "int", "decimal"] + }, + { + type: "BigInt", + includes: ["bigint"] + } + ] + } +}; diff --git a/build/cool/lib/eps/index.ts b/build/cool/lib/eps/index.ts index 9d995f9..6a71d03 100644 --- a/build/cool/lib/eps/index.ts +++ b/build/cool/lib/eps/index.ts @@ -3,6 +3,8 @@ import { isEmpty, last } from "lodash"; import { createDir, firstUpperCase, readFile, toCamel } from "../../utils"; import { createWriteStream } from "fs"; import { join } from "path"; +// import * as config from "/@/cool/config"; +import config from "./config"; // 临时目录路径 const tempPath = join(__dirname, "../../temp"); @@ -211,9 +213,70 @@ export async function createEps({ list, service }: any) { }) ) ); + + if (config.entity.enable) createEntity(list); } // 获取描述 export function getEps() { return JSON.stringify(readFile(join(tempPath, "eps.json"))); } + +function getType({ entityName, propertyName, type }) { + for (const map of config.entity.mapping) { + if (map.custom) { + const resType = map.custom({ entityName, propertyName, type }); + if (resType) return resType; + } + if (map.includes?.includes(type)) return map.type; + } + return type; +} + +// 创建Entity描述文件 +export function createEntity(list: any[]) { + const t2: any[] = []; + + for (const item of list) { + if (!item.name) continue; + const t = [`declare interface ${item.name} {`]; + for (const col of item.columns) { + // 描述 + t.push("\n"); + t.push("/**\n"); + t.push(` * ${col.comment}\n`); + t.push(" */\n"); + t.push( + `${col.propertyName}?: ${getType({ + entityName: item.name, + propertyName: col.propertyName, + type: col.type + })};` + ); + } + t.push("\n"); + t.push("/**\n"); + t.push(` * 任意键值\n`); + t.push(" */\n"); + t.push(`[key: string]: any;`); + t.push("}"); + t2.push(t); + } + + // 文本内容 + const content = prettier.format(t2.map((e) => e.join("")).join("\n\n"), { + parser: "typescript", + useTabs: true, + tabWidth: 4, + endOfLine: "lf", + semi: true, + singleQuote: false, + printWidth: 100, + trailingComma: "none" + }); + + // 创建 entity 描述文件 + createWriteStream(join(tempPath, "entity.d.ts"), { + flags: "w" + }).write(content); +} diff --git a/build/svg/index.ts b/build/svg/index.ts index 9640753..835ce56 100644 --- a/build/svg/index.ts +++ b/build/svg/index.ts @@ -1,5 +1,7 @@ import { Plugin } from "vite"; -import { readFileSync, readdirSync } from "fs"; +import { readFileSync, readdirSync, accessSync } from "fs"; +import path from "path"; +import { isArray } from "lodash"; let idPerfix = ""; const svgTitle = /+].*?)>/; @@ -9,16 +11,19 @@ const hasViewBox = /(viewBox="[^>+].*?")/g; const clearReturn = /(\r)|(\n)/g; -function findSvgFile(dir: string): string[] { +function findSvgFile(dir: string, uniqueNames: Record): string[] { const svgRes = []; const dirents = readdirSync(dir, { withFileTypes: true }); for (const dirent of dirents) { if (dirent.isDirectory()) { - svgRes.push(...findSvgFile(dir + dirent.name + "/")); + svgRes.push(...findSvgFile(path.join(dir, dirent.name), uniqueNames)); + } else if (uniqueNames[dirent.name]) { + continue; } else { - const svg = readFileSync(dir + dirent.name) + uniqueNames[dirent.name] = true; + const svg = readFileSync(path.join(dir, dirent.name)) .toString() .replace(clearReturn, "") .replace(svgTitle, (_: any, $2: any) => { @@ -47,10 +52,16 @@ function findSvgFile(dir: string): string[] { return svgRes; } -export const svgBuilder = (path: string, perfix = "icon"): Plugin | null => { - if (path !== "") { +export const svgBuilder = (paths: string | string[], perfix = "icon"): Plugin | null => { + if (paths) { idPerfix = perfix; - const res = findSvgFile(path); + paths = isArray(paths) ? paths : [paths]; + const uniqueNames: Record = {}; + const res = paths.reduce( + (previousValue, currentValue) => + previousValue.concat(findSvgFile(currentValue, uniqueNames)), + [] + ); return { name: "svg-transform", transformIndexHtml(html): string { @@ -69,3 +80,27 @@ export const svgBuilder = (path: string, perfix = "icon"): Plugin | null => { return null; } }; + +export const findSvgFolders = (dir: string): string[] => { + const svgFolders = []; + const dirents = readdirSync(dir, { + withFileTypes: true + }); + + // 找到结构为icons/svg的文件夹 + for (const dirent of dirents) { + if (dirent.isDirectory()) { + const testPath = + dirent.name === "icons" + ? path.join(dir, "icons/svg") + : path.join(dir, dirent.name, "icons/svg"); + try { + accessSync(testPath); + svgFolders.push(testPath); + } catch (e) { + continue; + } + } + } + return svgFolders; +}; diff --git a/src/env.d.ts b/src/env.d.ts index 191a0fe..db16053 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -1,4 +1,5 @@ /// /// +/// declare const __EPS__: string; diff --git a/vite.config.ts b/vite.config.ts index 6751274..b6be7eb 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -8,7 +8,7 @@ import Unocss from "unocss/vite"; import { presetUno } from "unocss"; import { proxy } from "./src/cool/config/proxy"; import { cool } from "./build/cool"; -import { svgBuilder } from "./build/svg"; +import { svgBuilder, findSvgFolders } from "./build/svg"; function resolve(dir: string) { return path.resolve(__dirname, ".", dir); @@ -27,7 +27,7 @@ export default (): UserConfig => { Unocss({ presets: [presetUno()] }), - svgBuilder("./src/icons/svg/"), + svgBuilder(["./src/icons/svg/",...findSvgFolders("./src/modules/")]), cool() ], css: {