Add Parameters to Cordova-CLI Hook Scripts?
Asked Answered
E

3

7

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.

Exum answered 28/1, 2014 at 22:24 Comment(0)
P
6

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);
Pencel answered 14/4, 2014 at 7:3 Comment(2)
That is a very difficult to maintain solution - Much better to compose that into an option to another command that will then invoke the cordova build process, such as Gulp or Grunt.Whalebone
What to do if on mac terminal?Exercise
E
1

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
Exclaim answered 2/8, 2017 at 14:21 Comment(1)
add hook script to config.xml <hook src="hooks/before_compile/hook-cmd-loop" type="before_compile" />Exclaim
R
0

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.

Regain answered 31/1, 2014 at 18:38 Comment(3)
That throws this build error and if you're running it from another script like grunt it shows as a failed build. platforms/ios/cordova/build --qa Unrecognized flag: --qa Error: /Users/tom.hicks/Projects/CloudCast/client/platforms/ios/cordova/build: Command failed with exit code 2 at ChildProcess.whenDone (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/superspawn.js:131:23) at ChildProcess.emit (events.js:98:17) at maybeClose (child_process.js:755:16) at Process.ChildProcess._handle.onexit (child_process.js:822:5)Intermigration
Yeah, I've found that this doesn't work any more after recent updates. Cordova keeps changing how it handles command line parameters and the environment that build scripts are run in.Regain
We've been ignoring the cordova build command altogether. So our process is edit the site in /www run cordova prepare from the project root and then open/build the xcode project in /platforms/ios. Now I just keep xcode open and run cordova prepare --qa after making changes then I hit play in xcode to start debugging.Intermigration

© 2022 - 2024 — McMap. All rights reserved.