助力国际化 使用node获取项目所有文件中的中文(去除注释)
最近项目正好需要做国际化,一个个复制词汇太麻烦,造个自动获取中文的轮子,方便后面使用。
完全体代码
使用了node-xlsx依赖,注意安装,用于直接生成xlsx文件
js
const path = '../test/src' // 文件夹地址
const outerFileName = '国际化词汇汇总' // 输出文件名
const fs = require('fs')
const xlsx = require('node-xlsx')
try {
const filePathArr = []
// 递归获取所有文件路径(js、vue)
function getAllFilesPath(path) {
const files = fs.readdirSync(path)
files.forEach((file) => {
if (/\.js$|\.vue$/.test(file)) {
filePathArr.push(path + '/' + file)
} else if (file.indexOf('.') === -1) {
getAllFilesPath(path + '/' + file)
}
})
}
getAllFilesPath(path)
console.debug('文件总数:', filePathArr.length)
// 遍历所有的文件 处理成字符串
let filesStr = ''
filePathArr.forEach((path) => {
filesStr += fs.readFileSync(path, 'utf8')
})
// 去除多行注释/单行注释/html注释
filesStr = filesStr.replace(/(\/\*[\s\S.]*?\*\/)|(\/\/[\s\S.]*?\n)|(<!--[\s\S.]*?-->)/g, '')
// 或者这样写也行,看起来清晰点,就是效率低点
// filesStr = filesStr
// .replace(/\/\*[\s\S.]*?\*\//g, '')
// .replace(/\/\/[\s\S.]*?\n/g, '')
// .replace(/<!--[\s\S.]*?-->/g, '')
// 获取字符串中的所有中文
const cnArr = Array.from(new Set(filesStr.match(/[\u4e00-\u9fa5]+/g)))
// 转换为xlsx格式
const buffer = xlsx.build([{ name: 'sheet', data: cnArr.map((word) => [word]) }])
// 写入文件
fs.writeFileSync(`${outerFileName}.xlsx`, buffer, 'binary')
console.debug('输出excel成功!')
} catch (err) {
console.debug(err)
}