Skip to content

Commit

Permalink
fix: remove cleanUp hook + delete file after copying it (#87)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
OS-martacarlos authored and OS-ricardomoreirasilva committed Apr 12, 2024
1 parent 2d65bea commit 02e21dd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
1 change: 1 addition & 0 deletions hooks/cleanUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
47 changes: 32 additions & 15 deletions hooks/unzipSound.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
let path = require("path");
let utils = require("./utilities");
let AdmZip = require("adm-zip");
var q;

let constants = {
soundZipFile: "sounds.zip"
Expand All @@ -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){
Expand All @@ -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) {
Expand All @@ -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();
}
12 changes: 6 additions & 6 deletions hooks/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
},
"engines": [],
"dependencies": {
"adm-zip": "0.5.2"
"adm-zip": "0.5.12"
}
}

0 comments on commit 02e21dd

Please sign in to comment.