check-api.js 2.9 KB

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