forked from taskworld/eslint-plugin-progress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateProgressReporter.js
More file actions
57 lines (57 loc) · 1.58 KB
/
createProgressReporter.js
File metadata and controls
57 lines (57 loc) · 1.58 KB
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
/* eslint no-console: off */
module.exports = function createProgressReporter(options) {
let lastReported = 0
let lastFile
let shouldHookExit = options && options.hookExit
const stats = []
const eslintPlugin = {
rules: {
activate: {
create(context) {
if (shouldHookExit) {
shouldHookExit = false
process.on('exit', printStats)
}
const now = Date.now()
if (now > lastReported + 15000) {
lastReported = now
console.error(
`* [${new Date().toJSON()}] Processed ${stats.length} files...`
)
}
if (lastFile) {
lastFile.finish = now
lastFile.duration = now - lastFile.start
stats.push(lastFile)
}
lastFile = {
name: context.getFilename(),
start: now
}
return {}
}
}
}
}
function printStats() {
const totalTime = stats.map(s => s.duration).reduce((a, b) => a + b, 0)
const minutes = (totalTime / 60000).toFixed(1)
console.log()
console.log('ESLint Stats Report')
console.log('===================')
console.log()
console.log(`${stats.length} files processed in ${minutes} minutes.`)
stats.sort((a, b) => b.duration - a.duration)
console.log()
const slow = stats.slice(0, 20)
console.log(`## Slowest ${slow.length} files`)
for (const file of stats.slice(0, 20)) {
console.log(` * ${file.name} (${file.duration} ms)`)
}
}
return {
eslintPlugin,
printStats,
stats
}
}