Permalink
Browse files

Remove mkdirs from reporter and make it a file-writer problem, enhanc…

…e filewriter interface to copy files
  • Loading branch information...
1 parent 10fc72d commit e1806b7aabb04e55e97edf8bd8a69a0df3ab848d @gotwarlost gotwarlost committed Nov 5, 2012
Showing with 12 additions and 9 deletions.
  1. +1 −5 lib/report/html.js
  2. +11 −4 lib/util/file-writer.js
View
@@ -9,7 +9,6 @@ var handlebars = require('handlebars'),
SEP = path.sep || '/',
fs = require('fs'),
util = require('util'),
- mkdirp = require('mkdirp'),
FileWriter = require('../util/file-writer'),
Report = require('./index'),
Store = require('../store'),
@@ -413,7 +412,6 @@ Report.mix(HtmlReport, {
var that = this,
indexFile = path.resolve(dir, 'index.html'),
childFile;
- mkdirp.sync(dir);
templateData.entity = node.name || 'All files';
if (this.opts.verbose) { console.error('Writing ' + indexFile); }
writer.writeFile(indexFile, function () {
@@ -490,13 +488,11 @@ Report.mix(HtmlReport, {
summarizer.addFileCoverageSummary(key, utils.summarizeFileCoverage(collector.fileCoverageFor(key)));
});
tree = summarizer.getTreeSummary();
- //copy assets to root
- mkdirp.sync(dir);
fs.readdirSync(path.resolve(__dirname, '..', 'vendor')).forEach(function (f) {
if (that.opts.verbose) {
console.log('Write asset: ' + path.resolve(dir, f));
}
- fs.writeFileSync(path.resolve(dir, f), fs.readFileSync(path.resolve(__dirname, '..', 'vendor', f)));
+ writer.copyFile(path.resolve(__dirname, '..', 'vendor', f), path.resolve(dir, f));
});
//console.log(JSON.stringify(tree.root, undefined, 4));
this.writeFiles(writer, tree.root, dir, linkMapper, { datetime: dt }, collector);
View
@@ -3,7 +3,9 @@
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
-var fs = require('fs');
+var path = require('path'),
+ fs = require('fs'),
+ mkdirp = require('mkdirp');
function FileWriter(sync) {
this.writing = false;
@@ -13,6 +15,10 @@ function FileWriter(sync) {
}
FileWriter.prototype = {
+ copyFile: function (source, dest) {
+ mkdirp.sync(path.dirname(dest));
+ fs.writeFileSync(dest, fs.readFileSync(source));
+ },
writeFile: function (file, callback) {
this.start(file);
callback(this);
@@ -23,7 +29,7 @@ FileWriter.prototype = {
this.write('\n');
},
startSync: function (fileName) {
- this.doStart();
+ this.doStart(fileName);
this.contents = '';
this.filename = fileName;
},
@@ -35,7 +41,7 @@ FileWriter.prototype = {
fs.writeFileSync(this.filename, this.contents, 'utf8');
},
startAsync: function (fileName) {
- this.doStart();
+ this.doStart(fileName);
this.stream = fs.createWriteStream(fileName);
},
writeAsync: function (str) {
@@ -45,9 +51,10 @@ FileWriter.prototype = {
this.doEnd();
this.stream.end();
},
- doStart: function () {
+ doStart: function (fileName) {
if (this.writing) { throw new Error('Attempt to start a new file before ending the previous one'); }
this.writing = true;
+ mkdirp.sync(path.dirname(fileName));
},
doEnd : function () {
if (!this.writing) { throw new Error('Attempt to end a file without starting it'); }

0 comments on commit e1806b7

Please sign in to comment.