Cordova Hooks

    • Application hooks from /hooks;
    • Application hooks from config.xml;
    • Plugin hooks from plugins/.../plugin.xml.

    Remember: Make your scripts executable.

    Note: .cordova/hooks directory is also supported for backward compatibility, but we don’t recommend using it as it is deprecated.

    The following hook types are supported:

    To execute custom action when corresponding hook type is fired, use hook type as a name for a subfolder inside ‘hooks’ directory and place you script file here, for example:

    1. # script file will be automatically executed after each build
    2. hooks/after_build/after_build_custom_action.js

    Hooks can be defined in project’s config.xml using <hook> elements, for example:

    1. <hook type="before_build" src="scripts/appBeforeBuild.bat" />
    2. <hook type="before_build" src="scripts/appBeforeBuild.js" />
    3. <platform name="wp8">
    4. <hook type="before_build" src="scripts/wp8/appWP8BeforeBuild.bat" />
    5. <hook type="before_build" src="scripts/wp8/appWP8BeforeBuild.js" />
    6. ...
    7. </platform>
    8. <platform name="windows8">
    9. <hook type="before_build" src="scripts/windows8/appWin8BeforeBuild.bat" />
    10. <hook type="before_build" src="scripts/windows8/appWin8BeforeBuild.js" />
    11. <hook type="before_plugin_install" src="scripts/windows8/appWin8BeforePluginInstall.js" />
    12. ...
    13. </platform>

    before_plugin_install, after_plugin_install, before_plugin_uninstall plugin hooks will be fired exclusively for the plugin being installed/uninstalled.

    If you are writing hooks in Javascript you should use the following module definition:

    1. module.exports = function(context) {
    2. ...

    You can make your scipts async using Q:

    1. var Q = context.requireCordovaModule('q');
    2. var deferral = new Q.defer();
    3. setTimeout(function(){
    4. console.log('hook.js>> end');
    5. deferral.resolve();
    6. }, 1000);
    7. return deferral.promise;
    8. }

    context object contains hook type, executed script full path, hook options, command-line arguments passed to Cordova and top-level “cordova” object:

    context.opts.plugin object will only be passed to plugin hooks scripts.

    1. var Q = context.requireCordovaModule('q');

    Note: new module loader script interface is used for the .js files defined via config.xml or plugin.xml only.
    For compatibility reasons hook files specified via folders are run via Node child_process spawn, see ‘Non-javascript’ section below.

    Non-javascript scripts are run via Node child_process spawn from the project’s root directory and have the root directory passes as the first argument. All other options are passed to the script using environment variables:

    • CORDOVA_VERSION - The version of the Cordova-CLI.
    • CORDOVA_PLATFORMS - Comma separated list of platforms that the command applies to (e.g.: android, ios).
    • CORDOVA_PLUGINS - Comma separated list of plugin IDs that the command applies to (e.g.: org.apache.cordova.file, org.apache.cordova.file-transfer)
    • CORDOVA_HOOK - Path to the hook that is being executed.

    If a script returns a non-zero exit code, then the parent cordova command will be aborted.

    We highly recommend writing your hooks using Node.js so that they are
    cross-platform. Some good examples are shown here:

    http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/

    1. #!/usr/bin/env [name_of_interpreter_executable]