const fs = require('fs') const path = require('path') function walk(dir, out = []) { for (const name of fs.readdirSync(dir)) { const p = path.join(dir, name) if (fs.statSync(p).isDirectory()) walk(p, out) else if (/\.vue$/.test(name)) out.push(p) } return out } function isValidUtf8Chinese(buf) { return buf.includes(Buffer.from([0xe5, 0x9c, 0xba, 0xe6, 0x99, 0xaf])) // 锟斤拷锟斤拷 || buf.includes(Buffer.from([0xe8, 0xb0, 0x83, 0xe8, 0xaf, 0x95])) // 锟斤拷锟斤拷 } function shouldConvert(buf) { if (buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) { return !isValidUtf8Chinese(buf) && buf.toString('utf8').includes('\uFFFD') } if (isValidUtf8Chinese(buf)) return false const asUtf = buf.toString('utf8') if (asUtf.includes('\uFFFD')) return true // GBK double-byte for CJK return /[\u0080-\u00ff]/.test(buf.slice(300, 400).toString('latin1')) } function convertFile(file) { const buf = fs.readFileSync(file) if (!shouldConvert(buf)) return false const text = new TextDecoder('gbk').decode(buf).replace(/^\uFEFF/, '') if (!/[\u4e00-\u9fff]/.test(text)) return false fs.writeFileSync(file, text, 'utf8') return true } const roots = [ path.join(__dirname, '../src/views/lobster'), path.join(__dirname, '../../saas-companyui/src/views/lobster') ] let count = 0 for (const root of roots) { if (!fs.existsSync(root)) continue for (const file of walk(root)) { if (convertFile(file)) { count++ console.log('converted', path.relative(path.join(__dirname, '..', '..'), file)) } } } console.log('done', count, 'files')