Skip to content

Commit e2a4388

Browse files
author
IceBuildUser
committed
Merge remote-tracking branch 'origin/release' into HEAD
2 parents 45e950d + cd4cde2 commit e2a4388

File tree

5 files changed

+92
-17
lines changed

5 files changed

+92
-17
lines changed

extending-cli.md

Lines changed: 80 additions & 0 deletions
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 according to your needs.
5+
Each time the CLI executes a particular command (for example `tns build`) it checks whether you have added any 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+
Hooks can be executable code, Node.js script, or even a directory containing other files. To be executed however, the hooks must be placed under the `hooks` subdirectory of your project.
12+
The hook name must follow a strict scheme, describing:
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 ccan create a sub-directory in the `hooks` directory using the naming convention described below.
33+
When you place code files into that folder, the CLI executes each file one after another, but not in a guaranteed order.
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 variable to check whether a LiveSync or normal build is in progress.
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 reject 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.

lib/commands/debug.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export class DebugPlatformCommand implements ICommand {
1616
canExecute(args: string[]): IFuture<boolean> {
1717
return ((): boolean => {
1818
this.$devicesService.initialize({ platform: this.debugService.platform, deviceId: this.$options.device }).wait();
19+
if(this.$options.emulator) {
20+
return true;
21+
}
22+
1923
if(this.$devicesService.deviceCount === 0) {
2024
this.$errors.failWithoutHelp("No devices detected. Connect a device and try again.");
2125
} else if (this.$devicesService.deviceCount > 1) {
@@ -34,16 +38,6 @@ export class DebugIOSCommand extends DebugPlatformCommand {
3438
$options: IOptions) {
3539
super($iOSDebugService, $devicesService, $errors, $options);
3640
}
37-
38-
canExecute(args: string[]): IFuture<boolean> {
39-
return ((): boolean => {
40-
if(this.$options.emulator) {
41-
return true;
42-
}
43-
44-
return super.canExecute(args).wait();
45-
}).future<boolean>()();
46-
}
4741
}
4842
$injector.registerCommand("debug|ios", DebugIOSCommand);
4943

@@ -54,9 +48,5 @@ export class DebugAndroidCommand extends DebugPlatformCommand {
5448
$options: IOptions) {
5549
super($androidDebugService, $devicesService, $errors, $options);
5650
}
57-
58-
canExecute(args: string[]): IFuture<boolean> {
59-
return super.canExecute(args);
60-
}
6151
}
6252
$injector.registerCommand("debug|android", DebugAndroidCommand);

lib/common

lib/services/android-debug-service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class AndroidDebugService implements IDebugService {
2020
private $hostInfo: IHostInfo,
2121
private $errors: IErrors,
2222
private $opener: IOpener,
23-
private $config: IConfiguration) { }
23+
private $config: IConfiguration,
24+
private $androidDeviceDiscovery: Mobile.IDeviceDiscovery) { }
2425

2526
public get platform() { return "android"; }
2627

@@ -41,6 +42,10 @@ class AndroidDebugService implements IDebugService {
4142
private debugOnEmulator(): IFuture<void> {
4243
return (() => {
4344
this.$platformService.deployOnEmulator(this.platform).wait();
45+
// Assure we've detected the emulator as device
46+
// For example in case deployOnEmulator had stated new emulator instance
47+
// we need some time to detect it. Let's force detection.
48+
this.$androidDeviceDiscovery.startLookingForDevices().wait();
4449
this.debugOnDevice().wait();
4550
}).future<void>()();
4651
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"ffi": "https://github.com/icenium/node-ffi/tarball/v2.0.0.1",
4040
"fibers": "https://github.com/icenium/node-fibers/tarball/v1.0.6.3",
4141
"filesize": "3.1.2",
42-
"gaze": "0.5.1",
42+
"gaze": "0.5.2",
4343
"gulp": "3.9.0",
4444
"iconv-lite": "0.4.11",
4545
"inquirer": "0.9.0",

0 commit comments

Comments
 (0)