<!--
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
-->
# Cordova Hooks
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:
after_build/
after_compile/
after_docs/
after_emulate/
after_platform_add/
after_platform_rm/
after_platform_ls/
after_plugin_add/
after_plugin_ls/
after_plugin_rm/
after_plugin_search/
after_plugin_install/ <-- Plugin hooks defined in plugin.xml are executed exclusively for a plugin being installed
after_prepare/
after_run/
after_serve/
before_build/
before_compile/
before_docs/
before_emulate/
before_platform_add/
before_platform_rm/
before_platform_ls/
before_plugin_add/
before_plugin_ls/
before_plugin_rm/
before_plugin_search/
before_plugin_install/ <-- Plugin hooks defined in plugin.xml are executed exclusively for a plugin being installed
before_plugin_uninstall/ <-- Plugin hooks defined in plugin.xml are executed exclusively for a plugin being uninstalled
before_prepare/
before_run/
before_serve/
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:
# script file will be automatically executed after each build
hooks/after_build/after_build_custom_action.js
### Config.xml
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" />
<hook type="before_plugin_install" src="scripts/appBeforePluginInstall.js" />
<platform name="wp8">
<hook type="before_build" src="scripts/wp8/appWP8BeforeBuild.bat" />
<hook type="before_build" src="scripts/wp8/appWP8BeforeBuild.js" />
<hook type="before_plugin_install" src="scripts/wp8/appWP8BeforePluginInstall.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>
### Plugin hooks (plugin.xml)
As a plugin developer you can define hook scripts using `<hook>` elements in a `plugin.xml` like that:
<hook type="before_plugin_install" src="scripts/beforeInstall.js" />
<hook type="after_build" src="scripts/afterBuild.js" />
<platform name="wp8">
<hook type="before_plugin_install" src="scripts/wp8BeforeInstall.js" />
<hook type="before_build" src="scripts/wp8BeforeBuild.js" />
...
</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:
```javascript
module.exports = function(context) {
...
}
```
You can make your scipts async using Q:
```javascript
module.exports = function(context) {
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:
```json
{
"hook": "before_plugin_install",
"scriptLocation": "c:\\script\\full\\path\\appBeforePluginInstall.js",
"cmdLine": "The\\exact\\command\\cordova\\run\\with arguments",
"opts": {
"projectRoot":"C:\\path\\to\\the\\project",
"cordova": {
"platforms": ["wp8"],
"plugins": ["com.plugin.withhooks"],
"version": "0.21.7-dev"
},
"plugin": {
"id": "com.plugin.withhooks",
"pluginInfo": {
...
},
"platform": "wp8",
"dir": "C:\\path\\to\\the\\project\\plugins\\com.plugin.withhooks"
}
},
"cordova": {...}
}
```
`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:
```javascript
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/](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:
#!/usr/bin/env [name_of_interpreter_executable]
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Ionic-bate(测试版)开发的校园代领快递APP,Ionic可同时部署IOS,Android,微信三个平台,通过[HTML5]+[Typescript ES6 AngularJS2]+[Facebook React]在开发运行效率可以说是最好的H5框架,把传统的JS开发变成面向对象开发,可控性比较好(个人实在是驾驭不了代码量较大的JS)。缺点就是现有的第三方SDK比如百度地图,支付宝,推送等都还是JS SDK,在Typescript上集成不太方便,So,坐等Typescript发展&普及&Ionic2;稳定版的推出。
资源推荐
资源详情
资源评论
收起资源包目录
校园代领快递APP (133个子文件)
.editorconfig 363B
.gitignore 165B
about.html 2KB
issuer.html 2KB
order_info.html 2KB
home.html 2KB
apply_identi.html 2KB
contact.html 1KB
person_center.html 1KB
index.html 1KB
PopoverPage.html 1KB
tast_appeal.html 1023B
editschool.html 932B
login.html 848B
register.html 756B
register_sub.html 737B
edit_info.html 504B
chose_img.html 494B
address.html 328B
tabs.html 311B
about_shundai.html 173B
demo_1.iml 595B
person_bg.jpg 148KB
user_head.jpg 9KB
010_add_platform_class.js 3KB
gulpfile.js 2KB
package.json 1KB
typings.json 392B
tsconfig.json 370B
typings.json 142B
ionic.config.json 75B
tslint.json 63B
README.md 8KB
README.md 1KB
banner1.png 438KB
banner2.png 306KB
card-sf.png 298KB
7.PNG 119KB
Default-Landscape@2x~ipad.png 100KB
Default-Portrait@2x~ipad.png 97KB
drawable-land-xxxhdpi-screen.png 87KB
drawable-port-xxxhdpi-screen.png 80KB
3.PNG 74KB
splash.png 61KB
icon.png 59KB
drawable-land-xxhdpi-screen.png 58KB
drawable-port-xxhdpi-screen.png 54KB
6.PNG 47KB
Default-736h.png 44KB
Default-Landscape-736h.png 44KB
4.PNG 41KB
Default-667h.png 39KB
drawable-land-xhdpi-screen.png 39KB
drawable-port-xhdpi-screen.png 38KB
5.PNG 35KB
Default-568h@2x~iphone.png 31KB
Default-Portrait~ipad.png 22KB
Default-Landscape~ipad.png 22KB
Default@2x~iphone.png 18KB
1.PNG 13KB
drawable-land-hdpi-screen.png 13KB
drawable-port-hdpi-screen.png 13KB
2.PNG 13KB
drawable-xxxhdpi-icon.png 12KB
icon-60@3x.png 11KB
icon-76@2x.png 8KB
icon-72@2x.png 8KB
drawable-xxhdpi-icon.png 8KB
Default~iphone.png 7KB
drawable-port-mdpi-screen.png 7KB
drawable-land-mdpi-screen.png 7KB
icon-60@2x.png 6KB
chuzi.png 6KB
person_center.png 6KB
icon@2x.png 6KB
about.png 6KB
shenqingrenzhen.png 5KB
renzhen.png 5KB
icon-50@2x.png 5KB
drawable-xhdpi-icon.png 4KB
icon-small@3x.png 4KB
drawable-land-ldpi-screen.png 4KB
drawable-port-ldpi-screen.png 4KB
icon-40@2x.png 3KB
icon-76.png 3KB
drawable-hdpi-icon.png 3KB
icon-72.png 3KB
icon-60.png 2KB
icon-small@2x.png 2KB
icon.png 2KB
icon-50.png 2KB
drawable-mdpi-icon.png 2KB
person.png 2KB
icon-40.png 1KB
drawable-ldpi-icon.png 1KB
next.png 1KB
icon-small.png 818B
app.variables.scss 1KB
app.md.scss 986B
app.wp.scss 946B
共 133 条
- 1
- 2
资源评论
qq_33633942
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功