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
@@ -41,13 +41,21 @@ The NativeScript CLI supports two different ways of executing hooks:
41
41
**Basic Module Definition**
42
42
43
43
To write an in-process hook, use the following module definition:
44
+
::: code-group
44
45
45
-
```javascript
46
+
```javascript [thehook.mjs (ESM)]
47
+
exportdefaultfunction (hookArgs) {
48
+
// Hook implementation ESM
49
+
}
50
+
```
51
+
52
+
```javascript [thehook.js (CommonJS)]
46
53
module.exports=function () {
47
-
// Hook implementation
54
+
// Hook implementation CommonJs
48
55
}
49
56
```
50
57
58
+
:::
51
59
**Using hookArgs**
52
60
53
61
The hook function can accept a special argument named `hookArgs`, which is an object containing all arguments passed to the hooked method.
@@ -71,29 +79,58 @@ Then `hookArgs` will have the following structure:
71
79
```
72
80
73
81
**Using hookArgs in your hook:**
82
+
::: code-group
74
83
75
-
```javascript
84
+
```javascript [thehook.mjs (ESM)]
85
+
exportdefaultfunction (hookArgs) {
86
+
console.log(JSON.stringify(hookArgs.prepareData))
87
+
}
88
+
```
89
+
90
+
```javascript [thehook.js (CommonJS)]
76
91
module.exports=function (hookArgs) {
77
92
console.log(hookArgs.projectData)
78
93
}
79
94
```
80
95
96
+
:::
81
97
**Dependency Injection**
82
98
83
99
NativeScript CLI is built with Dependency Injection and executes in-process hooks in a way that allows you to use any registered service from the injector.
@@ -102,6 +139,8 @@ module.exports = function ($injector, hookArgs) {
102
139
}
103
140
```
104
141
142
+
:::
143
+
105
144
**Important Notes:**
106
145
107
146
- Injected dependencies are resolved by name
@@ -112,7 +151,25 @@ module.exports = function ($injector, hookArgs) {
112
151
113
152
NativeScript CLI supports asynchronous code in hooks. If executing async code, you must return a Promise:
114
153
115
-
```javascript
154
+
::: code-group
155
+
156
+
```javascript [thehook.mjs (ESM)]
157
+
import { mkdirp } from'mkdirp'
158
+
159
+
exportdefaultfunction ($logger) {
160
+
returnnewPromise(function (resolve, reject) {
161
+
mkdirp('somedir', function (err) {
162
+
if (err) {
163
+
reject(err)
164
+
} else {
165
+
resolve()
166
+
}
167
+
})
168
+
})
169
+
}
170
+
```
171
+
172
+
```javascript [thehook.js (CommonJS)]
116
173
var mkdirp =require('mkdirp')
117
174
118
175
module.exports=function ($logger) {
@@ -128,6 +185,8 @@ module.exports = function ($logger) {
128
185
}
129
186
```
130
187
188
+
:::
189
+
131
190
## Spawned Hooks
132
191
133
192
Spawned hooks are executed via Node's `child_process.spawn` from the project's root directory. All options are passed to the script using environment variables:
@@ -146,27 +205,25 @@ If a spawned hook returns a non-zero exit code, NativeScript CLI will throw an e
146
205
147
206
Hooks can execute code before or after a specific action:
148
207
149
-
```javascript
150
-
module.exports=function (hookArgs) {
208
+
::: code-group
209
+
210
+
```javascript [thehook.mjs (ESM)]
211
+
exportdefaultfunction (hookArgs) {
151
212
if (hookArgs.prepareData.release) {
152
213
console.log('Before executing release build.')
153
214
}
154
215
}
155
216
```
156
217
157
-
**Replacement Hooks**
158
-
159
-
Hooks can replace the original CLI function (use sparingly):
160
-
161
-
```javascript
162
-
module.exports=function (hookArgs, $logger) {
163
-
return () => {
164
-
$logger.info('Replaced the original CLI function.')
218
+
```javascript [thehook.js (CommonJS)]
219
+
module.exports=function (hookArgs) {
220
+
if (hookArgs.prepareData.release) {
221
+
console.log('Before executing release build.')
165
222
}
166
223
}
167
224
```
168
225
169
-
**Note:** Replacement hooks should only be used in specific, rare cases. Before/after hooks are strongly recommended.
0 commit comments