check-locale.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. for (const file of files) {
  29. if (!/\.js$/.test(file) || file === 'bootstrap-table-en-US.js') {
  30. continue
  31. }
  32. console.log('-------------------------')
  33. console.log(`Checking file: ${file}`)
  34. console.log('-------------------------')
  35. const text = fs.readFileSync(DIR + file).toString()
  36. const obj = readString(readObj(text), text)
  37. const keys = Object.keys(obj)
  38. let offset = 0
  39. for (const [i, key] of Object.keys(baseObj).entries()) {
  40. if (!keys.includes(key)) {
  41. console.log(chalk.red(`Missing key: '${key}'`))
  42. offset++
  43. } else if (keys[i - offset] !== key) {
  44. console.log(chalk.red(`Order error: '${key}'`))
  45. }
  46. }
  47. }
  48. })