Skip to content

Commit 23b3f9c

Browse files
committed
docs: add documentation to the android-livesync-library
1 parent 9ee7967 commit 23b3f9c

File tree

2 files changed

+215
-2
lines changed

2 files changed

+215
-2
lines changed

lib/definitions/livesync.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ interface IAndroidLivesyncTool {
409409
*/
410410
removeFiles(filePaths: string[]): Promise<boolean[]>;
411411
/**
412-
* Sends doSyncOperation that will be handeled by the runtime.
412+
* Sends doSyncOperation that will be handled by the runtime.
413413
* @param operationId - The identifier of the operation
414-
* @param timeout - The timeout in miliseconds
414+
* @param timeout - The timeout in milliseconds
415415
* @returns {Promise<void>}
416416
*/
417417
sendDoSyncOperation(operationId: string, timeout?: number): Promise<void>;
@@ -424,6 +424,10 @@ interface IAndroidLivesyncTool {
424424
* @param operationId - The identifier of the operation.
425425
*/
426426
isOperationInProgress(operationId: string): boolean;
427+
428+
/**
429+
* Closes the current socket connection.
430+
*/
427431
end(): void;
428432
}
429433

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# android-livesync-library
2+
Library for livesyncing changes to a NativeScript application on Android.
3+
4+
## Usage
5+
The library has a few public methods that allow file manipulation to the files of a NativeScript application and provide control for refreshing the application. Restarting the application if necessary should be done by the user of this library.
6+
7+
### Getting an instance
8+
9+
* Example:
10+
```JavaScript
11+
const globalModulesPath = require("global-modules-path");
12+
const cliPath = globalModulesPath.getPath("nativescript", "tns");
13+
cli = require(cliPath);
14+
15+
const liveSyncTool = cli.androidLivesyncLibrary;
16+
```
17+
18+
19+
### Calling connect
20+
Connect method will establish a fresh socket connection with the application. The method takes a configuration as a parameter.
21+
22+
* Definition
23+
```TypeScript
24+
interface ILivesyncToolConfiguration {
25+
appIdentifier: string;
26+
deviceIdentifier: string;
27+
appPlatformsPath: string; // path to /c/myprojects/myapp/app/platforms/android/app/src/main/assets/app/
28+
localHostAddress?: string;
29+
errorHandler?: any;
30+
}
31+
32+
/**
33+
* Creates new socket connection.
34+
* @param configuration - The configuration to the socket connection.
35+
* @returns {Promise<void>}
36+
*/
37+
connect(configuration: ILivesyncToolConfiguration): Promise<void>;
38+
```
39+
40+
* Example:
41+
```JavaScript
42+
43+
var configuration = {
44+
appPlatformsPath: "/c/myprojects/myapp/app/platforms/android/app/src/main/assets/app/",
45+
fullApplicationName: "com.tns.myapp",
46+
deviceIdentifier: "aaaaaaaa"
47+
}
48+
49+
liveSyncTool.connect(configuration)
50+
```
51+
52+
The method returns a promise which is resolved once the connection is established. There is a 30 seconds timeout for establishing the connection. In order the connection to be established successfully, the app must be started.
53+
54+
### Calling sendFile
55+
Send file will create/update the file with the file content it reads from the filePath that is provided. It will compute the relative path based on the fullApplicationName provided in configuration that was passed to the connect method. This method resolves its promise once the file is written to the output stream of the socket. To be sure the all files have been read and saved by the runtime see sendDoSyncOperation.
56+
57+
* Definition
58+
```TypeScript
59+
/**
60+
* Sends a file through the socket.
61+
* @param filePath - The full path to the file.
62+
* @returns {Promise<void>}
63+
*/
64+
sendFile(filePath: string): Promise<void>;
65+
```
66+
67+
* Example:
68+
```JavaScript
69+
liveSyncTool.sendFile("/c/myprojects/myapp/app/platforms/android/app/src/main/assets/app/index.js");
70+
```
71+
72+
### Calling sendFiles
73+
This method takes an array of file paths as an argument and sends their content to the application.
74+
75+
* Definition
76+
```TypeScript
77+
/**
78+
* Sends files through the socket.
79+
* @param filePaths - Array of files that will be send by the socket.
80+
* @returns {Promise<void>}
81+
*/
82+
sendFiles(filePaths: string[]): Promise<void>;
83+
```
84+
85+
* Example:
86+
```JavaScript
87+
liveSyncTool.sendFile([
88+
"/c/myprojects/myapp/app/platforms/android/app/src/main/assets/app/index.js"
89+
"/c/myprojects/myapp/app/platforms/android/app/src/main/assets/app/test.js"
90+
"/c/myprojects/myapp/app/platforms/android/app/src/main/assets/app/page.js"
91+
]);
92+
```
93+
94+
### Calling sendDirectory
95+
This method takes a path to a directory, enumerates the files recursively and sends the to the device.
96+
97+
* Definition
98+
```TypeScript
99+
/**
100+
* Sends all files from directory by the socket.
101+
* @param directoryPath - The path to the directory which files will be send by the socket.
102+
* @returns {Promise<void>}
103+
*/
104+
sendDirectory(directoryPath: string): Promise<void>;
105+
```
106+
107+
* Example:
108+
```JavaScript
109+
liveSyncTool.sendDirectory("/c/myprojects/myapp/app/platforms/android/app/src/main/assets/app");
110+
```
111+
112+
### Calling removeFile
113+
When called, removeFile will compute the relative path based on the fullApplicationName provided in configuration that was passed to the connect method and delete the corresponding file/directory on the device.
114+
115+
* Definition
116+
```TypeScript
117+
/**
118+
* Removes file
119+
* @param filePath - The full path to the file.
120+
* @returns {Promise<boolean>}
121+
*/
122+
removeFile(filePath: string): Promise<boolean>;
123+
```
124+
125+
* Example:
126+
```JavaScript
127+
liveSyncTool.removeFile("/c/myprojects/myapp/app/platforms/android/app/src/main/assets/app/index.js");
128+
```
129+
130+
### Calling removeFiles
131+
When called, removeFiles will compute the relative paths based on the fullApplicationName provided in configuration that was passed to the connect method and delete the corresponding files/directories on the device.
132+
133+
* Definition
134+
```TypeScript
135+
/**
136+
* Removes files
137+
* @param filePaths - Array of files that will be removed.
138+
* @returns {Promise<boolean[]>}
139+
*/
140+
removeFiles(filePaths: string[]): Promise<boolean[]>;
141+
```
142+
143+
* Example:
144+
```JavaScript
145+
liveSyncTool.removeFiles([
146+
"/c/myprojects/myapp/app/platforms/android/app/src/main/assets/app/index.js"
147+
"/c/myprojects/myapp/app/platforms/android/app/src/main/assets/app/test.js"
148+
"/c/myprojects/myapp/app/platforms/android/app/src/main/assets/app/page.js"
149+
]);
150+
```
151+
152+
### Calling sendDoSyncOperation
153+
When called, sendDoSyncOperation will tell the runtime to execute a script that will refresh the application(this will render changes to the .html and .css files). This method accepts an optional parameter - operationId. It can be used for status check of the operation. The promise returned from this method will be resolved when the application has read the operation and started executing it. This can be used as a sync point - once this promise is resolved, the user can be sure that all other operations have been read and executed by the application. The operation accepts an operation id
154+
155+
* Definition
156+
```TypeScript
157+
/**
158+
* Sends doSyncOperation that will be handeled by the runtime.
159+
* @param operationId - The identifier of the operation
160+
* @param timeout - The timeout in miliseconds
161+
* @returns {Promise<void>}
162+
*/
163+
sendDoSyncOperation(operationId: string, timeout?: number): Promise<void>;
164+
```
165+
166+
* Example:
167+
```JavaScript
168+
const operationId = liveSyncTool.generateOperationIdentifier();
169+
await liveSyncTool.sendDoSyncOperation(operationId);
170+
```
171+
172+
### Calling end
173+
End will close the current liveSync socket. Any sync operations that are still in progress will be rejected.
174+
175+
* Definition
176+
```TypeScript
177+
/**
178+
* Closes the current socket connection.
179+
*/
180+
end(): void;
181+
```
182+
183+
* Example:
184+
```JavaScript
185+
liveSyncTool.end();
186+
```
187+
188+
## Protocol:
189+
190+
Application input
191+
192+
|Operation Name(not send) | Operation | Operation id | File Name Length Size | File Name Length | File Name | File Content Length Size | File Content Length | Header Hash | File Content | File hash |
193+
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
194+
| runSync: | 9 | 32bit | | | | | | md5 hash | | |
195+
| create: | 8 | | 1 | 7 | ./a.txt | 2 | 11 | md5 hash | fileContent | md5 hash |
196+
| delete: | 7 | | 1 | 3 | ./a | | | md5 hash | | |
197+
198+
Application output on connect
199+
200+
| Protocol Version length | Protocol Version String | Application Identifier |
201+
| --- | --- | --- |
202+
| 1 byte | "0.1.0" | "org.nativescript.myapp" |
203+
204+
Application output after connect
205+
206+
| Report Name(not send) | Report Code | Payload |
207+
| --- | --- | --- |
208+
| Error | 1 | Error message string |
209+
| Sync end | 2 | Sync operation uid |

0 commit comments

Comments
 (0)