Skip to content

Commit cd7f808

Browse files
rosen-vladimirovDimitar Kerezov
authored andcommitted
Add docs and improve liveSync operations when called multiple times for same dir
1 parent f45f1e3 commit cd7f808

File tree

3 files changed

+357
-131
lines changed

3 files changed

+357
-131
lines changed

PublicAPI.md

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ This document describes all methods that can be invoked when NativeScript CLI is
77
const tns = require("nativescript");
88
```
99

10+
# Contents
11+
* [projectService](#projectservice)
12+
* [createProject](#createproject)
13+
* [isValidNativeScriptProject](#isvalidnativescriptproject)
14+
* [extensibilityService](#extensibilityservice)
15+
* [installExtension](#installextension)
16+
* [uninstallExtension](#uninstallextension)
17+
* [getInstalledExtensions](#getinstalledextensions)
18+
* [loadExtensions](#loadextensions)
19+
* [settingsService](#settingsservice)
20+
* [setSettings](#setsettings)
21+
* [npm](#npm)
22+
* [install](#install)
23+
* [uninstall](#uninstall)
24+
* [search](#search)
25+
* [view](#view)
26+
* [analyticsService](#analyticsservice)
27+
* [startEqatecMonitor](#starteqatecmonitor)
28+
* [debugService](#debugservice)
29+
* [debug](#debug)
30+
* [liveSyncService](#livesyncservice)
31+
* [liveSync](#livesync)
32+
* [stopLiveSync](#stopLiveSync)
33+
* [events](#events)
34+
35+
1036
## Module projectService
1137

1238
`projectService` modules allow you to create new NativeScript application.
@@ -498,6 +524,191 @@ tns.debugService.debug(debugData, debugOptions)
498524
.catch(err => console.log(`Unable to start debug operation, reason: ${err.message}.`));
499525
```
500526
527+
## liveSyncService
528+
Used to LiveSync changes on devices. The operation can be started for multiple devices and stopped for each of them. During LiveSync operation, the service will emit different events based on the action that's executing.
529+
530+
### liveSync
531+
Starts a LiveSync operation for specified devices. During the operation, application may have to be rebuilt (for example in case a change in App_Resources is detected).
532+
By default the LiveSync operation will start file system watcher for `<project dir>/app` directory and any change in it will trigger a LiveSync operation.
533+
After calling the method once, you can add new devices to the same LiveSync operation by calling the method again with the new device identifiers.
534+
535+
> NOTE: Consecutive calls to `liveSync` method for the same project will execute the initial sync (deploy and fullSync) only for new device identifiers. So in case the first call is for devices with ids [ 'A' , 'B' ] and the second one is for devices with ids [ 'B', 'C' ], the initial sync will be executed only for device with identifier 'C'.
536+
537+
> NOTE: In case a consecutive call to `liveSync` method requires change in the pattern for watching files (i.e. `liveSyncData.syncAllFiles` option has changed), current watch operation will be stopped and a new one will be started.
538+
539+
* Definition
540+
```TypeScript
541+
/**
542+
* Starts LiveSync operation by rebuilding the application if necessary and starting watcher.
543+
* @param {ILiveSyncDeviceInfo[]} deviceDescriptors Describes each device for which we would like to sync the application - identifier, outputPath and action to rebuild the app.
544+
* @param {ILiveSyncInfo} liveSyncData Describes the LiveSync operation - for which project directory is the operation and other settings.
545+
* @returns {Promise<void>}
546+
*/
547+
liveSync(deviceDescriptors: ILiveSyncDeviceInfo[], liveSyncData: ILiveSyncInfo): Promise<void>;
548+
```
549+
550+
* Usage:
551+
```JavaScript
552+
const projectDir = "myProjectDir";
553+
const androidDeviceDescriptor = {
554+
identifier: "4df18f307d8a8f1b",
555+
buildAction: () => {
556+
return tns.localBuildService.build("Android", { projectDir, bundle: false, release: false, buildForDevice: true });
557+
},
558+
outputPath: null
559+
};
560+
561+
const iOSDeviceDescriptor = {
562+
identifier: "12318af23ebc0e25",
563+
buildAction: () => {
564+
return tns.localBuildService.build("iOS", { projectDir, bundle: false, release: false, buildForDevice: true });
565+
},
566+
outputPath: null
567+
};
568+
569+
const liveSyncData = {
570+
projectDir,
571+
skipWatcher: false,
572+
watchAllFiles: false,
573+
useLiveEdit: false
574+
};
575+
576+
tns.liveSyncService.liveSync([ androidDeviceDescriptor, iOSDeviceDescriptor ], liveSyncData)
577+
.then(() => {
578+
console.log("LiveSync operation started.");
579+
}, err => {
580+
console.log("An error occurred during LiveSync", err);
581+
});
582+
```
583+
584+
### stopLiveSync
585+
Stops LiveSync operation. In case deviceIdentifires are passed, the operation will be stopped only for these devices.
586+
587+
* Definition
588+
```TypeScript
589+
/**
590+
* Stops LiveSync operation for specified directory.
591+
* @param {string} projectDir The directory for which to stop the operation.
592+
* @param {string[]} @optional deviceIdentifiers Device ids for which to stop the application. In case nothing is passed, LiveSync operation will be stopped for all devices.
593+
* @returns {Promise<void>}
594+
*/
595+
stopLiveSync(projectDir: string, deviceIdentifiers?: string[]): Promise<void>;
596+
```
597+
598+
* Usage
599+
```JavaScript
600+
const projectDir = "myProjectDir";
601+
const deviceIdentifiers = [ "4df18f307d8a8f1b", "12318af23ebc0e25" ];
602+
tns.liveSyncService.stopLiveSync(projectDir, deviceIdentifiers)
603+
.then(() => {
604+
console.log("LiveSync operation stopped.");
605+
}, err => {
606+
console.log("An error occurred during stopage.", err);
607+
});
608+
```
609+
610+
### Events
611+
`liveSyncService` raises several events in order to provide information for current state of the operation.
612+
* liveSyncStarted - raised whenever CLI starts a LiveSync operation for specific device. When `liveSync` method is called, the initial LiveSync operation will emit `liveSyncStarted` for each specified device. After that the event will be emitted only in case when liveSync method is called again with different device instances. The event is raised with the following data:
613+
```TypeScript
614+
{
615+
projectDir: string;
616+
deviceIdentifier: string;
617+
applicationIdentifier: string;
618+
}
619+
```
620+
621+
Example:
622+
```JavaScript
623+
tns.liveSyncService.on("liveSyncStarted", data => {
624+
console.log(`Started LiveSync on ${data.deviceIdentifier} for ${data.applicationIdentifier}.`);
625+
});
626+
```
627+
628+
* liveSyncExecuted - raised whenever CLI finishes a LiveSync operation for specific device. When `liveSync` method is called, the initial LiveSync operation will emit `liveSyncExecuted` for each specified device once it finishes the operation. After that the event will be emitted whenever a change is detected (in case file system watcher is staretd) and the LiveSync operation is executed for each device. The event is raised with the following data:
629+
```TypeScript
630+
{
631+
projectDir: string;
632+
deviceIdentifier: string;
633+
applicationIdentifier: string;
634+
/**
635+
* Full paths to files synced during the operation. In case the `syncedFiles.length` is 0, the operation is "fullSync" (i.e. all project files are synced).
636+
*/
637+
syncedFiles: string[];
638+
}
639+
```
640+
641+
Example:
642+
```JavaScript
643+
tns.liveSyncService.on("liveSyncExecuted", data => {
644+
console.log(`Executed LiveSync on ${data.deviceIdentifier} for ${data.applicationIdentifier}. Uploaded files are: ${syncedFiles.join(" ")}.`);
645+
});
646+
```
647+
648+
* liveSyncStopped - raised when LiveSync operation is stopped. The event will be raised when the operation is stopped for each device and will be raised when the whole operation is stopped. The event is raised with the following data:
649+
```TypeScript
650+
{
651+
projectDir: string;
652+
/**
653+
* Passed only when the LiveSync operation is stopped for a specific device. In case it is not passed, the whole LiveSync operation is stopped.
654+
*/
655+
deviceIdentifier?: string;
656+
}
657+
```
658+
659+
Example:
660+
```JavaScript
661+
tns.liveSyncService.on("liveSyncStopped", data => {
662+
if (data.deviceIdentifier) {
663+
console.log(`Stopped LiveSync on ${data.deviceIdentifier} for ${data.projectDir}.`);
664+
} else {
665+
console.log(`Stopped LiveSync for ${data.projectDir}.`);
666+
}
667+
});
668+
```
669+
670+
* error - raised whenever an error is detected during LiveSync operation. The event is raised for specific device. Once an error is detected, the event will be raised and the LiveSync operation will be stopped for this device, i.e. `liveSyncStopped` event will be raised for it. The event is raised with the following data:
671+
```TypeScript
672+
{
673+
projectDir: string;
674+
deviceIdentifier: string;
675+
applicationIdentifier: string;
676+
error: Error;
677+
}
678+
```
679+
680+
Example:
681+
```JavaScript
682+
tns.liveSyncService.on("error", data => {
683+
console.log(`Error detected during LiveSync on ${data.deviceIdentifier} for ${data.projectDir}. Error: ${err.message}.`);
684+
});
685+
```
686+
687+
* fileChanged - raised when a watched file is modified. The event is raised witht he following data:
688+
```TypeScript
689+
{
690+
projectDir: string;
691+
/**
692+
* Device identifiers on which the file will be LiveSynced.
693+
*/
694+
deviceIdentifiers: string[];
695+
applicationIdentifier: string;
696+
modifiedFile: string;
697+
698+
/**
699+
* File System event - "add", "addDir", "change", "unlink", "unlinkDir".
700+
*/
701+
event: string;
702+
}
703+
```
704+
705+
Example:
706+
```JavaScript
707+
tns.liveSyncService.on("fileChanged", data => {
708+
console.log(`Detected file changed: ${data.modifiedFile} in ${data.projectDir}. Will start LiveSync operation on ${data.deviceIdentifiers.join(", ")}.`);
709+
});
710+
```
711+
501712
## How to add a new method to Public API
502713
CLI is designed as command line tool and when it is used as a library, it does not give you access to all of the methods. This is mainly implementation detail. Most of the CLI's code is created to work in command line, not as a library, so before adding method to public API, most probably it will require some modification.
503714
For example the `$options` injected module contains information about all `--` options passed on the terminal. When the CLI is used as a library, the options are not populated. Before adding method to public API, make sure its implementation does not rely on `$options`.

lib/definitions/livesync.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ interface IFSWatcher extends NodeJS.EventEmitter {
5757

5858
interface ILiveSyncProcessInfo {
5959
timer: NodeJS.Timer;
60-
watcher: IFSWatcher;
60+
watcherInfo: {
61+
watcher: IFSWatcher,
62+
pattern: string | string[]
63+
};
6164
actionsChain: Promise<any>;
6265
isStopped: boolean;
6366
deviceDescriptors: ILiveSyncDeviceInfo[];
@@ -134,9 +137,10 @@ interface ILiveSyncService {
134137
/**
135138
* Stops LiveSync operation for specified directory.
136139
* @param {string} projectDir The directory for which to stop the operation.
140+
* @param {string[]} @optional deviceIdentifiers Device ids for which to stop the application. In case nothing is passed, LiveSync operation will be stopped for all devices.
137141
* @returns {Promise<void>}
138142
*/
139-
stopLiveSync(projectDir: string): Promise<void>;
143+
stopLiveSync(projectDir: string, deviceIdentifiers?: string[]): Promise<void>;
140144
}
141145

142146
interface ILiveSyncWatchInfo {

0 commit comments

Comments
 (0)