check-locale.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import fs from 'fs'
  2. import chalk from 'chalk'
  3. import _ from 'lodash'
  4. const DIR = '../src/locale/'
  5. const readKeys = text => {
  6. return text.match(/ {2}(format\w*) \(/g)
  7. .map(it => it.replace(' ', '').replace(' (', ''))
  8. }
  9. const baseText = fs.readFileSync(DIR + 'bootstrap-table-en-US.js').toString()
  10. const baseKeys = readKeys(baseText)
  11. fs.readdir(`${DIR}`, (err, files) => {
  12. let errorSum = 0
  13. for (const file of files) {
  14. if (!/\.js$/.test(file) || file === 'bootstrap-table-en-US.js') {
  15. continue
  16. }
  17. const text = fs.readFileSync(DIR + file).toString()
  18. const keys = readKeys(text)
  19. let offset = 0
  20. const errors = []
  21. for (const [i, key] of baseKeys.entries()) {
  22. if (!keys.includes(key)) {
  23. errors.push(chalk.red(`Missing key: '${key}'`))
  24. offset++
  25. } else if (keys[i - offset] !== key) {
  26. errors.push(chalk.red(`Order error: '${key}'`))
  27. }
  28. }
  29. errorSum += errors.length
  30. if (errors.length > 0) {
  31. console.log('-------------------------')
  32. console.log(`Checking file: ${file}`)
  33. console.log('-------------------------')
  34. errors.forEach((error) => {
  35. console.log(error)
  36. })
  37. }
  38. }
  39. if (errorSum === 0) {
  40. console.log('Good job! Anything up to date!')
  41. process.exit(0)
  42. }
  43. process.exit(1)
  44. })