// node ./plugins/lang/scriptconst fs = require('fs')const path = require('path')const langMapping = { 英语: 'en', 阿语: 'ar', 西语: 'es', 葡语: 'pt', 德语: 'de', 法语: 'fr', 意大利语: 'it'}const rootKey = 'en'const filesPath = { ar: path.resolve(__dirname, './ar/index.json'), // 阿语 de: path.resolve(__dirname, './de/index.json'), // 德语 en: path.resolve(__dirname, './en/index.json'), // 英语 es: path.resolve(__dirname, './es/index.json'), // 西语 fr: path.resolve(__dirname, './fr/index.json'), // 法语 it: path.resolve(__dirname, './it/index.json'), // 意大利语 pt: path.resolve(__dirname, './pt/index.json') // 葡语}const writeFilePath = { ar: path.resolve(__dirname, './ar/new-index.json'), de: path.resolve(__dirname, './de/new-index.json'), en: path.resolve(__dirname, './en/new-index.json'), es: path.resolve(__dirname, './es/new-index.json'), fr: path.resolve(__dirname, './fr/new-index.json'), it: path.resolve(__dirname, './it/new-index.json'), pt: path.resolve(__dirname, './pt/new-index.json')}let flag = true // 验证字段const data = [ { 中文: '----', 英语: '----', 阿语: '----', 西语: '----', 葡语: '----', 德语: '----', 法语: '----', 意大利语: '----' }]function write () { Object.keys(filesPath).forEach(file => { try { const content = fs.readFileSync(filesPath[file]) const temp = JSON.parse(content) console.log(Object.keys(temp).length) fs.writeFileSync(writeFilePath[file], JSON.stringify(temp, null, 2)) } catch (e) { console.log(filesPath[file]) console.error(e) } })}function addWriteNew () { const list = [] data.forEach(__data => { const obj = {} Object.keys(langMapping).forEach(key => { obj[langMapping[key]] = __data[key] }) list.push(obj) // 转换key }) Object.keys(filesPath).forEach(itemKey => { try { const content = fs.readFileSync(writeFilePath[itemKey]) const temp = JSON.parse(content) list.forEach(item => { temp[item[rootKey]] = item[itemKey] }) fs.writeFileSync(filesPath[itemKey], JSON.stringify(temp, null, 2)) fs.unlinkSync(writeFilePath[itemKey]) } catch (e) { console.log(filesPath[itemKey]) console.error(e) } })}function verification () { Object.keys(filesPath).forEach(file => { try { const oldText = fs.readFileSync(filesPath[file]) const newText = fs.readFileSync(writeFilePath[file]) const oldData = JSON.parse(oldText) const newData = JSON.parse(newText) console.log('比较开始') console.log(`${filesPath[file]} ${Object.keys(oldData).length}`) console.log(`${writeFilePath[file]} ${Object.keys(newData).length}`) if (Object.keys(oldData).length !== Object.keys(newData).length) { throw '验证失败' } Object.keys(oldData).forEach(key => { if (oldData[key] !== newData[key]) { throw '验证失败' } }) console.log('比较成功') } catch (e) { flag = false console.error(filesPath[file]) console.error(e) } }) if (flag) { console.log('验证成功') } else { console.log('验证失败') }}// 读文件,写入一个新文件,保证格式正确write()// 新文件对比原有文件,每个key和value都一致verification()if (flag) { addWriteNew() // writeToNew();}
// 读excel 生成 new-data.json/** * 在项目根目录中运行命令 * node ./plugins/lang/readExcel.js -path ../excel.xlsx -o ./plugins/lang/new-data.json*/const XLSX = require('xlsx')const fs = require('fs')let filePath = ''let outputPath = ''let demoPath = ''process.argv.forEach((text, index) => { if (text === '-path') { filePath = process.argv[index + 1] } if (text === '-demo') { demoPath = process.argv[index + 1] } if (text === '-o') { outputPath = process.argv[index + 1] }})function readXlsx (filename) { const workbook = XLSX.readFile(filename) // 获取 Excel 中所有表名 var sheetNames = workbook.SheetNames // 返回 ['sheet1', 'sheet2',……] // 根据表名获取对应某张表 var worksheet = workbook.Sheets[sheetNames[0]] // 将表转换为json var workjson = XLSX.utils.sheet_to_json(worksheet) return workjson}function writeXlsxDemo (filename) { // 写入一个例子 const json = [ { 中文: '----', 英语: '----', 阿语: '----', 西语: '----', 葡语: '----', 德语: '----', 法语: '----', 意大利语: '----' } ] var workbook2 = XLSX.utils.json_to_sheet(json) var wb = { SheetNames: ['mySheet'], Sheets: { mySheet: workbook2 } } XLSX.writeFile(wb, filename)}function writeJson (filename, json) { console.log(json) // 写入一个例子 fs.writeFileSync(filename, JSON.stringify(json, null, 2))}try { // 验证文件是否存在, 并且有读权限 if (demoPath) { writeXlsxDemo(demoPath) return } fs.accessSync(filePath, fs.constants.R_OK) const json = readXlsx(filePath) writeJson(outputPath || './new-data.json', json)} catch (err) { console.error(err)}