From 0ea5ca9873aab0a4b357de77c446a3c61a10da28 Mon Sep 17 00:00:00 2001 From: Marisa DeMeglio Date: Sat, 30 Dec 2017 18:52:22 -0800 Subject: [PATCH] fix(report): remove @path property from iframes and images 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 --- packages/ace-core/src/core/ace.js | 12 ++++---- packages/ace-report/src/report-builders.js | 6 ++++ packages/ace-report/src/report.js | 33 +++++++++++----------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/packages/ace-core/src/core/ace.js b/packages/ace-core/src/core/ace.js index 16d2846f..4c36ddb5 100644 --- a/packages/ace-core/src/core/ace.js +++ b/packages/ace-core/src/core/ace.js @@ -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.'); diff --git a/packages/ace-report/src/report-builders.js b/packages/ace-report/src/report-builders.js index b9586ffc..a09861af 100644 --- a/packages/ace-report/src/report-builders.js +++ b/packages/ace-report/src/report-builders.js @@ -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; diff --git a/packages/ace-report/src/report.js b/packages/ace-report/src/report.js index d9c27940..7c94dd32 100644 --- a/packages/ace-report/src/report.js +++ b/packages/ace-report/src/report.js @@ -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(); @@ -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) { @@ -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)); - }*/ } }