From 02e21ddac4e843a95fc75ef7c8106644a592f5dc Mon Sep 17 00:00:00 2001 From: Marta Carlos <101343976+OS-martacarlos@users.noreply.github.com> Date: Mon, 8 Apr 2024 10:41:33 +0100 Subject: [PATCH] fix: remove cleanUp hook + delete file after copying it (#87) - adds a promise array. this way, it waits for all promises to be resolved before running the next hook - adds logs for easier future troubleshooting https://outsystemsrd.atlassian.net/browse/RMET-3326 --- CHANGELOG.md | 1 + hooks/cleanUp.js | 1 + hooks/unzipSound.js | 47 ++++++++++++++++++++++++++++++--------------- hooks/utilities.js | 12 ++++++------ package.json | 2 +- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c843adc..8a5bed00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The changes documented here do not include those from the original repository. ## [Unreleased] +- Fix: Make `cleanUp` hook only run after all sound files are copied successfully. (https://outsystemsrd.atlassian.net/browse/RMET-3326) - Chore: Update cordova hooks with new OutSystems specific errors. (https://outsystemsrd.atlassian.net/browse/RMET-3302) - Chore: Update `FirebaseMessaging` iOS pod to version `10.23.0`. This includes the Privacy Manifest (https://outsystemsrd.atlassian.net/browse/RMET-3274). - Chore: Update `Firebase/Messaging` iOS pod to version `8.15.0` (https://outsystemsrd.atlassian.net/browse/RMET-3141). diff --git a/hooks/cleanUp.js b/hooks/cleanUp.js index c58ad180..80d00239 100644 --- a/hooks/cleanUp.js +++ b/hooks/cleanUp.js @@ -28,6 +28,7 @@ module.exports = function(context) { let soundFolderPath = path.join(sourcePath, constants.soundFolder); if(utils.checkIfFileOrFolderExists(soundFolderPath)){ + console.log(`FCM_LOG: Deleting sounds folder @ ${soundFolderPath} `) utils.removeFolder(soundFolderPath); } diff --git a/hooks/unzipSound.js b/hooks/unzipSound.js index 64b72e55..98914b5b 100644 --- a/hooks/unzipSound.js +++ b/hooks/unzipSound.js @@ -3,6 +3,7 @@ let path = require("path"); let utils = require("./utilities"); let AdmZip = require("adm-zip"); +var q; let constants = { soundZipFile: "sounds.zip" @@ -12,10 +13,11 @@ function copyWavFiles(platformConfig, source, dest, defer) { let files = utils.getFilesFromPath(source); let filteredFiles = files.filter(function(file){ - return file.endsWith(platformConfig.soundFileExtension) == true; + return file.endsWith(platformConfig.soundFileExtension); }); - - copyFiles(filteredFiles, source, dest, defer) + + console.log(`FCM_LOG: Found ${filteredFiles.length} sound files!`); + return copyFiles(filteredFiles, source, dest, defer) } function copyFiles(files, source, dest, defer){ @@ -26,23 +28,30 @@ function copyFiles(files, source, dest, defer){ if(!utils.checkIfFileOrFolderExists(dest)) { utils.createOrCheckIfFolderExists(dest); } - + let promiseArray = [] for(const element of files) { let filePath = path.join(source, element); let destFilePath = path.join(dest, element); - utils.copyFromSourceToDestPath(defer, filePath, destFilePath); + console.log(`FCM_LOG: Copying [${filePath}] to [${destFilePath}]`); + + let copyDefer = q.defer(); + promiseArray.push(copyDefer); + utils.copyFromSourceToDestPath(copyDefer, filePath, destFilePath); } + + return promiseArray; } module.exports = function(context) { let cordovaAbove8 = utils.isCordovaAbove(context, 8); - let defer; + if (cordovaAbove8) { - defer = require("q").defer(); + q = require('q'); } else { - defer = context.requireCordovaModule("q").defer(); + q = context.requireCordovaModule("q"); } - + let defer = q.defer(); + let platform = context.opts.platforms[0]; let platformConfig = utils.getPlatformConfigs(platform); if (!platformConfig) { @@ -54,26 +63,34 @@ module.exports = function(context) { soundFolderPath = path.join(context.opts.projectRoot, soundFolderPath); let soundZipFile = path.join(sourcePath, constants.soundZipFile); - + let promises = []; if(utils.checkIfFileOrFolderExists(soundZipFile)){ let zip = new AdmZip(soundZipFile); zip.extractAllTo(sourcePath, true); let entriesNr = zip.getEntries().length; + console.log(`FCM_LOG: Sound zip file has ${entriesNr} entries`); if(entriesNr == 0) { throw new Error (`OUTSYSTEMS_PLUGIN_ERROR: Sound zip file is empty, either delete it or add one or more files.`) } let zipFolder = sourcePath + "/sounds" - + if(!utils.checkIfFileOrFolderExists(zipFolder)){ - /**to deal with the following case: + console.log(`FCM_LOG: No new folder unzipping.`) + /** + * to deal with the following case: * iOS + one file in zip + O11 **/ - if(sourcePath != soundFolderPath) - copyWavFiles(platformConfig, sourcePath, soundFolderPath, defer) + if(sourcePath != soundFolderPath){ + console.log(`FCM_LOG: ${sourcePath} != ${soundFolderPath} so we need to copy files.`) + promises = copyWavFiles(platformConfig, sourcePath, soundFolderPath, defer) + } else { + console.log(`FCM_LOG: ${sourcePath} == ${soundFolderPath} so we don't need to copy files.`) + } } else { - copyWavFiles(platformConfig, zipFolder, soundFolderPath, defer) + promises = copyWavFiles(platformConfig, zipFolder, soundFolderPath, defer) } } + return promises.length > 0 ? q.all(promises) : defer.resolve(); } diff --git a/hooks/utilities.js b/hooks/utilities.js index 3816f164..9fe5ff5f 100644 --- a/hooks/utilities.js +++ b/hooks/utilities.js @@ -64,14 +64,14 @@ function getPlatformSoundPath(context, platformConfig){ let platformPath; if(platformConfig === constants.android){ - platformPath = path.join(projectRoot, `platforms/android/www`); + platformPath = path.join(projectRoot, `platforms/android/www`); } else { - let appName = getAppName(context) - platformPath = path.join(projectRoot, `platforms/ios/${appName}/Resources/www`); + let appName = getAppName(context) + platformPath = path.join(projectRoot, `platforms/ios/${appName}/Resources/www`); } if(!fs.existsSync(platformPath)){ - platformPath = path.join(projectRoot, platformConfig.getWWWFolder()); + platformPath = path.join(projectRoot, platformConfig.getWWWFolder()); } return platformPath @@ -83,10 +83,10 @@ function isCordovaAbove(context, version) { return parseInt(sp[0]) >= version; } - function copyFromSourceToDestPath(defer, sourcePath, destPath) { fs.createReadStream(sourcePath).pipe(fs.createWriteStream(destPath)) - .on("close", function (err) { + .on("close", function () { + console.log(`Finished copying ${sourcePath}.`); defer.resolve(); }) .on("error", function (err) { diff --git a/package.json b/package.json index efcf2790..0ccb918e 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,6 @@ }, "engines": [], "dependencies": { - "adm-zip": "0.5.2" + "adm-zip": "0.5.12" } }