UNPKG

7.71 kBMarkdownView Raw
1<!--
2#
3# Licensed to the Apache Software Foundation (ASF) under one
4# or more contributor license agreements. See the NOTICE file
5# distributed with this work for additional information
6# regarding copyright ownership. The ASF licenses this file
7# to you under the Apache License, Version 2.0 (the
8# "License"); you may not use this file except in compliance
9# with the License. You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing,
14# software distributed under the License is distributed on an
15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16# KIND, either express or implied. See the License for the
17# specific language governing permissions and limitations
18# under the License.
19#
20-->
21# Cordova Hooks
22
23Cordova 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:
24* Application hooks from `/hooks`;
25* Application hooks from `config.xml`;
26* Plugin hooks from `plugins/.../plugin.xml`.
27
28__Remember__: Make your scripts executable.
29
30__Note__: `.cordova/hooks` directory is also supported for backward compatibility, but we don't recommend using it as it is deprecated.
31
32## Supported hook types
33The following hook types are supported:
34
35 after_build/
36 after_compile/
37 after_docs/
38 after_emulate/
39 after_platform_add/
40 after_platform_rm/
41 after_platform_ls/
42 after_plugin_add/
43 after_plugin_ls/
44 after_plugin_rm/
45 after_plugin_search/
46 after_plugin_install/ <-- Plugin hooks defined in plugin.xml are executed exclusively for a plugin being installed
47 after_prepare/
48 after_run/
49 after_serve/
50 before_build/
51 before_compile/
52 before_docs/
53 before_emulate/
54 before_platform_add/
55 before_platform_rm/
56 before_platform_ls/
57 before_plugin_add/
58 before_plugin_ls/
59 before_plugin_rm/
60 before_plugin_search/
61 before_plugin_install/ <-- Plugin hooks defined in plugin.xml are executed exclusively for a plugin being installed
62 before_plugin_uninstall/ <-- Plugin hooks defined in plugin.xml are executed exclusively for a plugin being uninstalled
63 before_prepare/
64 before_run/
65 before_serve/
66 pre_package/ <-- Windows 8 and Windows Phone only.
67
68## Ways to define hooks
69### Via '/hooks' directory
70To 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:
71
72 # script file will be automatically executed after each build
73 hooks/after_build/after_build_custom_action.js
74
75
76### Config.xml
77
78Hooks can be defined in project's `config.xml` using `<hook>` elements, for example:
79
80 <hook type="before_build" src="scripts/appBeforeBuild.bat" />
81 <hook type="before_build" src="scripts/appBeforeBuild.js" />
82 <hook type="before_plugin_install" src="scripts/appBeforePluginInstall.js" />
83
84 <platform name="wp8">
85 <hook type="before_build" src="scripts/wp8/appWP8BeforeBuild.bat" />
86 <hook type="before_build" src="scripts/wp8/appWP8BeforeBuild.js" />
87 <hook type="before_plugin_install" src="scripts/wp8/appWP8BeforePluginInstall.js" />
88 ...
89 </platform>
90
91 <platform name="windows8">
92 <hook type="before_build" src="scripts/windows8/appWin8BeforeBuild.bat" />
93 <hook type="before_build" src="scripts/windows8/appWin8BeforeBuild.js" />
94 <hook type="before_plugin_install" src="scripts/windows8/appWin8BeforePluginInstall.js" />
95 ...
96 </platform>
97
98### Plugin hooks (plugin.xml)
99
100As a plugin developer you can define hook scripts using `<hook>` elements in a `plugin.xml` like that:
101
102 <hook type="before_plugin_install" src="scripts/beforeInstall.js" />
103 <hook type="after_build" src="scripts/afterBuild.js" />
104
105 <platform name="wp8">
106 <hook type="before_plugin_install" src="scripts/wp8BeforeInstall.js" />
107 <hook type="before_build" src="scripts/wp8BeforeBuild.js" />
108 ...
109 </platform>
110
111`before_plugin_install`, `after_plugin_install`, `before_plugin_uninstall` plugin hooks will be fired exclusively for the plugin being installed/uninstalled.
112
113## Script Interface
114
115### Javascript
116
117If you are writing hooks in Javascript you should use the following module definition:
118```javascript
119module.exports = function(context) {
120 ...
121}
122```
123
124You can make your scipts async using Q:
125```javascript
126module.exports = function(context) {
127 var Q = context.requireCordovaModule('q');
128 var deferral = new Q.defer();
129
130 setTimeout(function(){
131 console.log('hook.js>> end');
132 deferral.resolve();
133 }, 1000);
134
135 return deferral.promise;
136}
137```
138
139`context` object contains hook type, executed script full path, hook options, command-line arguments passed to Cordova and top-level "cordova" object:
140```json
141{
142 "hook": "before_plugin_install",
143 "scriptLocation": "c:\\script\\full\\path\\appBeforePluginInstall.js",
144 "cmdLine": "The\\exact\\command\\cordova\\run\\with arguments",
145 "opts": {
146 "projectRoot":"C:\\path\\to\\the\\project",
147 "cordova": {
148 "platforms": ["wp8"],
149 "plugins": ["com.plugin.withhooks"],
150 "version": "0.21.7-dev"
151 },
152 "plugin": {
153 "id": "com.plugin.withhooks",
154 "pluginInfo": {
155 ...
156 },
157 "platform": "wp8",
158 "dir": "C:\\path\\to\\the\\project\\plugins\\com.plugin.withhooks"
159 }
160 },
161 "cordova": {...}
162}
163
164```
165`context.opts.plugin` object will only be passed to plugin hooks scripts.
166
167You can also require additional Cordova modules in your script using `context.requireCordovaModule` in the following way:
168```javascript
169var Q = context.requireCordovaModule('q');
170```
171
172__Note__: new module loader script interface is used for the `.js` files defined via `config.xml` or `plugin.xml` only.
173For compatibility reasons hook files specified via `/hooks` folders are run via Node child_process spawn, see 'Non-javascript' section below.
174
175### Non-javascript
176
177Non-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:
178
179* CORDOVA_VERSION - The version of the Cordova-CLI.
180* CORDOVA_PLATFORMS - Comma separated list of platforms that the command applies to (e.g.: android, ios).
181* CORDOVA_PLUGINS - Comma separated list of plugin IDs that the command applies to (e.g.: org.apache.cordova.file, org.apache.cordova.file-transfer)
182* CORDOVA_HOOK - Path to the hook that is being executed.
183* CORDOVA_CMDLINE - The exact command-line arguments passed to cordova (e.g.: cordova run ios --emulate)
184
185If a script returns a non-zero exit code, then the parent cordova command will be aborted.
186
187## Writing hooks
188
189We highly recommend writing your hooks using Node.js so that they are
190cross-platform. Some good examples are shown here:
191
192[http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/](http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/)
193
194Also, 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:
195
196 #!/usr/bin/env [name_of_interpreter_executable]