-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.coffee
84 lines (63 loc) · 2.42 KB
/
server.coffee
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
path = require 'path'
fs = require 'fs'
http = require 'http'
express = require 'express'
logger = require 'morgan'
mime = require 'mime'
async = require 'async'
XUnitXML = require './lib/xunit_xml'
MochaXUnitTestSuite = require './lib/mocha_xunit_test_suite'
module.exports = (options) ->
app = require('express')()
app.set 'app name', options.name || 'xunit-reports-viewer'
app.set 'view engine', 'hbs'
app.set 'views', path.join(__dirname, 'views')
app.use logger 'dev'
app.use express.static path.join(__dirname, 'public')
app.set 'port', parseInt(options.port || 2042)
if not options.dir
console.log "Reports directory is not defined."
process.exit 1
app.set 'reports dir', options.dir
if not fs.existsSync app.get 'reports dir'
console.log "Reports directory '#{app.get 'reports dir'}' does not exists."
process.exit 2
else
console.log "Using reports dir: #{app.get 'reports dir'}"
app.locals.name = app.get 'app name'
app.get '/', (req, res) ->
res.redirect '/reports'
app.get '/reports', (req, res, next) ->
fs.readdir app.get('reports dir'), (error, files) ->
if error then return next error
files = files.filter (fileName) ->
mime.lookup(path.join(app.get('reports dir'), fileName)) is 'application/xml'
xmls = files.map (fileName) -> new XUnitXML app.get('reports dir') + '/' + fileName
async.map xmls, ((xml, next) ->
xml.getSuits (error, suit) ->
next null, if error then null else suit
), (error, suits) ->
if error then return next error
suits = [].concat.apply [], suits
results = suits.filter (result) -> !!result
results = suits.sort (a, b) ->
if a.timestamp > b.timestamp
return 1
if a.timestamp < b.timestamp
return -1
return 0
res.render 'testSuitsList',
list: suits
app.get '/reports/:name', (req, res, next) ->
reportFileName = "#{app.get 'reports dir'}/#{req.params.name}.xml"
if not fs.existsSync reportFileName
res.status 404
res.end "Report '#{reportFileName}' was not found", 404
return
xml = new XUnitXML reportFileName
xml.getSuits (error, suits) ->
if error then return next error
res.render 'testSuite',
testSuits: suits
http.createServer(app).listen app.get('port'), () ->
console.log "Running server on http://localhost:#{app.get('port')} ..."