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
+39-17Lines changed: 39 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,17 @@
1
1
@nativescript/hook
2
2
=======================================
3
3
4
-
This module gives you an easier way to install hooks into NativeScript projects when using the `tns install <module>` command. A project hook is some sort of script that is configured to be executed when the NativeScript CLI executes some action.
4
+
An easier way to install hooks into NativeScript projects when using the `ns install <module>` command. A project hook is a script that is configured to be executed when the NativeScript CLI executes some action.
5
5
6
-
Hooks go into the `hooks/` folder of a project. For example, when `tns prepare ...` is executed, all script files in the `hooks/before-prepare/` and `hooks/after-prepare/` folders are executed, as well.
6
+
Hooks go into the `hooks/` folder of a project. For example, when `ns prepare ...` is executed, all script files in the `hooks/before-prepare/` and `hooks/after-prepare/` folders are executed as well.
7
7
8
-
This module simplifies the process of installing the scripts into the right project folder.
8
+
This simplifies the process of installing the scripts into the right project folder.
9
9
10
10
How to use
11
11
----------
12
12
13
13
### Describe the hooks
14
+
14
15
First, add a description of your hooks to the module's `package.json`. Here's an example:
15
16
```json
16
17
{
@@ -24,11 +25,13 @@ First, add a description of your hooks to the module's `package.json`. Here's an
24
25
},
25
26
}
26
27
```
27
-
The above specifies that the script `lib/before-prepare.js` should be executed when the `tns prepare ...` command is executed. the `"type"` property specifies the type of the hook to install. The `"script"` property specifies the path to the script to execute. You can add more hooks by extending the `"hooks"` array.
28
+
The above specifies that the script `lib/before-prepare.js` should be executed when the `ns prepare ...` command is executed. the `"type"` property specifies the type of the hook to install. The `"script"` property specifies the path to the script to execute. You can add more hooks by extending the `"hooks"` array.
28
29
29
30
### Install the hooks
31
+
30
32
Add a post-install and pre-uninstall script to your `package.json`, if you haven't done so already:
31
-
```
33
+
34
+
```ts
32
35
"scripts": {
33
36
...
34
37
"postinstall": "node postinstall.js",
@@ -37,20 +40,34 @@ Add a post-install and pre-uninstall script to your `package.json`, if you haven
37
40
```
38
41
39
42
The post-install script (`postinstall.js` in the example) should contain the following line:
These two hooks will take care of installing and removing the hooks from the NativeScript project, when your module is installed or uninstalled.
50
67
51
-
`tns install <module>`
68
+
`ns install <module>`
52
69
----------------------
53
-
NativeScript modules that install hooks are intended to be installed using the `tns install <module>` command, not through npm directly. During module installation the NativeScript CLI provides information to the post-install script where to put the hooks. The following environment variables are defined when installing through `tns install <module>`:
70
+
NativeScript modules that install hooks are intended to be installed using the `ns install <module>` command, not through npm directly. During module installation the NativeScript CLI provides information to the post-install script where to put the hooks. The following environment variables are defined when installing through `ns install <module>`:
54
71
* `TNS_HOOKS_DIR` - the directory where the hooks should be installed. It may or may not exist.
55
72
* `TNS_PROJECT_DIR` - the current project directory.
56
73
@@ -60,24 +77,29 @@ In-process hooks
60
77
----------------
61
78
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
79
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:
80
+
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 `exportdefaultfunction(...)` statement in the hook. For example, if the hook script is:
81
+
64
82
```javascript
65
-
module.exports=function($logger) {
83
+
export default function($logger) {
66
84
};
67
85
```
68
-
Then, the hook script will be require'd by the CLI and the exported function will be called through the injector.
86
+
Then, the hook script will be required by the CLI and the exported function will be called through the injector.
69
87
70
88
Hooks can take a special injected argument named `hookArgs`:
89
+
71
90
```javascript
72
-
module.exports=function(hookArgs) {
91
+
exportdefaultfunction(hookArgs) {
73
92
};
74
93
```
94
+
75
95
`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
96
77
97
If you execute asynchronous code in your hook, you need to return a promise, otherwise execution will continue before your hook completes:
0 commit comments