check-api.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 exampleFilesFolder = './bootstrap-table-examples/'
  8. const exampleFilesFound = fs.existsSync(exampleFilesFolder)
  9. let exampleFiles = []
  10. if (exampleFilesFound) {
  11. exampleFiles = [
  12. ...fs.readdirSync(exampleFilesFolder + 'welcomes'),
  13. ...fs.readdirSync(exampleFilesFolder + 'options'),
  14. ...fs.readdirSync(exampleFilesFolder + 'column-options'),
  15. ...fs.readdirSync(exampleFilesFolder + 'methods')
  16. ]
  17. } else {
  18. console.log((chalk.yellow(chalk.bold('Warning: ') + 'Cant check if example files are correct formatted and have a valid url.')))
  19. console.log((chalk.yellow(chalk.bold('Warning: ') + 'To enable that check, please clone the "bootstrap-table-examples" repository in the tools folder or create a symlink (if you already cloned the repository on an other path).')))
  20. }
  21. class API {
  22. constructor () {
  23. this.init()
  24. this.sortOptions()
  25. this.check()
  26. }
  27. sortOptions () {
  28. this.options.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
  29. }
  30. check () {
  31. const file = `../site/docs/api/${this.file}`
  32. const md = {}
  33. const content = fs.readFileSync(file).toString()
  34. const lines = content.split('## ')
  35. const outLines = lines.slice(0, 1)
  36. const errors = []
  37. const exampleRegex = /\[.*\]\(.*\/(.*\.html)\)/m
  38. const attributeRegex = /\*\*Attribute:\*\*\s`(.*)data-(.*)`/m
  39. for (const item of lines.slice(1)) {
  40. md[item.split('\n')[0]] = item
  41. }
  42. const mds = Object.keys(md)
  43. for (const [i, key] of this.options.entries()) {
  44. try {
  45. if (md[key]) {
  46. outLines.push(md[key])
  47. const details = md[key].split('\n\n- ')
  48. for (let i = 0; i < this.attributes.length; i++) {
  49. const name = this.attributes[i]
  50. if (this.ignore && this.ignore[key] && this.ignore[key].includes(name)) {
  51. continue
  52. }
  53. const tmpDetails = details[i + 1].trim()
  54. if (name === 'Example' && exampleFilesFound) {
  55. const matches = exampleRegex.exec(tmpDetails)
  56. if (!matches) {
  57. errors.push(chalk.red(`[${key}] missing or wrong formatted example`, `"${tmpDetails}"`))
  58. continue
  59. }
  60. if (!exampleFiles.includes(matches[1])) {
  61. errors.push(chalk.red(`[${key}] example '${matches[1]}' could not be found`))
  62. }
  63. } else if (name === 'Attribute' && key !== 'columns') {
  64. const attributeMatches = attributeRegex.exec(tmpDetails)
  65. if (!attributeMatches) {
  66. errors.push(chalk.red(`[${key}] missing or wrong formatted attribute`, `"${tmpDetails}"`))
  67. continue
  68. }
  69. }
  70. if (!tmpDetails || tmpDetails.indexOf(`**${name}:**`) === -1) {
  71. errors.push(chalk.red(`[${key}] missing '${name}'`))
  72. }
  73. }
  74. } else {
  75. errors.push(chalk.red(`[${key}] option could not be found`))
  76. }
  77. } catch (ex) {
  78. console.log(ex)
  79. }
  80. }
  81. errorSum += errors.length
  82. if (errors.length > 0) {
  83. console.log('-------------------------')
  84. console.log(`Checking file: ${file}`)
  85. console.log('-------------------------')
  86. errors.forEach((error) => {
  87. console.log(error)
  88. })
  89. }
  90. fs.writeFileSync(file, outLines.join('## '))
  91. }
  92. }
  93. class TableOptions extends API {
  94. init () {
  95. this.file = 'table-options.md'
  96. this.options = Object.keys(Constants.DEFAULTS).filter(it => {
  97. return !/^(on|format)[A-Z]/.test(it)
  98. })
  99. this.options.unshift('-')
  100. this.attributes = ['Attribute', 'Type', 'Detail', 'Default', 'Example']
  101. this.ignore = {
  102. totalRows: ['Example'],
  103. totalNotFiltered: ['Example'],
  104. virtualScrollItemHeight: ['Example']
  105. }
  106. }
  107. }
  108. class ColumnOptions extends API {
  109. init () {
  110. this.file = 'column-options.md'
  111. this.options = Object.keys(Constants.COLUMN_DEFAULTS)
  112. this.attributes = ['Attribute', 'Type', 'Detail', 'Default', 'Example']
  113. }
  114. }
  115. class Methods extends API {
  116. init () {
  117. this.file = 'methods.md'
  118. this.options = Constants.METHODS
  119. this.attributes = ['Parameter', 'Detail', 'Example']
  120. }
  121. }
  122. class Events extends API {
  123. init () {
  124. this.file = 'events.md'
  125. this.options = Object.values(Constants.EVENTS)
  126. this.attributes = ['jQuery Event', 'Parameter', 'Detail']
  127. }
  128. }
  129. class Localizations extends API {
  130. init () {
  131. this.file = 'localizations.md'
  132. this.options = Object.keys(Constants.LOCALES.en)
  133. this.attributes = ['Parameter', 'Default']
  134. }
  135. }
  136. new TableOptions()
  137. new ColumnOptions()
  138. new Methods()
  139. new Events()
  140. new Localizations()
  141. if (errorSum === 0) {
  142. console.log('Good job! Anything up to date!')
  143. process.exit(0)
  144. }
  145. process.exit(1)