Skip to content

Commit 17434a7

Browse files
committed
docs for in-process hooks
1 parent ed3d53f commit 17434a7

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

README.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ How to use
1212

1313
### Describe the hooks
1414
First, add a description of your hooks to the module's `package.json`. Here's an example:
15-
```
15+
```json
1616
{
1717
"nativescript": {
1818
"hooks": [
@@ -54,4 +54,53 @@ NativeScript modules that install hooks are intended to be installed using the `
5454
* `TNS_HOOKS_DIR` - the directory where the hooks should be installed. It may or may not exist.
5555
* `TNS_PROJECT_DIR` - the current project directory.
5656

57-
Modules installed this way can be uninstalled using `npm rm --save-dev`.
57+
Modules installed this way can be uninstalled simply by running `npm rm --save-dev`.
58+
59+
In-process hooks
60+
----------------
61+
By default, hooks are executed in a child process and execution continues when the child process dies. This gives you flexibility when writing your hooks, but doesn't allow you to use any of the services of the CLI.
62+
63+
To that end, in-process hooks allow you to execute your hooks in such a way so that you can use any of the services available from the injector. In-process hooks work only for JavaScript hooks. To enable in-process execution, all you need to have is a `module.exports = ...` statement in the hook. For example, if the hook script is:
64+
```javascript
65+
module.exports = function($logger) {
66+
};
67+
```
68+
Then, the hook script will be require'd by the CLI and the exported function will be called through the injector.
69+
70+
Hooks can take a special injected argument named `hookArgs`:
71+
```javascript
72+
module.exports = function(hookArgs) {
73+
};
74+
```
75+
`hookArgs` is a hash containing all the arguments passed to the hooked method. For example, the `prepare` hook is activated by the CLI method `preparePlatform(platform: string)`. Here, the hook will get the value of the `platform` argument in the `hookArgs.platform` property.
76+
77+
If you execute asynchronous code in your hook, you need to return a promise, otherwise execution will continue before your hook completes:
78+
```javascript
79+
var mkdirp = require('mkdirp');
80+
module.exports = function($logger) {
81+
return new Promise(function(resolve, reject) {
82+
mkdirp('somedir', function(err) {
83+
if (err) {
84+
reject(err);
85+
else {
86+
resolve();
87+
}
88+
})
89+
});
90+
}
91+
```
92+
93+
And finally, when installing in-process hooks through this module, you need to explicitly specify that using the `inject` property of the script descriptor in the `package.json`:
94+
```json
95+
{
96+
"nativescript": {
97+
"hooks": [
98+
{
99+
"type": "after-prepare",
100+
"script": "lib/after-prepare.js",
101+
"inject": true
102+
}
103+
]
104+
},
105+
}
106+
```

0 commit comments

Comments
 (0)