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:
# script file will be automatically executed after each build
hooks/after_build/after_build_custom_action.js
Hooks can be defined in project’s config.xml
using <hook>
elements, for example:
<hook type="before_build" src="scripts/appBeforeBuild.bat" />
<hook type="before_build" src="scripts/appBeforeBuild.js" />
<platform name="wp8">
<hook type="before_build" src="scripts/wp8/appWP8BeforeBuild.bat" />
<hook type="before_build" src="scripts/wp8/appWP8BeforeBuild.js" />
...
</platform>
<platform name="windows8">
<hook type="before_build" src="scripts/windows8/appWin8BeforeBuild.bat" />
<hook type="before_build" src="scripts/windows8/appWin8BeforeBuild.js" />
<hook type="before_plugin_install" src="scripts/windows8/appWin8BeforePluginInstall.js" />
...
</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:
module.exports = function(context) {
...
You can make your scipts async using Q:
var Q = context.requireCordovaModule('q');
var deferral = new Q.defer();
setTimeout(function(){
console.log('hook.js>> end');
deferral.resolve();
}, 1000);
return deferral.promise;
}
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.
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/
#!/usr/bin/env [name_of_interpreter_executable]