forked from wenzhixin/bootstrap-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-api.js
167 lines (145 loc) · 4.88 KB
/
check-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// eslint-disable-next-line no-global-assign
require = require('esm')(module)
const fs = require('fs')
const chalk = require('chalk')
const Constants = require('../src/constants/index.js').default
let errorSum = 0
const exampleFilesFolder = './bootstrap-table-examples/'
const exampleFilesFound = fs.existsSync(exampleFilesFolder)
let exampleFiles = []
if (exampleFilesFound) {
exampleFiles = [
...fs.readdirSync(exampleFilesFolder + 'welcomes'),
...fs.readdirSync(exampleFilesFolder + 'options'),
...fs.readdirSync(exampleFilesFolder + 'column-options'),
...fs.readdirSync(exampleFilesFolder + 'methods')
]
} else {
console.log((chalk.yellow(chalk.bold('Warning: ') + 'Cant check if example files are correct formatted and have a valid url.')))
console.log((chalk.yellow(chalk.bold('Warning: ') + 'To enable that check, please clone the "bootstrap-table-examples" repository in the tools folder or create a symlink (if you already cloned the repository on an other path).')))
}
class API {
constructor () {
this.init()
this.sortOptions()
this.check()
}
sortOptions () {
this.options.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
}
check () {
const file = `../site/docs/api/${this.file}`
const md = {}
const content = fs.readFileSync(file).toString()
const lines = content.split('## ')
const outLines = lines.slice(0, 1)
const errors = []
const exampleRegex = /\[.*\]\(.*\/(.*\.html)\)/m
const attributeRegex = /\*\*Attribute:\*\*\s`(.*)data-(.*)`/m
for (const item of lines.slice(1)) {
md[item.split('\n')[0]] = item
}
const mds = Object.keys(md)
for (const [i, key] of this.options.entries()) {
try {
if (md[key]) {
outLines.push(md[key])
const details = md[key].split('\n\n- ')
for (let i = 0; i < this.attributes.length; i++) {
const name = this.attributes[i]
if (this.ignore && this.ignore[key] && this.ignore[key].includes(name)) {
continue
}
const tmpDetails = details[i + 1].trim()
if (name === 'Example' && exampleFilesFound) {
const matches = exampleRegex.exec(tmpDetails)
if (!matches) {
errors.push(chalk.red(`[${key}] missing or wrong formatted example`, `"${tmpDetails}"`))
continue
}
if (!exampleFiles.includes(matches[1])) {
errors.push(chalk.red(`[${key}] example '${matches[1]}' could not be found`))
}
} else if (name === 'Attribute' && key !== 'columns') {
const attributeMatches = attributeRegex.exec(tmpDetails)
if (!attributeMatches) {
errors.push(chalk.red(`[${key}] missing or wrong formatted attribute`, `"${tmpDetails}"`))
continue
}
}
if (!tmpDetails || tmpDetails.indexOf(`**${name}:**`) === -1) {
errors.push(chalk.red(`[${key}] missing '${name}'`))
}
}
} else {
errors.push(chalk.red(`[${key}] option could not be found`))
}
} catch (ex) {
console.log(ex)
}
}
errorSum += errors.length
if (errors.length > 0) {
console.log('-------------------------')
console.log(`Checking file: ${file}`)
console.log('-------------------------')
errors.forEach((error) => {
console.log(error)
})
}
fs.writeFileSync(file, outLines.join('## '))
}
}
class TableOptions extends API {
init () {
this.file = 'table-options.md'
this.options = Object.keys(Constants.DEFAULTS).filter(it => {
return !/^(on|format)[A-Z]/.test(it)
})
this.options.unshift('-')
this.attributes = ['Attribute', 'Type', 'Detail', 'Default', 'Example']
this.ignore = {
totalRows: ['Example'],
totalNotFiltered: ['Example'],
virtualScrollItemHeight: ['Example']
}
}
}
class ColumnOptions extends API {
init () {
this.file = 'column-options.md'
this.options = Object.keys(Constants.COLUMN_DEFAULTS)
this.attributes = ['Attribute', 'Type', 'Detail', 'Default', 'Example']
}
}
class Methods extends API {
init () {
this.file = 'methods.md'
this.options = Constants.METHODS
this.attributes = ['Parameter', 'Detail', 'Example']
}
}
class Events extends API {
init () {
this.file = 'events.md'
this.options = Object.values(Constants.EVENTS)
this.attributes = ['jQuery Event', 'Parameter', 'Detail']
}
}
class Localizations extends API {
init () {
this.file = 'localizations.md'
this.options = Object.keys(Constants.LOCALES.en)
this.attributes = ['Parameter', 'Default']
}
}
new TableOptions()
new ColumnOptions()
new Methods()
new Events()
new Localizations()
if (errorSum === 0) {
console.log('Good job! Anything up to date!')
process.exit(0)
}
process.exit(1)