check-api.js 4.8 KB

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