123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- const fs = require('fs')
- const path = require('path')
- const config = {
- inputDir: './pages',
- outputFile: './zh-CN.js',
- extensions: ['.js', '.vue'],
- chineseRegex: /[\u4e00-\u9fa5\u3000-\u303f\uff01-\uff5e\u201c-\u201d\u3008-\u3011]+/g
- }
- const chineseSet = new Set()
- function scanDirectory(dir) {
- const files = fs.readdirSync(dir)
- files.forEach(file => {
- const filePath = path.join(dir, file)
- const stat = fs.statSync(filePath)
- if (stat.isDirectory()) {
- scanDirectory(filePath)
- } else if (stat.isFile() &&config.extensions.includes(path.extname(filePath).toLowerCase()))
- {
- processFile(filePath)
- }
- })
- }
- function processFile(filePath) {
- try {
- const content = fs.readFileSync(filePath, 'utf-8')
- const matches = content.match(config.chineseRegex)
- if (matches) {
- matches.forEach(text => chineseSet.add(text))
- }
- } catch (err) {
- console.error(`Error processing file ${filePath}:`, err)
- }
- }
- function generateOutput() {
- const output = `// 自动生成的中文字符列表
- module.exports = ${JSON.stringify([...chineseSet], null, 2)}`
- fs.writeFileSync(config.outputFile, output, 'utf-8')
- console.log(`找到 ${chineseSet.size} 个中文字符,已保存到 ${config.outputFile}`)
- }
- scanDirectory(config.inputDir)
- generateOutput()
|