From dcda07b969c8d8d0bab3054a4eaee5ce3ef64c37 Mon Sep 17 00:00:00 2001 From: Jason Pickens Date: Tue, 17 May 2022 12:24:29 +1200 Subject: [PATCH 1/2] Only call callback once when outputting multiple files --- index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/index.js b/index.js index e6f12aa..d59c28b 100644 --- a/index.js +++ b/index.js @@ -113,9 +113,6 @@ ProtobufPlugin.prototype.singleOutput = function (files, cb) { }; ProtobufPlugin.prototype.multipleOutput = function (files, cb) { - if (files.length === 0) { - cb(); - } var promise = []; var self = this; var root = path.relative(process.cwd(), this.options.output); From 626019c73c58a58ffcc1e8d0e3099629916e5455 Mon Sep 17 00:00:00 2001 From: Jason Pickens Date: Tue, 7 Jun 2022 07:01:53 +1200 Subject: [PATCH 2/2] Invoke callback after all files are written --- index.js | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index d59c28b..e8f0bf2 100644 --- a/index.js +++ b/index.js @@ -113,41 +113,32 @@ ProtobufPlugin.prototype.singleOutput = function (files, cb) { }; ProtobufPlugin.prototype.multipleOutput = function (files, cb) { - var promise = []; var self = this; var root = path.relative(process.cwd(), this.options.output); mkdir.sync(root); - files.forEach(function (file) { - promise.push(new Promise(function (resolve, reject) { + const promises = files.map(function (file) { + return new Promise(function (resolve, reject) { let command = [].concat(self.command); command.push(file); pbjs.main(command, function (err, output) { if (err) { reject(err); } else { + var outputPath = path.join(self.options.output, path.parse(file).name + '.js'); + fs.writeFile(outputPath, output, function (err) { + reject(err) + }); resolve({ output: output, file: file }); } }); - })); - }); - Promise.all(promise) - .then(function (res) { - res.forEach(function (result) { - var output = result.output; - var file = result.file; - var outputPath = path.join(self.options.output, path.parse(file).name + '.js'); - fs.writeFile(outputPath, output, function (error) { - if (error) { - console.log('[protobuf plugin] output error: ', error); - } - }); }); + }); + Promise.all(promises).then(function () { cb(); - }) - .catch(function (err) { + }).catch(function (err) { console.log('[protobuf plugin] promise reject error: ', err); cb(); });