Cordova Hooks

优质
小牛编辑
128浏览
2023-12-01

Cordova Hooks represent special scripts which could be added by application and plugin developers or even by your own build system to customize cordova commands. Hook scripts could be defined by adding them to the special predefined folder (/hooks) or via configuration files (config.xml and plugin.xml) and run serially in the following order:

  • 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.

Supported hook types

The following hook types are supported:

  1. after_build/
  2. after_compile/
  3. after_docs/
  4. after_emulate/
  5. after_platform_add/
  6. after_platform_rm/
  7. after_platform_ls/
  8. after_plugin_add/
  9. after_plugin_ls/
  10. after_plugin_rm/
  11. after_plugin_search/
  12. after_plugin_install/ <-- Plugin hooks defined in plugin.xml are executed exclusively for a plugin being installed
  13. after_prepare/
  14. after_run/
  15. after_serve/
  16. before_build/
  17. before_compile/
  18. before_docs/
  19. before_emulate/
  20. before_platform_add/
  21. before_platform_rm/
  22. before_platform_ls/
  23. before_plugin_add/
  24. before_plugin_ls/
  25. before_plugin_rm/
  26. before_plugin_search/
  27. before_plugin_install/ <-- Plugin hooks defined in plugin.xml are executed exclusively for a plugin being installed
  28. before_plugin_uninstall/ <-- Plugin hooks defined in plugin.xml are executed exclusively for a plugin being uninstalled
  29. before_prepare/
  30. before_run/
  31. before_serve/
  32. pre_package/ <-- Windows 8 and Windows Phone only.

Ways to define hooks

Via ‘/hooks’ directory

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

Config.xml

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. <hook type="before_plugin_install" src="scripts/appBeforePluginInstall.js" />
  4. <platform name="wp8">
  5. <hook type="before_build" src="scripts/wp8/appWP8BeforeBuild.bat" />
  6. <hook type="before_build" src="scripts/wp8/appWP8BeforeBuild.js" />
  7. <hook type="before_plugin_install" src="scripts/wp8/appWP8BeforePluginInstall.js" />
  8. ...
  9. </platform>
  10. <platform name="windows8">
  11. <hook type="before_build" src="scripts/windows8/appWin8BeforeBuild.bat" />
  12. <hook type="before_build" src="scripts/windows8/appWin8BeforeBuild.js" />
  13. <hook type="before_plugin_install" src="scripts/windows8/appWin8BeforePluginInstall.js" />
  14. ...
  15. </platform>

Plugin hooks (plugin.xml)

As a plugin developer you can define hook scripts using <hook> elements in a plugin.xml like that:

  1. <hook type="before_plugin_install" src="scripts/beforeInstall.js" />
  2. <hook type="after_build" src="scripts/afterBuild.js" />
  3. <platform name="wp8">
  4. <hook type="before_plugin_install" src="scripts/wp8BeforeInstall.js" />
  5. <hook type="before_build" src="scripts/wp8BeforeBuild.js" />
  6. ...
  7. </platform>

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

Script Interface

Javascript

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

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

You can make your scipts async using Q:

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

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

  1. {
  2. "hook": "before_plugin_install",
  3. "scriptLocation": "c:\script\full\path\appBeforePluginInstall.js",
  4. "cmdLine": "The\exact\command\cordova\run\with arguments",
  5. "opts": {
  6. "projectRoot":"C:\path\to\the\project",
  7. "cordova": {
  8. "platforms": ["wp8"],
  9. "plugins": ["com.plugin.withhooks"],
  10. "version": "0.21.7-dev"
  11. },
  12. "plugin": {
  13. "id": "com.plugin.withhooks",
  14. "pluginInfo": {
  15. ...
  16. },
  17. "platform": "wp8",
  18. "dir": "C:\path\to\the\project\plugins\com.plugin.withhooks"
  19. }
  20. },
  21. "cordova": {...}
  22. }

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

You can also require additional Cordova modules in your script using context.requireCordovaModule in the following way:

  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 /hooks folders are run via Node child_process spawn, see ‘Non-javascript’ section below.

Non-javascript

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.
  • CORDOVA_CMDLINE - The exact command-line arguments passed to cordova (e.g.: cordova run ios —emulate)

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

Writing hooks

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/

Also, note that even if you are working on Windows, and in case your hook scripts aren’t bat files (which is recommended, if you want your scripts to work in non-Windows operating systems) Cordova CLI will expect a shebang line as the first line for it to know the interpreter it needs to use to launch the script. The shebang line should match the following example:

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