check-api.js 2.5 KB

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