check-api.js 2.4 KB

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