Skip to content

Commit

Permalink
fix(report): remove @path property from iframes and images
Browse files Browse the repository at this point in the history
The json data is scrubbed clean of @path properties after the images are
copied. If we aren't storing the report on disk, the json data is scrubbed
before it gets written to the console.

I'd rather separate out any data that needs to be copied and not have it
stored in the report in the first place, but that would involve some
redesign. Maybe a good idea for the future.

fs-extra's `writeFile` was causing concurrency issues, so it's been
replaced with `writeFileSync`.

Closes #64
  • Loading branch information
marisademeglio authored and rdeltour committed Jan 18, 2018
1 parent 016a3c0 commit 0ea5ca9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
12 changes: 7 additions & 5 deletions packages/ace-core/src/core/ace.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ module.exports = function ace(epubPath, options) {
// Process the Results
.then((report) => {
if (options.outdir === undefined) {
report.cleanData();
return process.stdout.write(JSON.stringify(report.json, null, ' '));
}
return Promise.all([
report.copyData(options.outdir),
report.saveJson(options.outdir),
report.saveHtml(options.outdir),
]);
return report.copyData(options.outdir)
.then(() => report.cleanData())
.then(() => Promise.all([
report.saveJson(options.outdir),
report.saveHtml(options.outdir)
]));
})
.then(() => {
winston.info('Done.');
Expand Down
6 changes: 6 additions & 0 deletions packages/ace-report/src/report-builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ class ReportBuilder {
build() {
return this._json;
}
// run the function on the given item under _json.data
cleanData(key, fn) {
if (this._json.data.hasOwnProperty(key)) {
this._json.data[key] = fn(this._json.data[key]);
}
}
setOutdir(outdir) {
this.outdir = outdir;
return this;
Expand Down
33 changes: 17 additions & 16 deletions packages/ace-report/src/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ module.exports = class Report {
this._builder.withProperties(properties);
return this;
}
// remove the path properties as they aren't needed
// this has to happen after data is copied, if we're copying it
cleanData() {
winston.debug("Cleaning data");
var removePath = function(items) {
return items.map((item) => {
delete item.path;
return item;
});
};
this._builder.cleanData('images', removePath);
this._builder.cleanData('iframes', removePath);

return Promise.resolve();
}
copyData(outdir) {
winston.info("Copying data");
if (this.json.data === null) return Promise.resolve();
Expand All @@ -86,7 +101,6 @@ module.exports = class Report {
return Promise.all(this.json.data.images.map((img) => {
const fromPath = img.path;
const toPath = path.join(outdir, 'data', img.src);
delete img.path;
return fs.pathExists(fromPath)
.then((exists) => {
if (exists) {
Expand All @@ -111,27 +125,14 @@ module.exports = class Report {
return aceReport;
})
.then((aceReport) => Promise.all([
fs.writeFile(path.join(outdir, 'report.json'), aceReport, 'UTF-8'),
fs.writeFileSync(path.join(outdir, 'report.json'), aceReport, 'UTF-8'),
]));
}
saveHtml(outdir) {
winston.info("Saving HTML report");

generateHtmlReport(this.json)
.then((result) => fs.writeFile(path.join(outdir, 'report.html'), result, 'UTF-8'))
.then((result) => fs.writeFileSync(path.join(outdir, 'report.html'), result, 'UTF-8'))
.catch(err => winston.error(err));


// create a js file that the html report uses as its data source
/*const aceReport = JSON.stringify(this.json, null, ' ');
const js = "const aceReportData = " + aceReport + ";";
// copy report.html and the contents of /js and /css to the outdir
return fs.copy(path.join(__dirname, 'resources/report.html'), path.join(outdir, "report.html"))
//.then(() => fs.copy(path.join(__dirname, './resources/css/'), path.join(outdir, "css/")))
.then(() => fs.copy(path.join(__dirname, './resources/js/'), path.join(outdir, "js/")))
.then(() => fs.writeFile(path.join(outdir, 'js/', 'aceReportData.js'), js, 'UTF-8'))
.catch(err => winston.error(err));
}*/
}
}

0 comments on commit 0ea5ca9

Please sign in to comment.