From 007471652758729ca35f49c0c09dd51d1df864e4 Mon Sep 17 00:00:00 2001 From: Alejandro Berzosa Date: Tue, 21 Nov 2023 11:16:05 +0100 Subject: [PATCH 1/3] create all folders beforehand --- src/ftp-deploy.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/ftp-deploy.js b/src/ftp-deploy.js index 0b23da6..08124c8 100644 --- a/src/ftp-deploy.js +++ b/src/ftp-deploy.js @@ -30,12 +30,27 @@ const FtpDeployer = function () { filename: "", }; - this.makeAllAndUpload = function (remoteDir, filemap) { - let keys = Object.keys(filemap); - return Promise.mapSeries(keys, (key) => { - // console.log("Processing", key, filemap[key]); - return this.makeAndUpload(remoteDir, key, filemap[key]); + this.makeAllAndUpload = function (config, filemap) { + + let folders = Object.keys(filemap) + .map(key => { + return upath.join(config.remoteRoot, key) }); + + let files = Object.keys(filemap) + .map(key => { + return filemap[key].map(file => { + if (key === "/") return file + return upath.join(key, file) + }) + }) + .flat(); + + // create all the dirs + return Promise.mapSeries(folders, folder => { + return this.makeDir(folder) + }) + .then(() => this.makeAndUpload(config, "/", files)) }; this.makeDir = function (newDirectory) { From eb4884671c75c40fff82498e4f0889a118618e8e Mon Sep 17 00:00:00 2001 From: Alejandro Berzosa Date: Tue, 21 Nov 2023 11:18:08 +0100 Subject: [PATCH 2/3] split file list in chunks to enable parallel uploads --- src/ftp-deploy.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/ftp-deploy.js b/src/ftp-deploy.js index 08124c8..c1c8060 100644 --- a/src/ftp-deploy.js +++ b/src/ftp-deploy.js @@ -64,9 +64,21 @@ const FtpDeployer = function () { // Resolves a confirmation message on success this.makeAndUpload = (config, relDir, fnames) => { let newDirectory = upath.join(config.remoteRoot, relDir); - return this.makeDir(newDirectory, true).then(() => { + return this.makeDir(newDirectory).then(() => { // console.log("newDirectory", newDirectory); - return Promise.mapSeries(fnames, (fname) => { + const parallelUploads = config.parallelUploads || 1 + + // split file list into chunks of configured size + const chunks = fnames.reduce((result, current, index) => { + const position = Math.floor(index / parallelUploads) + result[position] = [].concat(result[position] || [], current) + return result + }, []) + + // iterate every chunk in serie + return Promise.mapSeries(chunks, chunk => + // iterate every element of the chunk in parallel + Promise.map(chunk, (fname) => { let tmpFileName = upath.join(config.localRoot, relDir, fname); let tmp = fs.readFileSync(tmpFileName); this.eventObject["filename"] = upath.join(relDir, fname); @@ -86,7 +98,10 @@ const FtpDeployer = function () { // if continue on error.... return Promise.reject(err); }); - }); + }) + ) + // flatten the result to return only a list of files + .then(result => result.flat()); }); }; From aa8dc0ce63c43a4a1aca9d771ff5ed58dd0b025b Mon Sep 17 00:00:00 2001 From: Alejandro Berzosa Date: Tue, 21 Nov 2023 11:19:32 +0100 Subject: [PATCH 3/3] fix events to reference current filename only --- src/ftp-deploy.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/ftp-deploy.js b/src/ftp-deploy.js index c1c8060..9476d51 100644 --- a/src/ftp-deploy.js +++ b/src/ftp-deploy.js @@ -26,8 +26,7 @@ const FtpDeployer = function () { this.ftp = null; this.eventObject = { totalFilesCount: 0, - transferredFileCount: 0, - filename: "", + transferredFileCount: 0 }; this.makeAllAndUpload = function (config, filemap) { @@ -66,6 +65,8 @@ const FtpDeployer = function () { let newDirectory = upath.join(config.remoteRoot, relDir); return this.makeDir(newDirectory).then(() => { // console.log("newDirectory", newDirectory); + + // get the configured parallelUpload param, default to 1 const parallelUploads = config.parallelUploads || 1 // split file list into chunks of configured size @@ -81,20 +82,20 @@ const FtpDeployer = function () { Promise.map(chunk, (fname) => { let tmpFileName = upath.join(config.localRoot, relDir, fname); let tmp = fs.readFileSync(tmpFileName); - this.eventObject["filename"] = upath.join(relDir, fname); + const filename = upath.join(relDir, fname); - this.emit("uploading", this.eventObject); + this.emit("uploading", Object.assign({ filename }, this.eventObject)); return this.ftp .put(tmp, upath.join(config.remoteRoot, relDir, fname)) .then(() => { this.eventObject.transferredFileCount++; - this.emit("uploaded", this.eventObject); + this.emit("uploaded", Object.assign({ filename }, this.eventObject)); return Promise.resolve("uploaded " + tmpFileName); }) .catch((err) => { - this.eventObject["error"] = err; - this.emit("upload-error", this.eventObject); + let error = err; + this.emit("upload-error", Object.assign({ filename, error }, this.eventObject)); // if continue on error.... return Promise.reject(err); });