cool-admin-vue/build/cool/lib/eps/index.ts

266 lines
5.3 KiB
TypeScript
Raw Normal View History

2022-04-02 22:50:51 +08:00
import prettier from "prettier";
import { isEmpty, last } from "lodash";
import { createDir, firstUpperCase, readFile, toCamel } from "../../utils";
import { createWriteStream } from "fs";
import { join } from "path";
import config from "./config";
2022-04-02 22:50:51 +08:00
// 临时目录路径
const tempPath = join(__dirname, "../../temp");
2022-07-21 19:07:32 +08:00
// 获取类型
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;
}
2022-04-02 22:50:51 +08:00
2022-07-21 19:07:32 +08:00
// 创建 Entity
function createEntity({ list }: any) {
const t0: any[] = [];
2022-04-02 22:50:51 +08:00
2022-07-21 19:07:32 +08:00
for (const item of list) {
if (!item.name) continue;
const t = [`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("}");
t0.push(t);
}
return t0.map((e) => e.join("")).join("\n\n");
}
// 创建 Service
function createService({ list, service }: any) {
const t0: any[] = [];
const t1 = [
`type Service = {
request(options: {
url: string;
method?: 'POST' | 'GET' | string;
data?: any;
params?: any;
proxy?: boolean;
[key: string]: any;
}): Promise<any>;
`
];
2022-04-02 22:50:51 +08:00
// 处理数据
function deep(d: any, k?: string) {
if (!k) k = "";
for (const i in d) {
const name = k + toCamel(firstUpperCase(i.replace(/[:]/g, "")));
if (d[i].namespace) {
// 查找配置
const item = list.find((e: any) => (e.prefix || "").includes(d[i].namespace));
if (item) {
2022-07-21 19:07:32 +08:00
const t = [`interface ${name} {`];
2022-04-02 22:50:51 +08:00
t1.push(`${i}: ${name};`);
// 插入方法
if (item.api) {
// 权限列表
const permission: string[] = [];
item.api.forEach((a: any) => {
// 方法名
const n = toCamel(a.name || last(a.path.split("/"))).replace(
/[:\/-]/g,
""
);
if (n) {
// 参数类型
let q: string[] = [];
// 参数列表
const { parameters = [] } = a.dts || {};
parameters.forEach((p: any) => {
if (p.description) {
q.push(`\n/** ${p.description} */\n`);
}
if (p.name.includes(":")) {
return false;
}
const a = `${p.name}${p.required ? "" : "?"}`;
const b = `${p.schema.type || "string"}`;
q.push(`${a}: ${b},`);
});
if (isEmpty(q)) {
q = ["any"];
} else {
q.unshift("{");
q.push("}");
}
// 返回类型
let res = "";
2022-07-21 19:07:32 +08:00
// 实体名
const en = item.name || "any";
2022-04-02 22:50:51 +08:00
switch (a.path) {
case "/page":
2022-07-21 19:07:32 +08:00
res = `
{
pagination: { size: number; page: number; total: number };
list: ${en} [];
[key: string]: any;
}
`;
break;
case "/list":
res = `${en} []`;
2022-04-02 22:50:51 +08:00
break;
2022-07-21 19:07:32 +08:00
case "/info":
res = en;
break;
2022-04-02 22:50:51 +08:00
default:
res = "any";
break;
}
// 描述
t.push("\n");
t.push("/**\n");
t.push(` * ${a.summary || n}\n`);
t.push(" */\n");
t.push(
`${n}(data${q.length == 1 ? "?" : ""}: ${q.join(
""
)}): Promise<${res}>;`
);
}
2022-07-21 19:07:32 +08:00
permission.push(n);
2022-04-02 22:50:51 +08:00
});
2022-07-21 19:07:32 +08:00
// 权限标识
2022-04-02 22:50:51 +08:00
t.push("\n");
t.push("/**\n");
2022-07-21 19:07:32 +08:00
t.push(" * 权限标识\n");
2022-04-02 22:50:51 +08:00
t.push(" */\n");
2022-07-21 19:07:32 +08:00
t.push(
`permission: { ${permission.map((e) => `${e}: string;`).join("\n")} };`
);
// 权限状态
t.push("\n");
t.push("/**\n");
t.push(" * 权限状态\n");
t.push(" */\n");
t.push(
`_permission: { ${permission
.map((e) => `${e}: boolean;`)
.join("\n")} };`
);
2022-04-02 22:50:51 +08:00
}
t.push("}");
t0.push(t);
}
} else {
t1.push(`${i}: {`);
deep(d[i], name);
t1.push(`},`);
}
}
}
2022-07-21 19:07:32 +08:00
// 深度
2022-04-02 22:50:51 +08:00
deep(service);
2022-07-21 19:07:32 +08:00
// 结束
2022-04-02 22:50:51 +08:00
t1.push("}");
// 追加
t0.push(t1);
2022-07-21 19:07:32 +08:00
return t0.map((e) => e.join("")).join("\n\n");
}
// 创建描述文件
export async function createEps({ list, service }: any) {
// 文件内容
const text = `
declare namespace Eps {
${createEntity({ list })}
${createService({ list, service })}
}
`;
2022-04-02 22:50:51 +08:00
// 文本内容
2022-07-21 19:07:32 +08:00
const content = prettier.format(text, {
2022-04-02 22:50:51 +08:00
parser: "typescript",
useTabs: true,
tabWidth: 4,
endOfLine: "lf",
semi: true,
singleQuote: false,
printWidth: 100,
trailingComma: "none"
});
// 创建 temp 目录
createDir(tempPath);
2022-07-21 19:07:32 +08:00
// 创建 eps 描述文件
createWriteStream(join(tempPath, "eps.d.ts"), {
2022-04-02 22:50:51 +08:00
flags: "w"
}).write(content);
2022-07-21 19:07:32 +08:00
// 创建 eps 数据文件
2022-04-02 22:50:51 +08:00
createWriteStream(join(tempPath, "eps.json"), {
flags: "w"
}).write(
JSON.stringify(
list.map((e: any) => {
return [e.prefix, e.api.map((a: any) => [a.method || "", a.path, a.name || ""])];
})
)
);
}
// 获取描述
export function getEps() {
return JSON.stringify(readFile(join(tempPath, "eps.json")));
}