-
Notifications
You must be signed in to change notification settings - Fork 90
/
reporter.js
120 lines (100 loc) · 2.5 KB
/
reporter.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
const getFilesCountText = (count) => (count === 1 ? '1 file' : `${count} files`)
class Reporter {
#hasPrinted = false
#options
#status
#logger
constructor(files, options) {
this.#options = options
this.#status = {
matchedFilesCount: files.length,
failedFilesCount: 0,
wellSortedFilesCount: 0,
changedFilesCount: 0,
}
this.#logger = options.shouldBeQuiet
? { log() {}, error() {} }
: {
log: (...args) => {
this.#hasPrinted = true
console.log(...args)
},
error: (...args) => {
this.#hasPrinted = true
console.error(...args)
},
}
}
// The file is well-sorted
reportNotChanged(/* file */) {
this.#status.wellSortedFilesCount++
}
reportChanged(file) {
this.#status.changedFilesCount++
this.#logger.log(this.#options.isCheck ? `${file}` : `${file} is sorted!`)
}
reportFailed(file, error) {
this.#status.failedFilesCount++
console.error('Error on: ' + file)
this.#logger.error(error.message)
}
printSummary() {
const {
matchedFilesCount,
failedFilesCount,
changedFilesCount,
wellSortedFilesCount,
} = this.#status
if (matchedFilesCount === 0) {
console.error('No matching files.')
process.exitCode = 2
return
}
const { isCheck, isQuiet } = this.#options
if (isCheck && changedFilesCount) {
process.exitCode = 1
}
if (failedFilesCount) {
process.exitCode = 2
}
if (isQuiet) {
return
}
const { log } = this.#logger
// Print an empty line.
if (this.#hasPrinted) {
log()
}
// Matched files
log('Found %s.', getFilesCountText(matchedFilesCount))
// Failed files
if (failedFilesCount) {
log(
'%s could not be %s.',
getFilesCountText(failedFilesCount),
isCheck ? 'checked' : 'sorted',
)
}
// Changed files
if (changedFilesCount) {
if (isCheck) {
log(
'%s %s not sorted.',
getFilesCountText(changedFilesCount),
changedFilesCount === 1 ? 'was' : 'were',
)
} else {
log('%s successfully sorted.', getFilesCountText(changedFilesCount))
}
}
// Well-sorted files
if (wellSortedFilesCount) {
log(
'%s %s already sorted.',
getFilesCountText(wellSortedFilesCount),
wellSortedFilesCount === 1 ? 'was' : 'were',
)
}
}
}
export default Reporter