check-api.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // eslint-disable-next-line no-global-assign
  2. require = require('esm')(module)
  3. const fs = require('fs')
  4. const chalk = require('chalk')
  5. const Constants = require('../src/constants/index.js').default
  6. let errorSum = 0
  7. const exampleFiles = [
  8. ...fs.readdirSync('./bootstrap-table-examples/welcomes'),
  9. ...fs.readdirSync('./bootstrap-table-examples/options'),
  10. ...fs.readdirSync('./bootstrap-table-examples/column-options'),
  11. ...fs.readdirSync('./bootstrap-table-examples/methods')
  12. ]
  13. class API {
  14. constructor () {
  15. this.init()
  16. this.sortOptions()
  17. this.check()
  18. }
  19. sortOptions () {
  20. this.options.sort()
  21. }
  22. check () {
  23. const file = `../site/docs/api/${this.file}`
  24. const md = {}
  25. const content = fs.readFileSync(file).toString()
  26. const lines = content.split('## ')
  27. const outLines = lines.slice(0, 1)
  28. const errors = []
  29. const exampleRegex = /\[.*\]\(.*\/(.*\.html)\)/m
  30. for (const item of lines.slice(1)) {
  31. md[item.split('\n')[0]] = item
  32. }
  33. const mds = Object.keys(md)
  34. for (const [i, key] of this.options.entries()) {
  35. if (md[key]) {
  36. outLines.push(md[key])
  37. const details = md[key].split('\n\n- ')
  38. for (let i = 0; i < this.attributes.length; i++) {
  39. const name = this.attributes[i]
  40. if (this.ignore && this.ignore[key] && this.ignore[key].includes(name)) {
  41. continue
  42. }
  43. const tmpDetails = details[i + 1].trim()
  44. if (name === 'Example') {
  45. const matches = exampleRegex.exec(tmpDetails)
  46. if (matches === null) {
  47. errors.push(chalk.red(`[${key}] missing or wrong formatted example`, `"${tmpDetails}"`))
  48. continue
  49. }
  50. if (!exampleFiles.includes(matches[1])) {
  51. errors.push(chalk.red(`[${key}] example '${matches[1]}' could not be found`))
  52. }
  53. }
  54. if (!tmpDetails || tmpDetails.indexOf(`**${name}:**`) === -1) {
  55. errors.push(chalk.red(`[${key}] missing '${name}'`))
  56. }
  57. }
  58. } else {
  59. outLines.push(key + '\n\n')
  60. }
  61. }
  62. errorSum += errors.length
  63. if (errors.length > 0) {
  64. console.log('-------------------------')
  65. console.log(`Checking file: ${file}`)
  66. console.log('-------------------------')
  67. errors.forEach((error) => {
  68. console.log(error)
  69. })
  70. }
  71. fs.writeFileSync(file, outLines.join('## '))
  72. }
  73. }
  74. class TableOptions extends API {
  75. init () {
  76. this.file = 'table-options.md'
  77. this.options = Object.keys(Constants.DEFAULTS).filter(it => {
  78. return !/^(on|format)[A-Z]/.test(it)
  79. })
  80. this.options.unshift('-')
  81. this.attributes = ['Attribute', 'Type', 'Detail', 'Default', 'Example']
  82. this.ignore = {
  83. totalRows: ['Example'],
  84. totalNotFiltered: ['Example'],
  85. virtualScrollItemHeight: ['Example']
  86. }
  87. }
  88. }
  89. class ColumnOptions extends API {
  90. init () {
  91. this.file = 'column-options.md'
  92. this.options = Object.keys(Constants.COLUMN_DEFAULTS)
  93. this.attributes = ['Attribute', 'Type', 'Detail', 'Default', 'Example']
  94. }
  95. }
  96. class Methods extends API {
  97. init () {
  98. this.file = 'methods.md'
  99. this.options = Constants.METHODS
  100. this.attributes = ['Parameter', 'Detail', 'Example']
  101. }
  102. }
  103. class Events extends API {
  104. init () {
  105. this.file = 'events.md'
  106. this.options = Object.values(Constants.EVENTS)
  107. this.attributes = ['jQuery Event', 'Parameter', 'Detail']
  108. }
  109. }
  110. class Localizations extends API {
  111. init () {
  112. this.file = 'localizations.md'
  113. this.options = Object.keys(Constants.LOCALES.en)
  114. this.attributes = ['Parameter', 'Default']
  115. }
  116. }
  117. new TableOptions()
  118. new ColumnOptions()
  119. new Methods()
  120. new Events()
  121. new Localizations()
  122. if (errorSum === 0) {
  123. console.log('Good job! Anything up to date!')
  124. process.exit(0)
  125. }
  126. process.exit(1)