-
-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathhtmlhint.ts
510 lines (447 loc) · 11.6 KB
/
htmlhint.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#!/usr/bin/env node
import { queue as asyncQueue, series as asyncSeries } from 'async'
import * as chalk from 'chalk'
import * as program from 'commander'
import { existsSync, readFileSync, statSync } from 'fs'
import * as glob from 'glob'
import { IGlob } from 'glob'
import * as parseGlob from 'parse-glob'
import { dirname, resolve, sep } from 'path'
import * as request from 'request'
import * as stripJsonComments from 'strip-json-comments'
import type { HTMLHint as IHTMLHint } from '../core/core'
import type { Configuration, Hint } from '../core/types'
import { Formatter } from './formatter'
const HTMLHint: typeof IHTMLHint = require('../htmlhint.js').HTMLHint
const formatter: Formatter = require('./formatter')
const pkg = require('../../package.json')
function map(val: string) {
const objMap: { [name: string]: string | true } = {}
val.split(',').forEach((item) => {
const arrItem = item.split(/\s*=\s*/)
objMap[arrItem[0]] = arrItem[1] ? arrItem[1] : true
})
return objMap
}
program.on('--help', () => {
console.log(' Examples:')
console.log('')
console.log(' htmlhint')
console.log(' htmlhint www')
console.log(' htmlhint www/test.html')
console.log(' htmlhint www/**/*.xhtml')
console.log(' htmlhint www/**/*.{htm,html}')
console.log(' htmlhint http://www.alibaba.com/')
console.log(' cat test.html | htmlhint stdin')
console.log(' htmlhint --list')
console.log(
' htmlhint --rules tag-pair,id-class-value=underline test.html'
)
console.log(' htmlhint --config .htmlhintrc test.html')
console.log(' htmlhint --ignore **/build/**,**/test/**')
console.log(' htmlhint --rulesdir ./rules/')
console.log('')
})
const arrSupportedFormatters = formatter.getSupported()
program
.version(pkg.version)
.usage('<file|folder|pattern|stdin|url ...> [options]')
.option('-l, --list', 'show all of the rules available')
.option('-c, --config <file>', 'custom configuration file')
.option(
'-r, --rules <ruleid, ruleid=value ...>',
'set all of the rules available',
map
)
.option(
'-R, --rulesdir <file|folder>',
'load custom rules from file or folder'
)
.option(
`-f, --format <${arrSupportedFormatters.join('|')}>`,
'output messages as custom format'
)
.option(
'-i, --ignore <pattern, pattern ...>',
'add pattern to exclude matches'
)
.option('--nocolor', 'disable color')
.option('--warn', 'Warn only, exit with 0')
.parse(process.argv)
if (program.list) {
listRules()
process.exit(0)
}
const arrTargets = program.args
if (arrTargets.length === 0) {
arrTargets.push('./')
}
// init formatter
formatter.init(HTMLHint, {
nocolor: program.nocolor,
})
const format = program.format || 'default'
if (format) {
formatter.setFormat(format)
}
// TODO: parse and validate `program.rules`
hintTargets(arrTargets, {
rulesdir: program.rulesdir,
config: program.rules ? { rules: program.rules } : undefined,
formatter: formatter,
ignore: program.ignore,
})
// list all rules
function listRules() {
const rules = HTMLHint.rules
let rule
console.log(' All rules:')
console.log(' ==================================================')
for (const id in rules) {
rule = rules[id]
console.log(' %s : %s', chalk.bold(rule.id), rule.description)
}
}
function hintTargets(
arrTargets: string[],
options: {
formatter: Formatter
config?: Configuration
rulesdir?: string
ignore?: string
}
) {
let arrAllMessages: Array<{
file: string
messages: Hint[]
time: number
}> = []
let allFileCount = 0
let allHintFileCount = 0
let allHintCount = 0
const startTime = new Date().getTime()
const formatter = options.formatter
// load custom rules
const rulesdir = options.rulesdir
if (rulesdir) {
loadCustomRules(rulesdir)
}
// start hint
formatter.emit('start')
const arrTasks: Array<(next: () => void) => void> = []
arrTargets.forEach((target) => {
arrTasks.push((next) => {
hintAllFiles(target, options, (result) => {
allFileCount += result.targetFileCount
allHintFileCount += result.targetHintFileCount
allHintCount += result.targetHintCount
arrAllMessages = arrAllMessages.concat(result.arrTargetMessages)
next()
})
})
})
asyncSeries(arrTasks, () => {
// end hint
const spendTime = new Date().getTime() - startTime
formatter.emit('end', {
arrAllMessages: arrAllMessages,
allFileCount: allFileCount,
allHintFileCount: allHintFileCount,
allHintCount: allHintCount,
time: spendTime,
})
process.exit(!program.warn && allHintCount > 0 ? 1 : 0)
})
}
// load custom rles
function loadCustomRules(rulesdir: string) {
rulesdir = rulesdir.replace(/\\/g, '/')
if (existsSync(rulesdir)) {
if (statSync(rulesdir).isDirectory()) {
rulesdir += /\/$/.test(rulesdir) ? '' : '/'
rulesdir += '**/*.js'
const arrFiles = glob.sync(rulesdir, {
dot: false,
nodir: true,
strict: false,
silent: true,
})
arrFiles.forEach((file) => {
loadRule(file)
})
} else {
loadRule(rulesdir)
}
}
}
// load rule
function loadRule(filepath: string) {
filepath = resolve(filepath)
try {
const module = require(filepath)
module(HTMLHint)
} catch (e) {
// ignore
}
}
// hint all files
function hintAllFiles(
target: string,
options: {
ignore?: string
formatter: Formatter
config?: Configuration
},
onFinised: (result: {
targetFileCount: number
targetHintFileCount: number
targetHintCount: number
arrTargetMessages: Array<{
file: string
messages: Hint[]
time: number
}>
}) => void
) {
const globInfo = getGlobInfo(target)
globInfo.ignore = options.ignore
const formatter = options.formatter
// hint result
let targetFileCount = 0
let targetHintFileCount = 0
let targetHintCount = 0
const arrTargetMessages: Array<{
file: string
messages: Hint[]
time: number
}> = []
// init config
let config = options.config
if (config === undefined) {
config = getConfig(program.config, globInfo.base, formatter)
}
// hint queue
const hintQueue = asyncQueue<string>((filepath, next) => {
const startTime = new Date().getTime()
if (filepath === 'stdin') {
hintStdin(config, hintNext)
} else if (/^https?:\/\//.test(filepath)) {
hintUrl(filepath, config, hintNext)
} else {
const messages = hintFile(filepath, config)
hintNext(messages)
}
function hintNext(messages: Hint[]) {
const spendTime = new Date().getTime() - startTime
const hintCount = messages.length
if (hintCount > 0) {
formatter.emit('file', {
file: filepath,
messages: messages,
time: spendTime,
})
arrTargetMessages.push({
file: filepath,
messages: messages,
time: spendTime,
})
targetHintFileCount++
targetHintCount += hintCount
}
targetFileCount++
setImmediate(next)
}
}, 10)
// start hint
let isWalkDone = false
let isHintDone = true
hintQueue.drain(() => {
isHintDone = true
checkAllHinted()
})
function checkAllHinted() {
if (isWalkDone && isHintDone) {
onFinised({
targetFileCount: targetFileCount,
targetHintFileCount: targetHintFileCount,
targetHintCount: targetHintCount,
arrTargetMessages: arrTargetMessages,
})
}
}
if (target === 'stdin') {
isWalkDone = true
hintQueue.push(target)
} else if (/^https?:\/\//.test(target)) {
isWalkDone = true
hintQueue.push(target)
} else {
walkPath(
globInfo,
(filepath) => {
isHintDone = false
hintQueue.push(filepath)
},
() => {
isWalkDone = true
checkAllHinted()
}
)
}
}
// split target to base & glob
function getGlobInfo(
target: string
): {
base: string
pattern: string
ignore?: string
} {
// fix windows sep
target = target.replace(/\\/g, '/')
const globInfo = parseGlob(target)
let base = resolve(globInfo.base)
base += /\/$/.test(base) ? '' : '/'
let pattern = globInfo.glob
const globPath = globInfo.path
const defaultGlob = '*.{htm,html}'
if (globInfo.is.glob === true) {
// no basename
if (globPath.basename === '') {
pattern += defaultGlob
}
} else {
// no basename
if (globPath.basename === '') {
pattern += `**/${defaultGlob}`
}
// detect directory
else if (existsSync(target) && statSync(target).isDirectory()) {
base += `${globPath.basename}/`
pattern = `**/${defaultGlob}`
}
}
return {
base: base,
pattern: pattern,
}
}
// search and load config
function getConfig(
configPath: string | undefined,
base: string,
formatter: Formatter
): Configuration | undefined {
if (configPath === undefined && existsSync(base)) {
// find default config file in parent directory
if (statSync(base).isDirectory() === false) {
base = dirname(base)
}
while (base) {
// TODO: load via cosmiconfig (https://github.com/htmlhint/HTMLHint/issues/126)
const tmpConfigFile = resolve(base, '.htmlhintrc')
if (existsSync(tmpConfigFile)) {
configPath = tmpConfigFile
break
}
if (!base) {
break
}
base = base.substring(0, base.lastIndexOf(sep))
}
}
// TODO: can configPath be undefined here?
if (configPath !== undefined && existsSync(configPath)) {
const configContent = readFileSync(configPath, 'utf-8')
let config: Configuration | undefined
try {
config = JSON.parse(stripJsonComments(configContent))
formatter.emit('config', {
configPath,
config,
})
} catch (e) {
// ignore
}
// TODO: validate config
return config
}
}
// walk path
function walkPath(
globInfo: { base: string; pattern: string; ignore?: string },
callback: (filepath: string) => void,
onFinish: () => void
) {
let base: string = globInfo.base
const pattern = globInfo.pattern
const ignore: string | undefined = globInfo.ignore
const arrIgnores = ['**/node_modules/**']
if (ignore) {
ignore.split(',').forEach((pattern) => {
arrIgnores.push(pattern)
})
}
const walk: IGlob = glob(
pattern,
{
cwd: base,
dot: false,
ignore: arrIgnores,
nodir: true,
strict: false,
silent: true,
},
() => {
onFinish()
}
)
walk.on('match', (file: string) => {
base = base.replace(/^.\//, '')
if (sep !== '/') {
base = base.replace(/\//g, sep)
}
callback(base + file)
})
}
// hint file
function hintFile(filepath: string, config?: Configuration) {
let content = ''
try {
content = readFileSync(filepath, 'utf-8')
} catch (e) {
// ignore
}
return HTMLHint.verify(content, config)
}
// hint stdin
function hintStdin(
config: Configuration | undefined,
callback: (messages: Hint[]) => void
) {
process.stdin.setEncoding('utf8')
const buffers: string[] = []
process.stdin.on('data', (text) => {
buffers.push(text)
})
process.stdin.on('end', () => {
const content = buffers.join('')
const messages = HTMLHint.verify(content, config)
callback(messages)
})
}
// hint url
function hintUrl(
url: string,
config: Configuration | undefined,
callback: (messages: Hint[]) => void
) {
request.get(url, (error, response, body) => {
if (!error && response.statusCode == 200) {
const messages = HTMLHint.verify(body, config)
callback(messages)
} else {
callback([])
}
})
}