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
@@ -4,11 +4,12 @@ description: A Guide to using CLI hooks.
4
4
contributors:
5
5
- jcassidyav
6
6
---
7
+
7
8
## Overview
8
9
9
10
NativeScript hooks are executable pieces of code or Node.js scripts that can be added by application or plugin developers to customize the execution of particular NativeScript commands. They provide the power to perform special activities by plugging into different parts of the build process of your application.
10
11
11
-
Hooks are added to the `hooks/` folder of a project by plugins, or are specified in the `nativescript.config.ts` by an application.
12
+
Hooks are added to the `hooks/` folder of a project by plugins, or are specified in the `nativescript.config.ts` by an application.
12
13
13
14
For example, when `ns prepare ...` is executed, all script files in the `hooks/before-prepare/` and `hooks/after-prepare/` folders are executed as well.
14
15
@@ -21,13 +22,15 @@ For versions of the NativeScript CLI < 9.0 hooks must be written in CommonJS wit
21
22
The NativeScript CLI supports two different ways of executing hooks:
22
23
23
24
**1. In-Process Execution**
25
+
24
26
- Available only for JavaScript hooks
25
27
- Executes within the CLI process
26
28
- Provides access to NativeScript CLI services via dependency injection
27
29
- Determined by the presence of `module.exports` statement
28
30
-**Recommended approach** for writing hooks
29
31
30
32
**2. Spawned Execution**
33
+
31
34
- Executed via Node.js's `child_process.spawn` function
32
35
- Run from the project's root directory
33
36
- Cannot access NativeScript CLI services
@@ -40,7 +43,7 @@ The NativeScript CLI supports two different ways of executing hooks:
40
43
To write an in-process hook, use the following module definition:
41
44
42
45
```javascript
43
-
module.exports=function() {
46
+
module.exports=function() {
44
47
// Hook implementation
45
48
}
46
49
```
@@ -70,36 +73,37 @@ Then `hookArgs` will have the following structure:
70
73
**Using hookArgs in your hook:**
71
74
72
75
```javascript
73
-
module.exports=function(hookArgs) {
74
-
console.log(hookArgs.projectData);
76
+
module.exports=function(hookArgs) {
77
+
console.log(hookArgs.projectData)
75
78
}
76
79
```
77
80
78
81
**Dependency Injection**
79
82
80
83
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.
NativeScript CLI supports asynchronous code in hooks. If executing async code, you must return a Promise:
110
114
111
115
```javascript
112
-
var mkdirp =require('mkdirp');
116
+
var mkdirp =require('mkdirp')
113
117
114
-
module.exports=function($logger) {
115
-
returnnewPromise(function(resolve, reject) {
116
-
mkdirp('somedir', function(err) {
118
+
module.exports=function($logger) {
119
+
returnnewPromise(function(resolve, reject) {
120
+
mkdirp('somedir', function(err) {
117
121
if (err) {
118
-
reject(err);
122
+
reject(err)
119
123
} else {
120
-
resolve();
124
+
resolve()
121
125
}
122
-
});
123
-
});
126
+
})
127
+
})
124
128
}
125
129
```
126
130
127
131
## Spawned Hooks
128
132
129
133
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:
130
134
131
-
| Environment Variable | Description |
132
-
|---------------------|-------------|
133
-
|`TNS-VERSION`| The version of the NativeScript CLI |
134
-
|`TNS-HOOK_FULL_PATH`| The full path to the executed hook |
135
-
|`TNS-COMMANDLINE`| The exact command-line arguments passed to NativeScript CLI (e.g., `tns run ios --emulator`) |
@@ -241,15 +246,19 @@ Define your hooks under the `nativescript` property:
241
246
### Hook Configuration Properties
242
247
243
248
#### type (Required)
249
+
244
250
Specifies when the hook should execute. Format: `before-<hookName>` or `after-<hookName>`
245
251
246
252
#### script (Required)
253
+
247
254
The relative path from the plugin root to the hook implementation file.
248
255
249
256
#### inject (Optional)
257
+
250
258
Boolean property indicating whether the hook should be executed in-process (`true`) or spawned (`false`). When `inject: true`, the hook can access NativeScript CLI services.
251
259
252
260
#### name (Optional)
261
+
253
262
Custom name for the hook. Defaults to the plugin package name.
254
263
255
264
**Example with custom name:**
@@ -282,13 +291,13 @@ export default {
282
291
hooks: [
283
292
{
284
293
type: 'before-prepare',
285
-
script: './scripts/hooks/before-prepare.js'
294
+
script: './scripts/hooks/before-prepare.js',
286
295
},
287
296
{
288
297
type: 'after-prepare',
289
-
script: './scripts/hooks/after-prepare.js'
290
-
}
291
-
]
298
+
script: './scripts/hooks/after-prepare.js',
299
+
},
300
+
],
292
301
} asNativeScriptConfig
293
302
```
294
303
@@ -309,28 +318,28 @@ hooks: [
309
318
310
319
The following hook types are available (prefix with `before-` or `after-`):
311
320
312
-
| Hook Name | Description | Execution Context |
313
-
|-----------|-------------|-------------------|
314
-
|`buildAndroidPlugin`| Builds aar file for Android plugin | Runs during `prepareNativeApp`|
315
-
|`buildAndroid`| Builds Android app | During Android build process |
316
-
|`buildIOS`| Builds iOS app | During iOS build process |
317
-
|`checkEnvironment`| Validates project environment | Runs during `ns doctor`, `ns clean`, and most build commands |
318
-
|`checkForChanges`| Detects changes during watch | NativeScript CLI checks application state to decide if rebuild/reinstall/restart is needed |
319
-
|`install`| Application installed to device/emulator | After app installation |
320
-
|`prepare`| Compiles webpack and prepares native app | Prepares the application in platforms folder |
321
-
|`prepareNativeApp`| Prepares the actual native app | Runs during `prepare`/`watch` hook |
322
-
|`resolveCommand`| Resolves command and arguments | Runs before all CLI commands |
323
-
|`watch`| Sets up watchers for live sync | During `prepare` hook for live development |
324
-
|`watchPatterns`| Sets up watch patterns | During `watch` hook |
Starting with NativeScript 9.0 CLI (`npm install -g nativescript`), a new commmand `ns hooks` is available.
376
+
As described above these hooks are installed automatically through npm postinstall scripts included in the plugin package.
368
377
369
-
As described above `postinstall` scripts are used by plugins to install hooks to the correct location for execution, this is not compatible with users
370
-
using `npm install --ignore-scripts` ( or other settings which prevent script execution ) given that plugin that requires hooks utilises post install events to install the hooks.
378
+
However, if you (or your CI environment) install dependencies with:
379
+
380
+
```
381
+
npm install --ignore-scripts
382
+
```
383
+
384
+
then those postinstall scripts don’t run, which means:
385
+
386
+
- Plugin hooks aren’t copied to the correct location (`hooks/` folder).
387
+
- Builds may fail or certain plugin functionality won’t work.
388
+
389
+
Starting with NativeScript 9.0 CLI (`npm install -g nativescript`), a new command was introduced:
390
+
391
+
```
392
+
ns hooks
393
+
```
394
+
395
+
This command installs all plugin hooks after dependencies have been installed.
396
+
397
+
So if your environment blocks postinstall scripts, you can now safely do:
398
+
399
+
```
400
+
npm install --ignore-scripts
401
+
ns hooks
402
+
```
371
403
372
-
The new `ns hooks` command resolves this by providing a mechanism to install plugin hooks after a `npm install`.
404
+
The new `ns hooks` command will install the hooks into the proper project locations (as postinstall would have done).
373
405
374
-
**Commands**
406
+
**Available Commands**
375
407
376
-
| Command | Description |
377
-
| ---------- | ------------ |
378
-
|`ns hooks`| Lists the hooks that are in the installed plugins ( also `ns hooks list` ). |
379
-
|`ns hooks install`| Installs the hooks. |
380
-
|`ns hooks lock`| Creates a `nativescript-lock.json` file, this is a list of hooks per plugin and a hash of the script for each hook. |
381
-
|`ns hooks verify`| Compares the hooks in the plugins with what is specified in the `nativescript-lock.json` file, failing if a hook is not listed or the hash is not the same. |
|`ns hooks`| Lists the hooks that are in the installed plugins ( also `ns hooks list` ).|
411
+
|`ns hooks install`| Installs the hooks. |
412
+
|`ns hooks lock`| Creates a `nativescript-lock.json` file, this is a list of hooks per plugin and a hash of the script for each hook.|
413
+
|`ns hooks verify`| Compares the hooks in the plugins with what is specified in the `nativescript-lock.json` file, failing if a hook is not listed or the hash is not the same. |
382
414
383
-
**Example usage**
415
+
**For extra peace of mind**
384
416
385
-
* Modify/Create `.npmrc` in the project root adding `ignore-scripts=true`
386
-
* After `npm i` run `ns hooks install`
417
+
Typically the contents of the hook scripts do not change in plugins from version to version, to prevent unexpecected changes to hooks you can utilize the `lock` command.
387
418
388
-
For extra peace of mind:
419
+
This will:
389
420
390
-
Run `ns hooks lock` and `ns hooks install` will fail if any of the hooks have changed.
421
+
- Create a `nativescript-lock.json` file containing details of the current plugin hooks.
422
+
- Ensure that any future `ns hooks install` or `ns hooks verify` invocations will fail if any new hooks are introduced by dependencies or if these hook scripts differ from what was previously installed.
391
423
424
+
i.e. run `ns hooks lock` and `ns hooks install` will fail if any of the hooks have changed since the last time `ns hooks lock` has been executed.
0 commit comments