check-locale.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const fs = require('fs')
  2. const safeEval = require('safe-eval')
  3. const _ = require('lodash')
  4. const chalk = require('chalk')
  5. const DIR = '../src/locale/'
  6. const readObj = text => {
  7. return safeEval(text.replace('($ => {', '')
  8. .replace('})(jQuery)', '')
  9. .replace(/\$\.fn\.bootstrapTable\.locales\['.*'\] = /, '')
  10. .replace(/\$\.extend.*/, '').trim())
  11. }
  12. const readString = (obj, text) => {
  13. const lines = text.split('\n')
  14. for (const key in obj) {
  15. if (obj[key]) {
  16. const index = _.findIndex(lines, line => line.includes(key))
  17. if (Object.keys(obj).pop() === key) {
  18. lines[index + 2] += ','
  19. }
  20. obj[key] = lines.slice(index, index + 3).join('\n')
  21. }
  22. }
  23. return obj
  24. }
  25. const baseText = fs.readFileSync(DIR + 'bootstrap-table-en-US.js').toString()
  26. const baseObj = readString(readObj(baseText), baseText)
  27. fs.readdir(`${DIR}`, (err, files) => {
  28. let errorSum = 0
  29. for (const file of files) {
  30. if (!/\.js$/.test(file) || file === 'bootstrap-table-en-US.js') {
  31. continue
  32. }
  33. const text = fs.readFileSync(DIR + file).toString()
  34. const obj = readString(readObj(text), text)
  35. const keys = Object.keys(obj)
  36. let offset = 0
  37. const errors = []
  38. for (const [i, key] of Object.keys(baseObj).entries()) {
  39. if (!keys.includes(key)) {
  40. errors.push(chalk.red(`Missing key: '${key}'`))
  41. offset++
  42. } else if (keys[i - offset] !== key) {
  43. errors.push(chalk.red(`Order error: '${key}'`))
  44. }
  45. }
  46. errorSum += errors.length
  47. if (errors.length > 0) {
  48. console.log('-------------------------')
  49. console.log(`Checking file: ${file}`)
  50. console.log('-------------------------')
  51. errors.forEach((error) => {
  52. console.log(error)
  53. })
  54. }
  55. }
  56. if (errorSum === 0) {
  57. console.log('Good job! Anything up to date!')
  58. }
  59. })