Skip to content

Commit 90e8be3

Browse files
committed
Merge pull request #1473 from NativeScript/hooks-docs
Documentation for hooks
2 parents a24b80b + a514325 commit 90e8be3

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

extending-cli.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Extending the CLI
2+
=================
3+
4+
NativeScript CLI allows you to extend its behavior and customize it for your project.
5+
When the CLI executes a particular command (for example `tns build`) it checks whether you have added such extending hooks and executes them.
6+
Additionally, plugins can use these hooks to control the compilation of the program.
7+
8+
What are the hooks
9+
==================
10+
11+
The hooks are any executable code, Node.js script or a directory placed under the `hooks` subdirectory of your project.
12+
The hook name must follow a strict scheme. The name describes:
13+
14+
- the action being hooked
15+
- whether the user code must be called before or after the action
16+
17+
For example, to execute your code before the `prepare` command starts, create a file named `before-prepare` and place it in the `hooks` directory.
18+
To execute your code after a command completes, create `after-prepare`.
19+
All file extensions (if present) are accepted but JavaScript files are treated specially, which is explained bellow.
20+
21+
The project structure looks like this:
22+
23+
```
24+
my-app/
25+
├── index.js
26+
├── package.json
27+
└── hooks/
28+
├── before-prepare.js (this is a Node.js script)
29+
└── after-prepare (this is an executable file)
30+
```
31+
32+
To support multiple scripts extending the same action, you can use a different approach. Create a sud-directory in the `hooks` directory using the naming convention described above.
33+
Place all extending code into it. The CLI will execute them one after another but the order is not guaranteed.
34+
35+
```
36+
my-app/
37+
├── index.js
38+
├── package.json
39+
└── hooks/
40+
└── before-prepare (a directory)
41+
├── hook1 (this is an executable file)
42+
└── hook2 (this is an executable file)
43+
```
44+
45+
Execute hooks as child process
46+
========================
47+
48+
If your hook is an executable file which is not a Node.js code, NativeScript executes it using the normal OS API for creating a child process. This gives you the flexibility to write it in any way you want.
49+
The hook receives three variables in its OS environment:
50+
51+
- NATIVESCRIPT-COMMANDLINE - the full command line which triggered the hook execution, for example: `/usr/local/bin/node /usr/local/lib/node_modules/nativescript/bin/nativescript-cli.js build android`
52+
- NATIVESCRIPT-HOOK_FULL_PATH - the full path to the hook file name, for example `/home/user/app/hooks/after-prepare/myhook`
53+
- NATIVESCRIPT-VERSION - the version of the NativeScript CLI which invokes the hook, for example `1.5.2`
54+
55+
Execute hooks in-process
56+
========================
57+
58+
When your hook is a Node.js script, the CLI executes it in-process. This gives you access to the entire internal state of the CLI and all of its functions.
59+
The CLI assumes that this is a CommonJS module and calls its single exported function with four parameters.
60+
The type of the parameters are described in the .d.ts files which are part of the CLI source code [here](https://github.com/NativeScript/nativescript-cli/tree/master/lib/definitions) and [here](https://github.com/telerik/mobile-cli-lib/tree/master/definitions)
61+
62+
- $logger: ILogger. Use the members of this class to show messages to the user cooperating with the CLI internal state.
63+
- $projectData: IProjectData. Contains data about the project, like project directory, id, dependencies, etc.
64+
- $usbLiveSyncService:ILiveSyncService. Use this service to invoke LiveSync for device or emulator.
65+
- hookArgs: any. Contains all the parameters of the original function in the CLI which is being hooked.
66+
67+
The hook must return a Promise. If the hook succeeds, it must fullfil the promise, but the fullfilment value is ignored.
68+
The hook can also rejects the promise with an instance of Error. The returned error can have two optional members controlling the CLI:
69+
70+
- stopExecution: boolean - set this to false to let the CLI continue executing this command
71+
- errorAsWarning: boolean: set this to treat the returned error as warning. The CLI prints the error.message colored as a warning and continues executing the current command
72+
73+
If these two members are not set, the CLI prints the returned error colored as fatal error and stops executing the current command.
74+
75+
Furthermore, the global variable `$injector: IInjector` gives access to the CLi Dependency Injector, through which all code services are available.
76+
77+
Supported commands for hooking
78+
==============================
79+
80+
Only the `prepare` comand can be hooked. Internally, this command is also invoked during build and livesync. The later commands will execute the prepare hooks at the proper moment of their execution.

0 commit comments

Comments
 (0)