Skip to content

Commit 55c420a

Browse files
author
Dimitar Tachev
authored
Merge pull request #4093 from NativeScript/fatme/preview-api-livesync
feat(preview-api): expose public api for starting the livesync operation to preview app
2 parents b3b2044 + e05af32 commit 55c420a

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

lib/definitions/livesync.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ interface ILiveSyncInfo extends IProjectDir, IEnvOptions, IBundle, IRelease, IOp
166166
* Defines the timeout in seconds {N} CLI will wait to find the inspector socket port from device's logs.
167167
* If not provided, defaults to 10seconds.
168168
*/
169-
timeout: string;
169+
timeout?: string;
170170
}
171171

172172
interface IHasSyncToPreviewAppOption {

lib/definitions/preview-app-livesync.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ declare global {
2424

2525
interface IPreviewQrCodeService {
2626
getPlaygroundAppQrCode(options?: IPlaygroundAppQrCodeOptions): Promise<IDictionary<IQrCodeImageData>>;
27+
getLiveSyncQrCode(url: string): Promise<IQrCodeImageData>;
2728
printLiveSyncQrCode(options: IPrintLiveSyncOptions): Promise<void>;
2829
}
2930

lib/services/livesync/livesync-service.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
3939
private $analyticsService: IAnalyticsService,
4040
private $usbLiveSyncService: DeprecatedUsbLiveSyncService,
4141
private $previewAppLiveSyncService: IPreviewAppLiveSyncService,
42+
private $previewQrCodeService: IPreviewQrCodeService,
43+
private $previewSdkService: IPreviewSdkService,
4244
private $hmrStatusService: IHmrStatusService,
4345
private $injector: IInjector) {
4446
super();
@@ -51,6 +53,21 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
5153
await this.liveSyncOperation(deviceDescriptors, liveSyncData, projectData);
5254
}
5355

56+
public async liveSyncToPreviewApp(data: IPreviewAppLiveSyncData): Promise<IQrCodeImageData> {
57+
await this.liveSync([], {
58+
syncToPreviewApp: true,
59+
projectDir: data.projectDir,
60+
bundle: data.appFilesUpdaterOptions.bundle,
61+
useHotModuleReload: data.appFilesUpdaterOptions.useHotModuleReload,
62+
release: false,
63+
env: data.env,
64+
});
65+
66+
const url = this.$previewSdkService.getQrCodeUrl({ useHotModuleReload: data.appFilesUpdaterOptions.useHotModuleReload });
67+
const result = await this.$previewQrCodeService.getLiveSyncQrCode(url);
68+
return result;
69+
}
70+
5471
public async stopLiveSync(projectDir: string, deviceIdentifiers?: string[], stopOptions?: { shouldAwaitAllActions: boolean }): Promise<void> {
5572
const liveSyncProcessInfo = this.liveSyncProcessesInfo[projectDir];
5673

lib/services/livesync/playground/preview-qr-code-service.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,26 @@ export class PreviewQrCodeService implements IPreviewQrCodeService {
1919
const result = Object.create(null);
2020

2121
if (!options || !options.platform || this.$mobileHelper.isAndroidPlatform(options.platform)) {
22-
result.android = await this.getQrCodeImageData(PlaygroundStoreUrls.GOOGLE_PLAY_URL);
22+
result.android = await this.getLiveSyncQrCode(PlaygroundStoreUrls.GOOGLE_PLAY_URL);
2323
}
2424

2525
if (!options || !options.platform || this.$mobileHelper.isiOSPlatform(options.platform)) {
26-
result.ios = await this.getQrCodeImageData(PlaygroundStoreUrls.APP_STORE_URL);
26+
result.ios = await this.getLiveSyncQrCode(PlaygroundStoreUrls.APP_STORE_URL);
2727
}
2828

2929
return result;
3030
}
3131

32+
public async getLiveSyncQrCode(url: string): Promise<IQrCodeImageData> {
33+
const shortenUrl = await this.getShortenUrl(url);
34+
const imageData = await this.$qr.generateDataUri(shortenUrl);
35+
return {
36+
originalUrl: url,
37+
shortenUrl,
38+
imageData
39+
};
40+
}
41+
3242
public async printLiveSyncQrCode(options: IPrintLiveSyncOptions): Promise<void> {
3343
const qrCodeUrl = this.$previewSdkService.getQrCodeUrl(options);
3444
const url = await this.getShortenUrl(qrCodeUrl);
@@ -63,15 +73,5 @@ To scan the QR code and deploy your app on a device, you need to have the \`Nati
6373

6474
return url;
6575
}
66-
67-
private async getQrCodeImageData(url: string): Promise<IQrCodeImageData> {
68-
const shortenUrl = await this.getShortenUrl(url);
69-
const imageData = await this.$qr.generateDataUri(shortenUrl);
70-
return {
71-
originalUrl: url,
72-
shortenUrl,
73-
imageData
74-
};
75-
}
7676
}
7777
$injector.register("previewQrCodeService", PreviewQrCodeService);

test/services/livesync-service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const createTestInjector = (): IInjector => {
3838
}
3939
});
4040
testInjector.register("previewAppLiveSyncService", {});
41+
testInjector.register("previewQrCodeService", {});
42+
testInjector.register("previewSdkService", {});
4143

4244
return testInjector;
4345
};
@@ -60,6 +62,8 @@ class LiveSyncServiceInheritor extends LiveSyncService {
6062
$usbLiveSyncService: DeprecatedUsbLiveSyncService,
6163
$injector: IInjector,
6264
$previewAppLiveSyncService: IPreviewAppLiveSyncService,
65+
$previewQrCodeService: IPreviewQrCodeService,
66+
$previewSdkService: IPreviewSdkService,
6367
$hmrStatusService: IHmrStatusService,
6468
$platformsData: IPlatformsData) {
6569

@@ -80,6 +84,8 @@ class LiveSyncServiceInheritor extends LiveSyncService {
8084
$analyticsService,
8185
$usbLiveSyncService,
8286
$previewAppLiveSyncService,
87+
$previewQrCodeService,
88+
$previewSdkService,
8389
$hmrStatusService,
8490
$injector
8591
);

0 commit comments

Comments
 (0)