Search code examples
cordovahookcordova-3

Add Parameters to Cordova-CLI Hook Scripts?


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.


Solution

  • 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);