check-api.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. console.log('-------------------------')
  41. console.log(`Checking file: ${file}`)
  42. console.log('-------------------------')
  43. const noDefaults = Object.keys(md).filter(it => !this.options.includes(it))
  44. if (noDefaults.length) {
  45. errorSum += noDefaults.length
  46. console.log(chalk.red(`No default option was found for "${noDefaults.join(', ')}", should the documentation be removed!`))
  47. return
  48. }
  49. for (const [i, key] of this.options.entries()) {
  50. try {
  51. if (md[key]) {
  52. outLines.push(md[key])
  53. const details = md[key].split('\n\n- ')
  54. for (let i = 0; i < this.attributes.length; i++) {
  55. const name = this.attributes[i]
  56. if (this.ignore && this.ignore[key] && this.ignore[key].includes(name)) {
  57. continue
  58. }
  59. const tmpDetails = details[i + 1].trim()
  60. if (name === 'Example' && exampleFilesFound) {
  61. const matches = exampleRegex.exec(tmpDetails)
  62. if (!matches) {
  63. errors.push(chalk.red(`[${key}] missing or wrong formatted example`, `"${tmpDetails}"`))
  64. continue
  65. }
  66. if (!exampleFiles.includes(matches[1])) {
  67. errors.push(chalk.red(`[${key}] example '${matches[1]}' could not be found`))
  68. }
  69. } else if (name === 'Attribute' && key !== 'columns') {
  70. const attributeMatches = attributeRegex.exec(tmpDetails)
  71. if (!attributeMatches) {
  72. errors.push(chalk.red(`[${key}] missing or wrong formatted attribute`, `"${tmpDetails}"`))
  73. continue
  74. }
  75. }
  76. if (!tmpDetails || tmpDetails.indexOf(`**${name}:**`) === -1) {
  77. errors.push(chalk.red(`[${key}] missing '${name}'`))
  78. }
  79. }
  80. } else {
  81. errors.push(chalk.red(`[${key}] option could not be found`))
  82. }
  83. } catch (ex) {
  84. console.log(ex)
  85. }
  86. }
  87. errorSum += errors.length
  88. if (errors.length > 0) {
  89. errors.forEach((error) => {
  90. console.log(error)
  91. })
  92. }
  93. fs.writeFileSync(file, outLines.join('## '))
  94. }
  95. }
  96. class TableOptions extends API {
  97. init () {
  98. this.file = 'table-options.md'
  99. this.options = Object.keys(Constants.DEFAULTS).filter(it => {
  100. return !/^(on|format)[A-Z]/.test(it)
  101. })
  102. this.options.unshift('-')
  103. this.attributes = ['Attribute', 'Type', 'Detail', 'Default', 'Example']
  104. this.ignore = {
  105. totalRows: ['Example'],
  106. totalNotFiltered: ['Example'],
  107. virtualScrollItemHeight: ['Example']
  108. }
  109. }
  110. }
  111. class ColumnOptions extends API {
  112. init () {
  113. this.file = 'column-options.md'
  114. this.options = Object.keys(Constants.COLUMN_DEFAULTS)
  115. this.attributes = ['Attribute', 'Type', 'Detail', 'Default', 'Example']
  116. }
  117. }
  118. class Methods extends API {
  119. init () {
  120. this.file = 'methods.md'
  121. this.options = Constants.METHODS
  122. this.attributes = ['Parameter', 'Detail', 'Example']
  123. }
  124. }
  125. class Events extends API {
  126. init () {
  127. this.file = 'events.md'
  128. this.options = Object.values(Constants.EVENTS)
  129. this.attributes = ['jQuery Event', 'Parameter', 'Detail']
  130. }
  131. }
  132. class Localizations extends API {
  133. init () {
  134. this.file = 'localizations.md'
  135. this.options = Object.keys(Constants.LOCALES.en)
  136. this.attributes = ['Parameter', 'Default']
  137. }
  138. }
  139. new TableOptions()
  140. new ColumnOptions()
  141. new Methods()
  142. new Events()
  143. new Localizations()
  144. if (errorSum === 0) {
  145. console.log('Good job! Anything up to date!')
  146. process.exit(0)
  147. }
  148. process.exit(1)