testem
Version:
Test'em 'scripts! Javascript Unit testing made easy.
42 lines (31 loc) • 969 B
JavaScript
;
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const PassThrough = require('stream').PassThrough;
const Bluebird = require('bluebird');
module.exports = class ReportFile {
constructor(reportFile) {
this.file = reportFile;
this.outputStream = new PassThrough();
mkdirp.sync(path.dirname(path.resolve(reportFile)));
this.outputStream = fs.createWriteStream(reportFile, { flags: 'w+' });
let alreadyEnded = false;
function finish(data) {
if (!alreadyEnded) {
alreadyEnded = true;
this.outputStream.end(data);
}
}
this.outputStream.on('end', finish);
this.outputStream.on('error', finish);
this.closePromise = new Bluebird.Promise((resolve, reject) => {
this.outputStream.on('finish', resolve);
this.outputStream.on('error', reject);
});
}
close() {
this.outputStream.end();
return this.closePromise;
}
};