1. // node ./plugins/lang/script
    2. const fs = require('fs')
    3. const path = require('path')
    4. const langMapping = {
    5. 英语: 'en',
    6. 阿语: 'ar',
    7. 西语: 'es',
    8. 葡语: 'pt',
    9. 德语: 'de',
    10. 法语: 'fr',
    11. 意大利语: 'it'
    12. }
    13. const rootKey = 'en'
    14. const filesPath = {
    15. ar: path.resolve(__dirname, './ar/index.json'), // 阿语
    16. de: path.resolve(__dirname, './de/index.json'), // 德语
    17. en: path.resolve(__dirname, './en/index.json'), // 英语
    18. es: path.resolve(__dirname, './es/index.json'), // 西语
    19. fr: path.resolve(__dirname, './fr/index.json'), // 法语
    20. it: path.resolve(__dirname, './it/index.json'), // 意大利语
    21. pt: path.resolve(__dirname, './pt/index.json') // 葡语
    22. }
    23. const writeFilePath = {
    24. ar: path.resolve(__dirname, './ar/new-index.json'),
    25. de: path.resolve(__dirname, './de/new-index.json'),
    26. en: path.resolve(__dirname, './en/new-index.json'),
    27. es: path.resolve(__dirname, './es/new-index.json'),
    28. fr: path.resolve(__dirname, './fr/new-index.json'),
    29. it: path.resolve(__dirname, './it/new-index.json'),
    30. pt: path.resolve(__dirname, './pt/new-index.json')
    31. }
    32. let flag = true // 验证字段
    33. const data = [
    34. {
    35. 中文: '----',
    36. 英语: '----',
    37. 阿语: '----',
    38. 西语: '----',
    39. 葡语: '----',
    40. 德语: '----',
    41. 法语: '----',
    42. 意大利语: '----'
    43. }
    44. ]
    45. function write () {
    46. Object.keys(filesPath).forEach(file => {
    47. try {
    48. const content = fs.readFileSync(filesPath[file])
    49. const temp = JSON.parse(content)
    50. console.log(Object.keys(temp).length)
    51. fs.writeFileSync(writeFilePath[file], JSON.stringify(temp, null, 2))
    52. } catch (e) {
    53. console.log(filesPath[file])
    54. console.error(e)
    55. }
    56. })
    57. }
    58. function addWriteNew () {
    59. const list = []
    60. data.forEach(__data => {
    61. const obj = {}
    62. Object.keys(langMapping).forEach(key => {
    63. obj[langMapping[key]] = __data[key]
    64. })
    65. list.push(obj) // 转换key
    66. })
    67. Object.keys(filesPath).forEach(itemKey => {
    68. try {
    69. const content = fs.readFileSync(writeFilePath[itemKey])
    70. const temp = JSON.parse(content)
    71. list.forEach(item => {
    72. temp[item[rootKey]] = item[itemKey]
    73. })
    74. fs.writeFileSync(filesPath[itemKey], JSON.stringify(temp, null, 2))
    75. fs.unlinkSync(writeFilePath[itemKey])
    76. } catch (e) {
    77. console.log(filesPath[itemKey])
    78. console.error(e)
    79. }
    80. })
    81. }
    82. function verification () {
    83. Object.keys(filesPath).forEach(file => {
    84. try {
    85. const oldText = fs.readFileSync(filesPath[file])
    86. const newText = fs.readFileSync(writeFilePath[file])
    87. const oldData = JSON.parse(oldText)
    88. const newData = JSON.parse(newText)
    89. console.log('比较开始')
    90. console.log(`${filesPath[file]} ${Object.keys(oldData).length}`)
    91. console.log(`${writeFilePath[file]} ${Object.keys(newData).length}`)
    92. if (Object.keys(oldData).length !== Object.keys(newData).length) {
    93. throw '验证失败'
    94. }
    95. Object.keys(oldData).forEach(key => {
    96. if (oldData[key] !== newData[key]) {
    97. throw '验证失败'
    98. }
    99. })
    100. console.log('比较成功')
    101. } catch (e) {
    102. flag = false
    103. console.error(filesPath[file])
    104. console.error(e)
    105. }
    106. })
    107. if (flag) {
    108. console.log('验证成功')
    109. } else {
    110. console.log('验证失败')
    111. }
    112. }
    113. // 读文件,写入一个新文件,保证格式正确
    114. write()
    115. // 新文件对比原有文件,每个key和value都一致
    116. verification()
    117. if (flag) {
    118. addWriteNew()
    119. // writeToNew();
    120. }
    1. // 读excel 生成 new-data.json
    2. /**
    3. * 在项目根目录中运行命令
    4. * node ./plugins/lang/readExcel.js -path ../excel.xlsx -o ./plugins/lang/new-data.json
    5. */
    6. const XLSX = require('xlsx')
    7. const fs = require('fs')
    8. let filePath = ''
    9. let outputPath = ''
    10. let demoPath = ''
    11. process.argv.forEach((text, index) => {
    12. if (text === '-path') {
    13. filePath = process.argv[index + 1]
    14. }
    15. if (text === '-demo') {
    16. demoPath = process.argv[index + 1]
    17. }
    18. if (text === '-o') {
    19. outputPath = process.argv[index + 1]
    20. }
    21. })
    22. function readXlsx (filename) {
    23. const workbook = XLSX.readFile(filename)
    24. // 获取 Excel 中所有表名
    25. var sheetNames = workbook.SheetNames // 返回 ['sheet1', 'sheet2',……]
    26. // 根据表名获取对应某张表
    27. var worksheet = workbook.Sheets[sheetNames[0]]
    28. // 将表转换为json
    29. var workjson = XLSX.utils.sheet_to_json(worksheet)
    30. return workjson
    31. }
    32. function writeXlsxDemo (filename) {
    33. // 写入一个例子
    34. const json = [
    35. {
    36. 中文: '----',
    37. 英语: '----',
    38. 阿语: '----',
    39. 西语: '----',
    40. 葡语: '----',
    41. 德语: '----',
    42. 法语: '----',
    43. 意大利语: '----'
    44. }
    45. ]
    46. var workbook2 = XLSX.utils.json_to_sheet(json)
    47. var wb = {
    48. SheetNames: ['mySheet'],
    49. Sheets: {
    50. mySheet: workbook2
    51. }
    52. }
    53. XLSX.writeFile(wb, filename)
    54. }
    55. function writeJson (filename, json) {
    56. console.log(json)
    57. // 写入一个例子
    58. fs.writeFileSync(filename, JSON.stringify(json, null, 2))
    59. }
    60. try {
    61. // 验证文件是否存在, 并且有读权限
    62. if (demoPath) {
    63. writeXlsxDemo(demoPath)
    64. return
    65. }
    66. fs.accessSync(filePath, fs.constants.R_OK)
    67. const json = readXlsx(filePath)
    68. writeJson(outputPath || './new-data.json', json)
    69. } catch (err) {
    70. console.error(err)
    71. }