/** * 批量添加 TableColumnControl 组件的脚本 * 用于为 stuwork 文件夹下的所有页面添加列显隐控制功能 */ const fs = require('fs') const path = require('path') // 需要处理的目录 const targetDir = path.join(__dirname, '../src/views/stuwork') // 获取所有 index.vue 文件 function getAllIndexFiles(dir) { const files = [] const items = fs.readdirSync(dir, { withFileTypes: true }) for (const item of items) { const fullPath = path.join(dir, item.name) if (item.isDirectory()) { const indexPath = path.join(fullPath, 'index.vue') if (fs.existsSync(indexPath)) { files.push(indexPath) } } } return files } // 检查文件是否已经包含 TableColumnControl function hasTableColumnControl(content) { return content.includes('TableColumnControl') || content.includes('table-column-control') } // 提取表格列配置 function extractTableColumns(content) { const columns = [] const columnRegex = /]+)>/g let match while ((match = columnRegex.exec(content)) !== null) { const attrs = match[1] const propMatch = attrs.match(/prop=["']([^"']+)["']/) const labelMatch = attrs.match(/label=["']([^"']+)["']/) const typeMatch = attrs.match(/type=["']([^"']+)["']/) const fixedMatch = attrs.match(/fixed=["']([^"']+)["']/) if (typeMatch && typeMatch[1] === 'index') { // 序号列,跳过 continue } if (labelMatch && labelMatch[1] === '操作') { // 操作列,标记为 alwaysShow columns.push({ prop: '操作', label: '操作', alwaysShow: true, fixed: fixedMatch ? fixedMatch[1] : false }) continue } if (propMatch && labelMatch) { columns.push({ prop: propMatch[1], label: labelMatch[1], alwaysShow: false, fixed: fixedMatch ? fixedMatch[1] : false }) } } return columns } // 添加 TableColumnControl 导入 function addImport(content) { if (content.includes("import TableColumnControl")) { return content } // 查找 import 语句的位置 const importRegex = /import\s+.*from\s+['"][^'"]+['"]/g const lastImportMatch = [...content.matchAll(importRegex)].pop() if (lastImportMatch) { const insertPos = lastImportMatch.index + lastImportMatch[0].length const newImport = "\nimport TableColumnControl from '/@/components/TableColumnControl/index.vue'" return content.slice(0, insertPos) + newImport + content.slice(insertPos) } return content } // 添加 useRoute 导入 function addUseRouteImport(content) { if (content.includes("import.*useRoute")) { return content } // 查找 vue-router 相关的导入 const routerImportRegex = /import\s+.*from\s+['"]vue-router['"]/ if (routerImportRegex.test(content)) { // 如果已经有 vue-router 导入,添加 useRoute return content.replace( /import\s+([^}]+)\s+from\s+['"]vue-router['"]/, (match, imports) => { if (imports.includes('useRoute')) { return match } return `import { ${imports.trim()}, useRoute } from 'vue-router'` } ) } else { // 如果没有 vue-router 导入,添加新的导入 const importRegex = /import\s+.*from\s+['"]vue['"]/ const vueImportMatch = content.match(importRegex) if (vueImportMatch) { const insertPos = vueImportMatch.index + vueImportMatch[0].length return content.slice(0, insertPos) + "\nimport { useRoute } from 'vue-router'" + content.slice(insertPos) } } return content } // 添加 Menu 图标导入 function addMenuIconImport(content) { if (content.includes('Menu') && content.includes('@element-plus/icons-vue')) { return content } // 查找 @element-plus/icons-vue 导入 const iconImportRegex = /import\s+.*from\s+['"]@element-plus\/icons-vue['"]/ const iconImportMatch = content.match(iconImportRegex) if (iconImportMatch) { // 如果已经有图标导入,添加 Menu return content.replace( /import\s+{([^}]+)}\s+from\s+['"]@element-plus\/icons-vue['"]/, (match, imports) => { if (imports.includes('Menu')) { return match } return `import { ${imports.trim()}, Menu } from '@element-plus/icons-vue'` } ) } else { // 如果没有图标导入,添加新的导入 const importRegex = /import\s+.*from\s+['"][^'"]+['"]/ const lastImportMatch = [...content.matchAll(importRegex)].pop() if (lastImportMatch) { const insertPos = lastImportMatch.index + lastImportMatch[0].length return content.slice(0, insertPos) + "\nimport { Menu } from '@element-plus/icons-vue'" + content.slice(insertPos) } } return content } // 主处理函数 function processFile(filePath) { console.log(`处理文件: ${filePath}`) let content = fs.readFileSync(filePath, 'utf-8') // 检查是否已经包含 TableColumnControl if (hasTableColumnControl(content)) { console.log(` 跳过: 已包含 TableColumnControl`) return false } // 检查是否有 el-table if (!content.includes('