|
| 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 or node.js scripts placed in the `hooks` subdirectory of your project. |
| 12 | +The hook file name must follow a strict naming scheme. The name declares: |
| 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 | +my-app/ |
| 24 | +├── index.js |
| 25 | +├── package.json |
| 26 | +└── hooks/ |
| 27 | + ├── before-prepare.js (this is a Node.js script) |
| 28 | + └── after-prepare (this is an executable file) |
| 29 | + |
| 30 | +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. |
| 31 | +Place all extending code into it. The CLI will execute them one after another but the order is not guaranteed. |
| 32 | + |
| 33 | +my-app/ |
| 34 | +├── index.js |
| 35 | +├── package.json |
| 36 | +└── hooks/ |
| 37 | + └── before-prepare (a directory) |
| 38 | + ├── hook1 (this is an executable file) |
| 39 | + └── hook2 (this is an executable file) |
| 40 | + |
| 41 | +Execute hooks as child process |
| 42 | +======================== |
| 43 | + |
| 44 | +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. |
| 45 | +The hook receives three variables in its OS environment: |
| 46 | + |
| 47 | + - 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` |
| 48 | + - NATIVESCRIPT-HOOK_FULL_PATH - the full path to the hook file name, for example `/home/user/app/hooks/after-prepare/myhook` |
| 49 | + - NATIVESCRIPT-VERSION - the version of the NativeScript CLI which invokes the hook, for example `1.5.2` |
| 50 | + |
| 51 | +Execute hooks in-process |
| 52 | +======================== |
| 53 | + |
| 54 | +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. |
| 55 | +The CLI assumes that this is a CommonJS module and calls its single exported function with four parameters. |
| 56 | +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) |
| 57 | + |
| 58 | + - $logger: ILogger. Use the members of this class to show messages to the user cooperating with the CLI internal state. |
| 59 | + - $projectData: IProjectData. Contains data about the project, like project directory, id, dependencies, etc. |
| 60 | + - $usbLiveSyncService:ILiveSyncService. Use this service to invoke LiveSync for device or emulator. |
| 61 | + - hookArgs: any. Contains all the parameters of the original function in the CLI which is being hooked. |
| 62 | + |
| 63 | +The hook must return a Promise. If the hook succeeds, it must fullfil the promise, but the fullfilment value is ignored. |
| 64 | +The hook can also rejects the promise with an instance of Error. The returned error can have two optional members controlling the CLI: |
| 65 | + |
| 66 | + - stopExecution: boolean - set this to false to let the CLI continue executing this command |
| 67 | + - 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 |
| 68 | + |
| 69 | +If these two members are not set, the CLI prints the returned error colored as fatal error and stops executing the current command. |
| 70 | + |
| 71 | +Furthermore, the global variable `$injector: IInjector` gives access to the CLi Dependency Injector, through which all code services are available. |
| 72 | + |
| 73 | +Supported commands for hooking |
| 74 | +============================== |
| 75 | + |
| 76 | +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