Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RMET-2695 :: fix hook execution #70

Merged
merged 7 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions hooks/cleanUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ var constants = {
};

module.exports = function(context) {
var platform = context.opts.plugin.platform;
var platformConfig = utils.getPlatformConfigs(platform);
let cordovaAbove8 = utils.isCordovaAbove(context, 8);
let defer;
if (cordovaAbove8) {
defer = require("q").defer();
} else {
defer = context.requireCordovaModule("q").defer();
}

let platform = context.opts.platforms[0];
let platformConfig = utils.getPlatformConfigs(platform);

if (!platformConfig) {
utils.handleError("Invalid platform", defer);
}

var sourcePath = platformConfig.getSoundSourceFolder();
var soundFolderPath = path.join(sourcePath, constants.soundFolder);
let sourcePath = utils.getPlatformSoundPath(context, platformConfig)
let soundFolderPath = path.join(sourcePath, constants.soundFolder);

if(utils.checkIfFileOrFolderExists(soundFolderPath)){
utils.removeFolder(soundFolderPath);
Expand Down
27 changes: 0 additions & 27 deletions hooks/installDependencies.js

This file was deleted.

5 changes: 0 additions & 5 deletions hooks/package.json

This file was deleted.

38 changes: 18 additions & 20 deletions hooks/unzipSound.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

var path = require("path");
let path = require("path");
var utils = require("./utilities");
var AdmZip = require("adm-zip");

Expand All @@ -15,6 +15,7 @@ function copyWavFiles(platformConfig, source, dest, defer) {
return file.endsWith(platformConfig.soundFileExtension) == true;
});

console.log("Nr of sound files in zip: ", filteredFiles.length)
copyFiles(filteredFiles, source, dest, defer)
}

Expand All @@ -28,53 +29,50 @@ function copyFiles(files, source, dest, defer){
utils.createOrCheckIfFolderExists(dest);
}

for(var i = 0; i < files.length; i++) {
var filePath = path.join(source, files[i]);
var destFilePath = path.join(dest, files[i]);
for(const element of files) {
let filePath = path.join(source, element);
let destFilePath = path.join(dest, element);
utils.copyFromSourceToDestPath(defer, filePath, destFilePath);
}
}

module.exports = function(context) {
var cordovaAbove8 = utils.isCordovaAbove(context, 8);
var defer;
let cordovaAbove8 = utils.isCordovaAbove(context, 8);
let defer;
if (cordovaAbove8) {
defer = require("q").defer();
} else {
defer = context.requireCordovaModule("q").defer();
}

var platform = context.opts.plugin.platform;
var platformConfig = utils.getPlatformConfigs(platform);
let platform = context.opts.platforms[0];
let platformConfig = utils.getPlatformConfigs(platform);
if (!platformConfig) {
utils.handleError("Invalid platform", defer);
}

var sourcePath = platformConfig.getSoundSourceFolder();
var soundFolderPath = platformConfig.getSoundDestinationFolder();
let sourcePath = utils.getPlatformSoundPath(context, platformConfig)
let soundFolderPath = platformConfig.getSoundDestinationFolder();

var soundZipFile = path.join(sourcePath, constants.soundZipFile);
let soundZipFile = path.join(sourcePath, constants.soundZipFile);

if(utils.checkIfFileOrFolderExists(soundZipFile)){
var zip = new AdmZip(soundZipFile);
let zip = new AdmZip(soundZipFile);
zip.extractAllTo(sourcePath, true);

var entriesNr = zip.getEntries().length;
console.log("Number of entries in zip file: ", entriesNr);
let entriesNr = zip.getEntries().length;

if(entriesNr == 0) {
utils.handleError("Sound zip file is empty, either delete it or add one or more files", defer);
return
}

var zipFolder = sourcePath + "/sounds"
let zipFolder = sourcePath + "/sounds"

if(!utils.checkIfFileOrFolderExists(zipFolder)){
if(utils.isAndroid(platform))
copyWavFiles(platformConfig, sourcePath, soundFolderPath, defer)
} else {
var files = utils.getFilesFromPath(zipFolder);
copyFiles(files, zipFolder, soundFolderPath, defer)
copyWavFiles(platformConfig, sourcePath, soundFolderPath, defer)
} else {
copyWavFiles(platformConfig, zipFolder, soundFolderPath, defer)
}

utils.removeFile(soundZipFile);
Expand Down
40 changes: 33 additions & 7 deletions hooks/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ var constants = {
platforms: "platforms",
android: {
platform: "android",
wwwFolder: "assets/www",
wwwFolder: "www",
soundFileExtension: ".wav",
getSoundDestinationFolder: function() {
return "platforms/android/app/src/main/res/raw";
},
getSoundSourceFolder: function() {
getWWWFolder: function() {
return "www";
}
},
Expand All @@ -21,10 +21,10 @@ var constants = {
wwwFolder: "www",
soundFileExtension: ".wav",
getSoundDestinationFolder: function() {
return "www";
return "platforms/ios/www";
},
getSoundSourceFolder: function() {
return "www";
getWWWFolder: function() {
return "platforms/ios/www";
}
}
};
Expand All @@ -44,7 +44,7 @@ function removeFile(path){
}

function removeFolder(path){
fs.rmdirSync(path, { recursive: true })
fs.rmSync(path, { recursive: true })
}

function getFilesFromPath(path) {
Expand All @@ -65,6 +65,24 @@ function getPlatformConfigs(platform) {
}
}

function getPlatformSoundPath(context, platformConfig){
let projectRoot = context.opts.projectRoot;
let platformPath;

if(platformConfig === constants.android){
platformPath = path.join(projectRoot, `platforms/android/www`);
} else {
let appName = getAppName(context)
platformPath = path.join(projectRoot, `platforms/ios/${appName}/Resources/www`);
}

if(!fs.existsSync(platformPath)){
platformPath = path.join(projectRoot, platformConfig.getWWWFolder());
}

return platformPath
}

function isCordovaAbove(context, version) {
let cordovaVersion = context.opts.cordova.version;
let sp = cordovaVersion.split('.');
Expand All @@ -87,6 +105,12 @@ function isAndroid(platform){
return platform === constants.android.platform
}

function getAppName(context) {
let ConfigParser = context.requireCordovaModule("cordova-lib").configparser;
let config = new ConfigParser("config.xml");
return config.name();
}

module.exports = {
isCordovaAbove,
handleError,
Expand All @@ -97,5 +121,7 @@ module.exports = {
checkIfFileOrFolderExists,
removeFile,
removeFolder,
isAndroid
isAndroid,
getAppName,
getPlatformSoundPath
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"android"
]
},
"engines": []
"engines": [],
"dependencies": {
"adm-zip": "0.4.11"
}
}
7 changes: 4 additions & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
</js-module>

<dependency id="cordova-outsystems-firebase-core" url="https://github.com/OutSystems/cordova-outsystems-firebase-core.git#2.0.0"/>
<hook type="before_plugin_install" src="hooks/installDependencies.js" />
<hook type="before_plugin_install" src="hooks/unzipSound.js" />
<hook type="after_plugin_install" src="hooks/cleanUp.js" />


<hook type="after_prepare" src="hooks/unzipSound.js" />
<hook type="after_prepare" src="hooks/cleanUp.js" />

<platform name="ios">

Expand Down