repair-list-search-corruption.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * 修复 list-search-actions 内误嵌套查询项 / 未闭合 / 重复 toolbar 的问题
  3. */
  4. const fs = require('fs')
  5. const path = require('path')
  6. const targets = process.argv.slice(2)
  7. if (!targets.length) {
  8. console.error('Usage: node repair-list-search-corruption.js <vue-file> [...]')
  9. process.exit(1)
  10. }
  11. function extractButtons(html) {
  12. return (html.match(/<el-button[\s\S]*?<\/el-button>/g) || []).map((btn) =>
  13. btn.replace(/\stype="primary"/, ' type="cyan"')
  14. )
  15. }
  16. function repair(content) {
  17. let next = content
  18. // list-search-actions 内只有嵌套查询项 + 紧跟 </el-form> + 可选 list-toolbar
  19. next = next.replace(
  20. /<el-form-item class="list-search-actions">\s*((?:<el-form-item\b[\s\S]*?<\/el-form-item>\s*)+)<\/el-form>([\s\S]*?)(?=<el-table|<\/el-card|<el-tabs|<div class="batch|<el-row(?![^>]*list-toolbar))/g,
  21. (full, nested, tail) => {
  22. const toolbar = tail.match(/<el-row\b[\s\S]*?list-toolbar[\s\S]*?<\/el-row>/)
  23. const buttons = toolbar ? extractButtons(toolbar[0]) : []
  24. const tailWithoutToolbar = toolbar ? tail.replace(toolbar[0], '') : tail
  25. if (!buttons.length) {
  26. return `${nested}\n</el-form>${tailWithoutToolbar}`
  27. }
  28. return `${nested}<el-form-item class="list-search-actions">\n ${buttons.join(
  29. '\n '
  30. )}\n </el-form-item>\n</el-form>${tailWithoutToolbar}`
  31. }
  32. )
  33. // 仅嵌套查询项、无按钮:去掉 list-search-actions 包裹
  34. next = next.replace(
  35. /<el-form-item class="list-search-actions">\s*((?:<el-form-item\b[\s\S]*?<\/el-form-item>\s*)+)<\/el-form>/g,
  36. '$1\n</el-form>'
  37. )
  38. return next
  39. }
  40. for (const file of targets) {
  41. const abs = path.resolve(file)
  42. const original = fs.readFileSync(abs, 'utf8')
  43. const fixed = repair(original)
  44. if (fixed !== original) {
  45. fs.writeFileSync(abs, fixed, 'utf8')
  46. console.log('repaired:', abs)
  47. } else {
  48. console.log('unchanged:', abs)
  49. }
  50. }