From cbc33b4ad4a07ed0b62268b744c643b95b4100cb Mon Sep 17 00:00:00 2001 From: Inga Lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Wed, 1 Feb 2017 17:30:24 +0300 Subject: [PATCH] Slight refactoring + optimizations --- BuildServer/lib/report-processor.js | 12 ++++++++++-- BuildServer/lib/task-processor.js | 4 +++- BuildServer/lib/tasks/index.js | 10 ++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 BuildServer/lib/tasks/index.js diff --git a/BuildServer/lib/report-processor.js b/BuildServer/lib/report-processor.js index c915e3e..14e5eae 100644 --- a/BuildServer/lib/report-processor.js +++ b/BuildServer/lib/report-processor.js @@ -11,12 +11,16 @@ const reportFilename = "report.json.gz"; const writeReport = (releaseDir, err, result, callback) => { const readable = new streamBuffers.ReadableStreamBuffer(); + const writeStream = fs.createWriteStream(path.join(releaseDir, reportFilename)); readable .pipe(zlib.createGzip()) - .pipe(fs.createWriteStream(path.join(releaseDir, reportFilename))) + .pipe(writeStream) .on("error", callback) - .on("finish", callback); + .on("finish", () => { + writeStream.close(); + callback(); + }); readable.put(JSON.stringify({ "date": Date.now(), @@ -35,6 +39,8 @@ const readReport = (releaseDir, callback) => { .pipe(writable) .on("error", callback) .on("finish", () => { + readStream.destroy(); + const data = writable.getContentsAsString(); if (!data) { @@ -45,6 +51,8 @@ const readReport = (releaseDir, callback) => { }); }; +exports.readReport = readReport; + exports.writeReport = writeReport; exports.loadReport = (app, options, callback) => { diff --git a/BuildServer/lib/task-processor.js b/BuildServer/lib/task-processor.js index e29cc29..fa6a84b 100644 --- a/BuildServer/lib/task-processor.js +++ b/BuildServer/lib/task-processor.js @@ -1,5 +1,7 @@ "use strict"; +const tasks = require("./tasks"); + // TaskProcessor does not look like EventEmitter, so no need to extend EventEmitter and use `emit' here. const TaskProcessor = function (task, outerProcessor, callback) { if (!this) { @@ -38,7 +40,7 @@ const TaskProcessor = function (task, outerProcessor, callback) { that.done = done; that.context = outerProcessor.context; - const taskImpl = require(`./tasks/${task.type.match(/[\w\-]/g).join("")}`); + const taskImpl = tasks[task.type]; taskWorker = taskImpl(task.params || {}, that); diff --git a/BuildServer/lib/tasks/index.js b/BuildServer/lib/tasks/index.js new file mode 100644 index 0000000..4c179a7 --- /dev/null +++ b/BuildServer/lib/tasks/index.js @@ -0,0 +1,10 @@ +"use strict"; + +// Code taken from http://stackoverflow.com/a/17204293 +require("fs").readdirSync(__dirname).forEach((file) => { + if (file.match(/\.js$/) !== null && file !== "index.js") { + const name = file.replace(".js", ""); + + exports[name] = require(`./${file}`); + } +});