Is there a way to pass in command parameters to a Cordova-CLI hook script? Specifically I want to skin an application for a few clients and I would like to copy in their specific settings before the build by passing in an id number or something.
You can access parameters passed to cordova hooks via environment variables. You can set an environment variable that will stay 'alive' for the current session.
For example, if we have a variable called 'TARGET':
Windows cmd:
SET TARGET=someValue
cordova build android
Powershell:
$env:TARGET = "someValue"
iex "cordova build android"
You can then access these environment variables in your hooks with the following syntax (this is assuming you are writing your hooks with node.js):
var target = "someDefaultValue";
// Check for existence of the environment variable
if (process.env.TARGET) {
// Log the value to the console
console.log('process.env.TARGET is set to: ' + process.env.TARGET);
// Override the default
target = process.env.TARGET;
}
// Log the set value
console.log('Target is set to: ' + target);
Look at using the cordova command and passing you own set of commands EG: cordova run android -e env_value
In your hooks you able to look up the -e command passed by using the CORDOVA_CMDLINE
In the below bash script I am able to do a loop through each word in the command passed
#!/bin/sh
(
command=${CORDOVA_CMDLINE}
for word in $command
do
if [ "$flag" = "true" ]
then
echo "Flag is true Word printed is: '$word'"
fi
if [ "$word" = "-e" ]
then
echo $flag
flag="true"
else
echo $word
flag="false"
fi
done
Echo for above example: cordova run ios -e "prod"
cordova
run
android
-e
Flag is true word printed is: prod
Yes, you can see the full command line passed to Cordova by looking at the CORDOVA_CMDLINE
environment variable. You should see it set to something like this:
node /usr/local/bin/cordova build ios your_extra_parameters_can_go_here
It seems that Cordova ignores any parameter that it doesn't recognise (avoid starting your own parameters with a dash), so you can add your own custom values after the platform parameter.
Bear in mind that this functionality was added very recently (version 3.3.0). So if that variable isn't set for you, try upgrading your Cordova.
© 2022 - 2024 — McMap. All rights reserved.