-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcli.js
61 lines (52 loc) · 1.63 KB
/
cli.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
#!/usr/bin/env node
const yargs = require('yargs')
const path = require('path')
const fs = require('fs')
const express = require('express')
const pkg = require('./package.json')
const coverageViewer = require('./')
const cli = yargs
.usage('\nUsage: coverage-viewer <coverageFile> [options]')
.example('coverage-viewer coverage.json -s ./src -o ./coverage')
.describe('s', "The root of your project's source code directory")
.describe('o', 'Where coverage-viewer should write output')
.describe('u', 'Whether to start the express viewing server')
.alias('u', 'up')
.demandOption(['s'])
.help('help')
.alias('h', 'help')
.version('v', pkg.version)
.alias('v', 'version')
const options = {
coverageFile: path.resolve(cli.argv._[0]),
sourceRoot: path.resolve(cli.argv.s),
outputFolder: path.resolve(cli.argv.o)
}
coverageViewer.render(options)
if (cli.argv.u) {
// should watch for updates to the coverage file, and run the generator again
// if any updates occur
fs.watch(options.coverageFile, (eventType, filename) => {
if (filename) {
console.log(`Changed: ${filename}`)
}
coverageViewer.render(options)
})
// start express app
const app = express()
app.get('/', (req, res) => {
res.send(
fs.readFileSync(path.join(options.outputFolder, 'index.html'), 'utf8')
)
})
// catch the request for a favicon
app.get('/favicon.ico', (req, res) => {
res.status(204)
})
app.get('*', (req, res) => {
res.send(fs.readFileSync(path.join(options.outputFolder, req.url), 'utf8'))
})
app.listen(3000, () =>
console.log('coverage-viewer listening at http://localhost:3000/')
)
}