-
-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathformatter.ts
113 lines (92 loc) · 2.79 KB
/
formatter.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
import * as chalk from 'chalk'
import { EventEmitter } from 'events'
import { sync as globSync } from 'glob'
import { parse, resolve } from 'path'
import type { HTMLHint as IHTMLHint } from '../core/core'
import type { Configuration, Hint } from '../core/types'
let HTMLHint: typeof IHTMLHint
let options: { nocolor?: boolean }
// load formatters
const mapFormatters = loadFormatters()
const arrSupportedFormatters: string[] = []
for (const formatterName in mapFormatters) {
if (formatterName !== 'default') {
arrSupportedFormatters.push(formatterName)
}
}
// load all formatters
function loadFormatters() {
const arrFiles = globSync('./formatters/*.js', {
cwd: __dirname,
dot: false,
nodir: true,
strict: false,
silent: true,
})
const mapFormatters: { [name: string]: FormatterCallback } = {}
arrFiles.forEach((file) => {
const fileInfo = parse(file)
const formatterPath = resolve(__dirname, file)
mapFormatters[fileInfo.name] = require(formatterPath)
})
return mapFormatters
}
export interface FormatterFileEvent {
file: string
messages: Hint[]
time: number
}
export interface FormatterConfigEvent {
config?: Configuration
configPath?: string
}
export interface FormatterEndEvent {
arrAllMessages: {
file: string
messages: Hint[]
time: number
}[]
allFileCount: number
allHintFileCount: number
allHintCount: number
time: number
}
export interface Formatter extends EventEmitter {
getSupported(): typeof arrSupportedFormatters
init(tmpHTMLHint: typeof IHTMLHint, tmpOptions: { nocolor?: boolean }): void
setFormat(format: string): void
emit(event: 'start'): boolean
on(event: 'start', listener: () => void): this
emit(event: 'file', arg: FormatterFileEvent): boolean
on(event: 'file', listener: (event: FormatterFileEvent) => void): this
emit(event: 'config', arg: FormatterConfigEvent): boolean
on(event: 'config', listener: (event: FormatterConfigEvent) => void): this
emit(event: 'end', arg: FormatterEndEvent): boolean
on(event: 'end', listener: (event: FormatterEndEvent) => void): this
}
const formatter: Formatter = new EventEmitter() as Formatter
formatter.getSupported = function () {
return arrSupportedFormatters
}
formatter.init = function (tmpHTMLHint, tmpOptions) {
HTMLHint = tmpHTMLHint
options = tmpOptions
}
formatter.setFormat = function (format) {
const formatHandel = mapFormatters[format]
if (formatHandel === undefined) {
console.log(
chalk.red('No supported formatter, supported formatters: %s'),
arrSupportedFormatters.join(', ')
)
process.exit(1)
} else {
formatHandel(formatter, HTMLHint, options)
}
}
export type FormatterCallback = (
formatter: Formatter,
HTMLHint: typeof IHTMLHint,
options: { nocolor?: boolean }
) => void
module.exports = formatter