check-api.js 4.5 KB

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