fix-gbk-vue-encoding.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const fs = require('fs')
  2. const path = require('path')
  3. function walk(dir, out = []) {
  4. for (const name of fs.readdirSync(dir)) {
  5. const p = path.join(dir, name)
  6. if (fs.statSync(p).isDirectory()) walk(p, out)
  7. else if (/\.vue$/.test(name)) out.push(p)
  8. }
  9. return out
  10. }
  11. function isValidUtf8Chinese(buf) {
  12. return buf.includes(Buffer.from([0xe5, 0x9c, 0xba, 0xe6, 0x99, 0xaf])) // 锟斤拷锟斤拷
  13. || buf.includes(Buffer.from([0xe8, 0xb0, 0x83, 0xe8, 0xaf, 0x95])) // 锟斤拷锟斤拷
  14. }
  15. function shouldConvert(buf) {
  16. if (buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) {
  17. return !isValidUtf8Chinese(buf) && buf.toString('utf8').includes('\uFFFD')
  18. }
  19. if (isValidUtf8Chinese(buf)) return false
  20. const asUtf = buf.toString('utf8')
  21. if (asUtf.includes('\uFFFD')) return true
  22. // GBK double-byte for CJK
  23. return /[\u0080-\u00ff]/.test(buf.slice(300, 400).toString('latin1'))
  24. }
  25. function convertFile(file) {
  26. const buf = fs.readFileSync(file)
  27. if (!shouldConvert(buf)) return false
  28. const text = new TextDecoder('gbk').decode(buf).replace(/^\uFEFF/, '')
  29. if (!/[\u4e00-\u9fff]/.test(text)) return false
  30. fs.writeFileSync(file, text, 'utf8')
  31. return true
  32. }
  33. const roots = [
  34. path.join(__dirname, '../src/views/lobster'),
  35. path.join(__dirname, '../../saas-companyui/src/views/lobster')
  36. ]
  37. let count = 0
  38. for (const root of roots) {
  39. if (!fs.existsSync(root)) continue
  40. for (const file of walk(root)) {
  41. if (convertFile(file)) {
  42. count++
  43. console.log('converted', path.relative(path.join(__dirname, '..', '..'), file))
  44. }
  45. }
  46. }
  47. console.log('done', count, 'files')