-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·107 lines (95 loc) · 2.68 KB
/
index.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
#! /usr/bin/env node
'use strict'
const minimist = require('minimist')
const fs = require('fs')
const path = require('path')
const buildReport = require('./template')
const help = fs.readFileSync(path.join(__dirname, 'help.txt'), 'utf8')
const ndjson = require('ndjson')
const steed = require('steed')
function start () {
const argv = minimist(process.argv.slice(2), {
boolean: ['help'],
alias: {
input: 'i',
compare: 'c',
version: 'v',
help: 'h'
},
default: {
}
})
if (argv.version) {
console.log('autocannon-reporter', 'v' + require('./package').version)
console.log('node', process.version)
return
}
if (argv.help) {
console.error(help)
return
}
if (argv.compare) {
argv._.push(argv.compare)
argv.compare = argv._
}
argv.outputPath = path.join(process.cwd(), 'report.html')
argv.outputPathFolder = path.dirname(argv.outputPath)
if (process.stdin.isTTY) {
if (!argv.input) {
console.error('input (-i) is required when not piping into this')
console.error(help)
return
}
argv.inputPath = path.isAbsolute(argv.input) ? argv.input : path.join(process.cwd(), argv.input)
argv.compare = argv.compare || []
steed.map(argv.compare, (val, cb) => {
val = path.isAbsolute(val) ? val : path.join(process.cwd(), val)
fs.access(val, fs.F_OK, function (err) {
if (err) return cb(new Error('Can\'t access ' + val))
cb(null, require(val))
})
}, (err, compare) => {
if (err) return console.log(err)
compare = sort(compare)
const results = require(argv.inputPath)
const report = buildReport(results, compare)
writeReport(report, argv.outputPath, (err) => {
if (err) console.err('Error writting report: ', err)
else console.log('Report written to: ', argv.outputPath)
})
})
} else {
let compare = []
process.stdin
.pipe(ndjson.parse())
.on('data', (json) => { compare.push(json) })
.on('finish', () => {
compare = sort(compare)
const report = buildReport(compare[0], compare)
writeReport(report, argv.outputPath, (err) => {
if (err) console.err('Error writting report: ', err)
else console.log('Report written to: ', argv.outputPath)
})
})
}
}
function writeReport (report, path, cb) {
fs.writeFile(path, report, cb)
}
module.exports.buildReport = buildReport
module.exports.writeReport = writeReport
if (require.main === module) {
start()
}
function sort (array) {
array.sort(function (a, b) {
if (a.finish < b.finish) {
return 1
}
if (a.finish > b.finish) {
return -1
}
return 0
})
return array
}