check-api.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. 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. }
  62. if (!tmpDetails || tmpDetails.indexOf(`**${name}:**`) === -1) {
  63. errors.push(chalk.red(`[${key}] missing '${name}'`))
  64. }
  65. }
  66. } else {
  67. errors.push(chalk.red(`[${key}] option could not be found`))
  68. }
  69. }
  70. errorSum += errors.length
  71. if (errors.length > 0) {
  72. console.log('-------------------------')
  73. console.log(`Checking file: ${file}`)
  74. console.log('-------------------------')
  75. errors.forEach((error) => {
  76. console.log(error)
  77. })
  78. }
  79. fs.writeFileSync(file, outLines.join('## '))
  80. }
  81. }
  82. class TableOptions extends API {
  83. init () {
  84. this.file = 'table-options.md'
  85. this.options = Object.keys(Constants.DEFAULTS).filter(it => {
  86. return !/^(on|format)[A-Z]/.test(it)
  87. })
  88. this.options.unshift('-')
  89. this.attributes = ['Attribute', 'Type', 'Detail', 'Default', 'Example']
  90. this.ignore = {
  91. totalRows: ['Example'],
  92. totalNotFiltered: ['Example'],
  93. virtualScrollItemHeight: ['Example']
  94. }
  95. }
  96. }
  97. class ColumnOptions extends API {
  98. init () {
  99. this.file = 'column-options.md'
  100. this.options = Object.keys(Constants.COLUMN_DEFAULTS)
  101. this.attributes = ['Attribute', 'Type', 'Detail', 'Default', 'Example']
  102. }
  103. }
  104. class Methods extends API {
  105. init () {
  106. this.file = 'methods.md'
  107. this.options = Constants.METHODS
  108. this.attributes = ['Parameter', 'Detail', 'Example']
  109. }
  110. }
  111. class Events extends API {
  112. init () {
  113. this.file = 'events.md'
  114. this.options = Object.values(Constants.EVENTS)
  115. this.attributes = ['jQuery Event', 'Parameter', 'Detail']
  116. }
  117. }
  118. class Localizations extends API {
  119. init () {
  120. this.file = 'localizations.md'
  121. this.options = Object.keys(Constants.LOCALES.en)
  122. this.attributes = ['Parameter', 'Default']
  123. }
  124. }
  125. new TableOptions()
  126. new ColumnOptions()
  127. new Methods()
  128. new Events()
  129. new Localizations()
  130. if (errorSum === 0) {
  131. console.log('Good job! Anything up to date!')
  132. process.exit(0)
  133. }
  134. process.exit(1)