check-api.js 2.6 KB

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