Skip to content

Commit

Permalink
add a way to download other format reports
Browse files Browse the repository at this point in the history
  • Loading branch information
RoCat committed Oct 21, 2016
1 parent d09c92e commit 5ac9ab1
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions lib/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var path = require('path'),
fs = require('fs'),
core = require('./core'),
istanbul = require('istanbul'),
FileWriter = istanbul.FileWriter,
bodyParser = require('body-parser'),
ASSETS_DIR = istanbul.assetsDir,
existsSync = fs.existsSync || path.existsSync,
Expand Down Expand Up @@ -114,6 +115,51 @@ function createHandler(opts) {
writer.done();
});

app.get('/downloadFormat', function (req, res) {
var
availlableFormat = ['json', 'cobertura', 'clover', 'json-summary', 'lcovonly'],
writer = new FileWriter({sync: true}),
coverageObject = core.getCoverageObject() || {},
collector = new Collector(),
baseDir = opts.tmpDir || process.cwd(),
format = req.query.format || 'json',
file,
report;
if(availlableFormat.indexOf(format) === -1){
res.statusCode = '403';
return res.send();
}
report = Report.create(format,
{
writer: writer,
dir: baseDir
});

utils.removeDerivedInfo(coverageObject);
collector.add(coverageObject);

res.statusCode = 200;
res.setHeader('Content-type', 'application/octet-stream');
report.writeReport(collector);

if(report.file){
file = report.file;
} else if (report.opts && report.opts.file){
file = report.opts.file;
}
if(!file){
res.statusCode = '403';
return res.send();
}
var filePath = path.join(baseDir, file);
var stat = fs.statSync(filePath);
res.setHeader('Content-Disposition', 'attachment; filename='+file);
res.setHeader('Content-Length', stat.size);
fs.createReadStream(filePath).pipe(res).on('finish', function(){
fs.unlink(filePath);
});
});

//merge client coverage posted from browser
app.post('/client', function (req, res) {
var body = req.body;
Expand Down

0 comments on commit 5ac9ab1

Please sign in to comment.