You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+51-2Lines changed: 51 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ How to use
12
12
13
13
### Describe the hooks
14
14
First, add a description of your hooks to the module's `package.json`. Here's an example:
15
-
```
15
+
```json
16
16
{
17
17
"nativescript": {
18
18
"hooks": [
@@ -54,4 +54,53 @@ NativeScript modules that install hooks are intended to be installed using the `
54
54
*`TNS_HOOKS_DIR` - the directory where the hooks should be installed. It may or may not exist.
55
55
*`TNS_PROJECT_DIR` - the current project directory.
56
56
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
+
returnnewPromise(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`:
0 commit comments