check-api.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. class API {
  8. constructor () {
  9. this.init()
  10. this.sortOptions()
  11. this.check()
  12. }
  13. sortOptions () {
  14. this.options.sort()
  15. }
  16. check () {
  17. const file = `../site/docs/api/${this.file}`
  18. const md = {}
  19. const content = fs.readFileSync(file).toString()
  20. const lines = content.split('## ')
  21. const outLines = lines.slice(0, 1)
  22. const errors = []
  23. for (const item of lines.slice(1)) {
  24. md[item.split('\n')[0]] = item
  25. }
  26. const mds = Object.keys(md)
  27. for (const [i, key] of this.options.entries()) {
  28. if (md[key]) {
  29. outLines.push(md[key])
  30. const details = md[key].split('\n\n- ')
  31. for (let i = 0; i < this.attributes.length; i++) {
  32. const name = this.attributes[i]
  33. if (this.ignore && this.ignore[key] && this.ignore[key].includes(name)) {
  34. continue
  35. }
  36. if (!details[i + 1] || details[i + 1].indexOf(`**${name}:**`) === -1) {
  37. errors.push(chalk.red(`[${key}] missing '${name}'`))
  38. }
  39. }
  40. } else {
  41. outLines.push(key + '\n\n')
  42. }
  43. }
  44. errorSum += errors.length
  45. if (errors.length > 0) {
  46. console.log('-------------------------')
  47. console.log(`Checking file: ${file}`)
  48. console.log('-------------------------')
  49. errors.forEach((error) => {
  50. console.log(error)
  51. })
  52. }
  53. fs.writeFileSync(file, outLines.join('## '))
  54. }
  55. }
  56. class TableOptions extends API {
  57. init () {
  58. this.file = 'table-options.md'
  59. this.options = Object.keys(Constants.DEFAULTS).filter(it => {
  60. return !/^(on|format)[A-Z]/.test(it)
  61. })
  62. this.options.unshift('-')
  63. this.attributes = ['Attribute', 'Type', 'Detail', 'Default', 'Example']
  64. this.ignore = {
  65. totalRows: ['Example'],
  66. totalNotFiltered: ['Example']
  67. }
  68. }
  69. }
  70. class ColumnOptions extends API {
  71. init () {
  72. this.file = 'column-options.md'
  73. this.options = Object.keys(Constants.COLUMN_DEFAULTS)
  74. this.attributes = ['Attribute', 'Type', 'Detail', 'Default', 'Example']
  75. }
  76. }
  77. class Methods extends API {
  78. init () {
  79. this.file = 'methods.md'
  80. this.options = Constants.METHODS
  81. this.attributes = ['Parameter', 'Detail', 'Example']
  82. }
  83. }
  84. class Events extends API {
  85. init () {
  86. this.file = 'events.md'
  87. this.options = Object.values(Constants.EVENTS)
  88. this.attributes = ['jQuery Event', 'Parameter', 'Detail']
  89. }
  90. }
  91. class Localizations extends API {
  92. init () {
  93. this.file = 'localizations.md'
  94. this.options = Object.keys(Constants.LOCALES.en)
  95. this.attributes = ['Parameter', 'Default']
  96. }
  97. }
  98. new TableOptions()
  99. new ColumnOptions()
  100. new Methods()
  101. new Events()
  102. new Localizations()
  103. if (errorSum === 0) {
  104. console.log('Good job! Anything up to date!')
  105. process.exit(0)
  106. }
  107. process.exit(1)