normalize-list-labels.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Batch normalize saas-adminui list page labels and button types.
  3. * Usage: node scripts/normalize-list-labels.js
  4. */
  5. const fs = require('fs')
  6. const path = require('path')
  7. const viewsDir = path.join(__dirname, '../src/views')
  8. const replacements = [
  9. ['label="\u516c\u53f8\u540d"', 'label="\u516c\u53f8\u540d\u79f0"'],
  10. ['label="\u516c\u53f8\u540d:"', 'label="\u516c\u53f8\u540d\u79f0:"'],
  11. ['placeholder="\u8bf7\u9009\u62e9\u516c\u53f8\u540d"', 'placeholder="\u8bf7\u9009\u62e9\u516c\u53f8\u540d\u79f0"'],
  12. ['placeholder="\u8bf7\u8f93\u5165\u516c\u53f8\u540d"', 'placeholder="\u8bf7\u8f93\u5165\u516c\u53f8\u540d\u79f0"'],
  13. ['placeholder="\u5168\u90e8\u516c\u53f8"', 'placeholder="\u5168\u90e8"'],
  14. ['label="\u624b\u673a" prop="mobile"', 'label="\u624b\u673a\u53f7\u7801" prop="mobile"'],
  15. ['label="\u624b\u673a"', 'label="\u624b\u673a\u53f7\u7801"'],
  16. ['placeholder="\u8bf7\u8f93\u5165\u624b\u673a"', 'placeholder="\u8bf7\u8f93\u5165\u624b\u673a\u53f7\u7801"'],
  17. ['type="cyan"', 'type="primary"'],
  18. ['label-width="68px"', 'label-width="88px"'],
  19. ['label-width="85px"', 'label-width="88px"'],
  20. ['label-width="90px"', 'label-width="88px"'],
  21. ['label-width="100px"', 'label-width="88px"'],
  22. ['style="width: 205.4px"', 'style="width: 100%"'],
  23. ['style="width:205.4px"', 'style="width: 100%"'],
  24. ['style="width: 220px"', 'style="width: 100%"'],
  25. ['style="width:220px"', 'style="width: 100%"']
  26. ]
  27. function walk(dir, files = []) {
  28. for (const name of fs.readdirSync(dir)) {
  29. const full = path.join(dir, name)
  30. const stat = fs.statSync(full)
  31. if (stat.isDirectory()) {
  32. walk(full, files)
  33. } else if (name.endsWith('.vue')) {
  34. files.push(full)
  35. }
  36. }
  37. return files
  38. }
  39. let changedFiles = 0
  40. let totalReplacements = 0
  41. for (const file of walk(viewsDir)) {
  42. let content = fs.readFileSync(file, 'utf8')
  43. const original = content
  44. let fileCount = 0
  45. for (const [from, to] of replacements) {
  46. if (content.includes(from)) {
  47. const parts = content.split(from)
  48. fileCount += parts.length - 1
  49. content = parts.join(to)
  50. }
  51. }
  52. if (content !== original) {
  53. fs.writeFileSync(file, content, 'utf8')
  54. changedFiles++
  55. totalReplacements += fileCount
  56. console.log('updated: ' + path.relative(viewsDir, file) + ' (' + fileCount + ')')
  57. }
  58. }
  59. console.log('\nDone. ' + changedFiles + ' files, ' + totalReplacements + ' replacements.')