From ca823845ab7d430426cb63a43f71868561e6532a Mon Sep 17 00:00:00 2001 From: icssoa <615206459@qq.com> Date: Fri, 16 May 2025 18:34:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BC=82=E5=B8=B8=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=AF=BC=E8=87=B4=20eps.d.ts=20=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- packages/vite-plugin/dist/index.js | 27 +++++++++++--- packages/vite-plugin/package.json | 2 +- packages/vite-plugin/src/eps/index.ts | 51 ++++++++++++++++++--------- pnpm-lock.yaml | 37 +++++++++++++------ 5 files changed, 86 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index d73bd15..766376b 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "xlsx": "^0.18.5" }, "devDependencies": { - "@cool-vue/vite-plugin": "^8.1.1", + "@cool-vue/vite-plugin": "^8.1.2", "@intlify/unplugin-vue-i18n": "^6.0.3", "@rushstack/eslint-patch": "^1.10.5", "@tsconfig/node20": "^20.1.4", diff --git a/packages/vite-plugin/dist/index.js b/packages/vite-plugin/dist/index.js index 8ef90d2..94b45cf 100644 --- a/packages/vite-plugin/dist/index.js +++ b/packages/vite-plugin/dist/index.js @@ -161,7 +161,8 @@ } // 格式化代码 async function formatCode(text) { - return prettier.format(text, { + return prettier + .format(text, { parser: "typescript", useTabs: true, tabWidth: 4, @@ -170,6 +171,11 @@ singleQuote: false, printWidth: 100, trailingComma: "none", + }) + .catch((err) => { + console.log(err); + error(`[cool-eps] Failed to format /build/cool/eps.d.ts. Please delete the file and try again`); + return null; }); } // 获取数据 @@ -265,12 +271,16 @@ function formatName(name) { return (name || "").replace(/[:,\s,\/,-]/g, ""); } + // 检查方法名,包含特殊字符则忽略 + function checkName(name) { + return name && !["{", "}", ":"].some((e) => name.includes(e)); + } // 创建 Entity function createEntity() { const ignore = []; let t0 = ""; for (const item of list) { - if (!item.name) + if (!checkName(item.name)) continue; let t = `interface ${formatName(item.name)} {`; // 合并多个列 @@ -319,6 +329,9 @@ k = ""; for (const i in d) { const name = k + toCamel(firstUpperCase(formatName(i))); + // 检查方法名 + if (!checkName(name)) + continue; if (d[i].namespace) { // 查找配置 const item = list.find((e) => (e.prefix || "") === `/${d[i].namespace}`); @@ -330,7 +343,10 @@ const permission = []; item.api.forEach((a) => { // 方法名 - const n = toCamel(formatName(a.name || lodash.last(a.path.split("/")) || "")); + const n = toCamel(formatName(a.name || lodash.last(a.path.split("/")))); + // 检查方法名 + if (!checkName(n)) + return; if (n) { // 参数类型 let q = []; @@ -340,7 +356,8 @@ if (p.description) { q.push(`\n/** ${p.description} */\n`); } - if (p.name.includes(":")) { + // 检查参数名 + if (!checkName(p.name)) { return false; } const a = `${p.name}${p.required ? "" : "?"}`; @@ -462,7 +479,7 @@ const content = await formatCode(text); const local_content = readFile(getEpsPath("eps.d.ts")); // 是否需要更新 - if (content != local_content) { + if (content && content != local_content) { // 创建 eps 描述文件 fs.createWriteStream(getEpsPath("eps.d.ts"), { flags: "w", diff --git a/packages/vite-plugin/package.json b/packages/vite-plugin/package.json index 7b1266f..5e43883 100644 --- a/packages/vite-plugin/package.json +++ b/packages/vite-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@cool-vue/vite-plugin", - "version": "8.1.1", + "version": "8.1.2", "description": "cool-admin、cool-uni builder", "types": "./dist/index.d.ts", "main": "/dist/index.js", diff --git a/packages/vite-plugin/src/eps/index.ts b/packages/vite-plugin/src/eps/index.ts index 99e13e3..2eac4e4 100644 --- a/packages/vite-plugin/src/eps/index.ts +++ b/packages/vite-plugin/src/eps/index.ts @@ -54,16 +54,25 @@ function findColumns(sources: string[], item: Eps.Entity) { // 格式化代码 async function formatCode(text: string) { - return prettier.format(text, { - parser: "typescript", - useTabs: true, - tabWidth: 4, - endOfLine: "lf", - semi: true, - singleQuote: false, - printWidth: 100, - trailingComma: "none", - }); + return prettier + .format(text, { + parser: "typescript", + useTabs: true, + tabWidth: 4, + endOfLine: "lf", + semi: true, + singleQuote: false, + printWidth: 100, + trailingComma: "none", + }) + .catch((err) => { + console.log(err); + error( + `[cool-eps] Failed to format /build/cool/eps.d.ts. Please delete the file and try again`, + ); + + return null; + }); } // 获取数据 @@ -169,6 +178,11 @@ async function createDescribe({ list, service }: { list: Eps.Entity[]; service: return (name || "").replace(/[:,\s,\/,-]/g, ""); } + // 检查方法名,包含特殊字符则忽略 + function checkName(name: string) { + return name && !["{", "}", ":"].some((e) => name.includes(e)); + } + // 创建 Entity function createEntity() { const ignore: string[] = []; @@ -176,7 +190,7 @@ async function createDescribe({ list, service }: { list: Eps.Entity[]; service: let t0 = ""; for (const item of list) { - if (!item.name) continue; + if (!checkName(item.name)) continue; let t = `interface ${formatName(item.name)} {`; @@ -235,6 +249,9 @@ async function createDescribe({ list, service }: { list: Eps.Entity[]; service: for (const i in d) { const name = k + toCamel(firstUpperCase(formatName(i))); + // 检查方法名 + if (!checkName(name)) continue; + if (d[i].namespace) { // 查找配置 const item = list.find((e) => (e.prefix || "") === `/${d[i].namespace}`); @@ -249,9 +266,10 @@ async function createDescribe({ list, service }: { list: Eps.Entity[]; service: item.api.forEach((a) => { // 方法名 - const n = toCamel( - formatName(a.name || last(a.path.split("/")) || ""), - ); + const n = toCamel(formatName(a.name || last(a.path.split("/"))!)); + + // 检查方法名 + if (!checkName(n)) return; if (n) { // 参数类型 @@ -265,7 +283,8 @@ async function createDescribe({ list, service }: { list: Eps.Entity[]; service: q.push(`\n/** ${p.description} */\n`); } - if (p.name.includes(":")) { + // 检查参数名 + if (!checkName(p.name)) { return false; } @@ -407,7 +426,7 @@ async function createDescribe({ list, service }: { list: Eps.Entity[]; service: const local_content = readFile(getEpsPath("eps.d.ts")); // 是否需要更新 - if (content != local_content) { + if (content && content != local_content) { // 创建 eps 描述文件 createWriteStream(getEpsPath("eps.d.ts"), { flags: "w", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 43b452e..3a63d87 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@cool-vue/crud': - specifier: ^8.0.2 - version: 8.0.2(typescript@5.5.4) + specifier: ^8.0.4 + version: 8.0.4(typescript@5.5.4) '@element-plus/icons-vue': specifier: ^2.3.1 version: 2.3.1(vue@3.5.13(typescript@5.5.4)) @@ -82,8 +82,8 @@ importers: version: 0.18.5 devDependencies: '@cool-vue/vite-plugin': - specifier: ^8.1.1 - version: 8.1.1 + specifier: file:packages/vite-plugin + version: file:packages/vite-plugin '@intlify/unplugin-vue-i18n': specifier: ^6.0.3 version: 6.0.8(@vue/compiler-dom@3.5.13)(eslint@9.25.1(jiti@1.21.7))(rollup@4.40.0)(typescript@5.5.4)(vue-i18n@11.1.3(vue@3.5.13(typescript@5.5.4)))(vue@3.5.13(typescript@5.5.4)) @@ -331,11 +331,11 @@ packages: resolution: {integrity: sha512-H45s8fVLYjbhFH62dIJ3WtmJ6RSPt/3DRO0ZcT2SUiYiQyz3BLVb9ADEnLl91m74aQPS3AzzeajZHYOalWe3bg==} engines: {node: '>=6.9.0'} - '@cool-vue/crud@8.0.2': - resolution: {integrity: sha512-7fXGfn1Li0n6vtHSj40mu2VuZV2L/T/HDllJXHMKq2ODo9EAttjcuTN/Z8b3KuOFHYpWp5iN+FqIzRNGEDxjUQ==} + '@cool-vue/crud@8.0.4': + resolution: {integrity: sha512-EBaqZQrJGqcUAsoHi7EL2OIJmE/+Mlofj8T8DIigkBLm6NQ7j8X1V7gpH1Q/yky9cw1NXUeSJrPXcvTlagdUdA==} - '@cool-vue/vite-plugin@8.1.1': - resolution: {integrity: sha512-s4F2NHUZ1Dc7KjEumzzbnOkKPw0Fdb2GwdWmgTpaxwBg9BTpiVA/N+92kqh8mlSmRJjn/tMMaEUInxQzbLQ0TQ==} + '@cool-vue/vite-plugin@file:packages/vite-plugin': + resolution: {directory: packages/vite-plugin, type: directory} '@ctrl/tinycolor@3.6.1': resolution: {integrity: sha512-SITSV6aIXsuVNV3f3O0f2n/cgyEDWoSqtZMYiAmcsYHydcKrOz3gUxB/iXd/Qf08+IZX4KpgNbvUdMBmWz+kcA==} @@ -675,36 +675,42 @@ packages: engines: {node: '>= 10.0.0'} cpu: [arm] os: [linux] + libc: [glibc] '@parcel/watcher-linux-arm-musl@2.5.1': resolution: {integrity: sha512-6E+m/Mm1t1yhB8X412stiKFG3XykmgdIOqhjWj+VL8oHkKABfu/gjFj8DvLrYVHSBNC+/u5PeNrujiSQ1zwd1Q==} engines: {node: '>= 10.0.0'} cpu: [arm] os: [linux] + libc: [musl] '@parcel/watcher-linux-arm64-glibc@2.5.1': resolution: {integrity: sha512-LrGp+f02yU3BN9A+DGuY3v3bmnFUggAITBGriZHUREfNEzZh/GO06FF5u2kx8x+GBEUYfyTGamol4j3m9ANe8w==} engines: {node: '>= 10.0.0'} cpu: [arm64] os: [linux] + libc: [glibc] '@parcel/watcher-linux-arm64-musl@2.5.1': resolution: {integrity: sha512-cFOjABi92pMYRXS7AcQv9/M1YuKRw8SZniCDw0ssQb/noPkRzA+HBDkwmyOJYp5wXcsTrhxO0zq1U11cK9jsFg==} engines: {node: '>= 10.0.0'} cpu: [arm64] os: [linux] + libc: [musl] '@parcel/watcher-linux-x64-glibc@2.5.1': resolution: {integrity: sha512-GcESn8NZySmfwlTsIur+49yDqSny2IhPeZfXunQi48DMugKeZ7uy1FX83pO0X22sHntJ4Ub+9k34XQCX+oHt2A==} engines: {node: '>= 10.0.0'} cpu: [x64] os: [linux] + libc: [glibc] '@parcel/watcher-linux-x64-musl@2.5.1': resolution: {integrity: sha512-n0E2EQbatQ3bXhcH2D1XIAANAcTZkQICBPVaxMeaCVBtOpBZpWJuf7LwyWPSBDITb7In8mqQgJ7gH8CILCURXg==} engines: {node: '>= 10.0.0'} cpu: [x64] os: [linux] + libc: [musl] '@parcel/watcher-win32-arm64@2.5.1': resolution: {integrity: sha512-RFzklRvmc3PkjKjry3hLF9wD7ppR4AKcWNzH7kXR7GUe0Igb3Nz8fyPwtZCSquGrhU5HhUNDr/mKBqj7tqA2Vw==} @@ -782,56 +788,67 @@ packages: resolution: {integrity: sha512-y/qUMOpJxBMy8xCXD++jeu8t7kzjlOCkoxxajL58G62PJGBZVl/Gwpm7JK9+YvlB701rcQTzjUZ1JgUoPTnoQA==} cpu: [arm] os: [linux] + libc: [glibc] '@rollup/rollup-linux-arm-musleabihf@4.40.0': resolution: {integrity: sha512-GoCsPibtVdJFPv/BOIvBKO/XmwZLwaNWdyD8TKlXuqp0veo2sHE+A/vpMQ5iSArRUz/uaoj4h5S6Pn0+PdhRjg==} cpu: [arm] os: [linux] + libc: [musl] '@rollup/rollup-linux-arm64-gnu@4.40.0': resolution: {integrity: sha512-L5ZLphTjjAD9leJzSLI7rr8fNqJMlGDKlazW2tX4IUF9P7R5TMQPElpH82Q7eNIDQnQlAyiNVfRPfP2vM5Avvg==} cpu: [arm64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-arm64-musl@4.40.0': resolution: {integrity: sha512-ATZvCRGCDtv1Y4gpDIXsS+wfFeFuLwVxyUBSLawjgXK2tRE6fnsQEkE4csQQYWlBlsFztRzCnBvWVfcae/1qxQ==} cpu: [arm64] os: [linux] + libc: [musl] '@rollup/rollup-linux-loongarch64-gnu@4.40.0': resolution: {integrity: sha512-wG9e2XtIhd++QugU5MD9i7OnpaVb08ji3P1y/hNbxrQ3sYEelKJOq1UJ5dXczeo6Hj2rfDEL5GdtkMSVLa/AOg==} cpu: [loong64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-powerpc64le-gnu@4.40.0': resolution: {integrity: sha512-vgXfWmj0f3jAUvC7TZSU/m/cOE558ILWDzS7jBhiCAFpY2WEBn5jqgbqvmzlMjtp8KlLcBlXVD2mkTSEQE6Ixw==} cpu: [ppc64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-riscv64-gnu@4.40.0': resolution: {integrity: sha512-uJkYTugqtPZBS3Z136arevt/FsKTF/J9dEMTX/cwR7lsAW4bShzI2R0pJVw+hcBTWF4dxVckYh72Hk3/hWNKvA==} cpu: [riscv64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-riscv64-musl@4.40.0': resolution: {integrity: sha512-rKmSj6EXQRnhSkE22+WvrqOqRtk733x3p5sWpZilhmjnkHkpeCgWsFFo0dGnUGeA+OZjRl3+VYq+HyCOEuwcxQ==} cpu: [riscv64] os: [linux] + libc: [musl] '@rollup/rollup-linux-s390x-gnu@4.40.0': resolution: {integrity: sha512-SpnYlAfKPOoVsQqmTFJ0usx0z84bzGOS9anAC0AZ3rdSo3snecihbhFTlJZ8XMwzqAcodjFU4+/SM311dqE5Sw==} cpu: [s390x] os: [linux] + libc: [glibc] '@rollup/rollup-linux-x64-gnu@4.40.0': resolution: {integrity: sha512-RcDGMtqF9EFN8i2RYN2W+64CdHruJ5rPqrlYw+cgM3uOVPSsnAQps7cpjXe9be/yDp8UC7VLoCoKC8J3Kn2FkQ==} cpu: [x64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-x64-musl@4.40.0': resolution: {integrity: sha512-HZvjpiUmSNx5zFgwtQAV1GaGazT2RWvqeDi0hV+AtC8unqqDSsaFjPxfsO6qPtKRRg25SisACWnJ37Yio8ttaw==} cpu: [x64] os: [linux] + libc: [musl] '@rollup/rollup-win32-arm64-msvc@4.40.0': resolution: {integrity: sha512-UtZQQI5k/b8d7d3i9AZmA/t+Q4tk3hOC0tMOMSq2GlMYOfxbesxG4mJSeDp0EHs30N9bsfwUvs3zF4v/RzOeTQ==} @@ -3063,7 +3080,7 @@ snapshots: '@babel/helper-string-parser': 7.25.9 '@babel/helper-validator-identifier': 7.25.9 - '@cool-vue/crud@8.0.2(typescript@5.5.4)': + '@cool-vue/crud@8.0.4(typescript@5.5.4)': dependencies: '@vue/runtime-core': 3.5.13 element-plus: 2.9.8(vue@3.5.13(typescript@5.5.4)) @@ -3073,7 +3090,7 @@ snapshots: - '@vue/composition-api' - typescript - '@cool-vue/vite-plugin@8.1.1': + '@cool-vue/vite-plugin@file:packages/vite-plugin': dependencies: '@vue/compiler-sfc': 3.5.13 axios: 1.8.4