From 61c08ef05b241f76a945420fa9c0af73699c40f3 Mon Sep 17 00:00:00 2001 From: fatme Date: Sat, 3 Nov 2018 14:27:26 +0200 Subject: [PATCH 1/2] feat(preview-api): expose public api for starting the livesync operation to preview app --- lib/definitions/livesync.d.ts | 2 +- lib/definitions/preview-app-livesync.d.ts | 1 + lib/services/livesync/livesync-service.ts | 17 +++++++++++++ .../playground/preview-qr-code-service.ts | 24 +++++++++---------- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/definitions/livesync.d.ts b/lib/definitions/livesync.d.ts index 5904ec2430..36d153d452 100644 --- a/lib/definitions/livesync.d.ts +++ b/lib/definitions/livesync.d.ts @@ -166,7 +166,7 @@ interface ILiveSyncInfo extends IProjectDir, IEnvOptions, IBundle, IRelease, IOp * Defines the timeout in seconds {N} CLI will wait to find the inspector socket port from device's logs. * If not provided, defaults to 10seconds. */ - timeout: string; + timeout?: string; } interface IHasSyncToPreviewAppOption { diff --git a/lib/definitions/preview-app-livesync.d.ts b/lib/definitions/preview-app-livesync.d.ts index bbee82aa56..01faa8bda3 100644 --- a/lib/definitions/preview-app-livesync.d.ts +++ b/lib/definitions/preview-app-livesync.d.ts @@ -24,6 +24,7 @@ declare global { interface IPreviewQrCodeService { getPlaygroundAppQrCode(options?: IPlaygroundAppQrCodeOptions): Promise>; + getLiveSyncQrCode(url: string): Promise; printLiveSyncQrCode(options: IPrintLiveSyncOptions): Promise; } diff --git a/lib/services/livesync/livesync-service.ts b/lib/services/livesync/livesync-service.ts index 3a6cf0b68a..6741cca82a 100644 --- a/lib/services/livesync/livesync-service.ts +++ b/lib/services/livesync/livesync-service.ts @@ -39,6 +39,8 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi private $analyticsService: IAnalyticsService, private $usbLiveSyncService: DeprecatedUsbLiveSyncService, private $previewAppLiveSyncService: IPreviewAppLiveSyncService, + private $previewQrCodeService: IPreviewQrCodeService, + private $previewSdkService: IPreviewSdkService, private $hmrStatusService: IHmrStatusService, private $injector: IInjector) { super(); @@ -51,6 +53,21 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi await this.liveSyncOperation(deviceDescriptors, liveSyncData, projectData); } + public async liveSyncToPreviewApp(data: IPreviewAppLiveSyncData): Promise { + await this.liveSync([], { + syncToPreviewApp: true, + projectDir: data.projectDir, + bundle: data.appFilesUpdaterOptions.bundle, + useHotModuleReload: data.appFilesUpdaterOptions.useHotModuleReload, + release: false, + env: data.env, + }); + + const url = this.$previewSdkService.getQrCodeUrl({ useHotModuleReload: data.appFilesUpdaterOptions.useHotModuleReload }); + const result = await this.$previewQrCodeService.getLiveSyncQrCode(url); + return result; + } + public async stopLiveSync(projectDir: string, deviceIdentifiers?: string[], stopOptions?: { shouldAwaitAllActions: boolean }): Promise { const liveSyncProcessInfo = this.liveSyncProcessesInfo[projectDir]; diff --git a/lib/services/livesync/playground/preview-qr-code-service.ts b/lib/services/livesync/playground/preview-qr-code-service.ts index 3421fe4903..5d5048b8a3 100644 --- a/lib/services/livesync/playground/preview-qr-code-service.ts +++ b/lib/services/livesync/playground/preview-qr-code-service.ts @@ -19,16 +19,26 @@ export class PreviewQrCodeService implements IPreviewQrCodeService { const result = Object.create(null); if (!options || !options.platform || this.$mobileHelper.isAndroidPlatform(options.platform)) { - result.android = await this.getQrCodeImageData(PlaygroundStoreUrls.GOOGLE_PLAY_URL); + result.android = await this.getLiveSyncQrCode(PlaygroundStoreUrls.GOOGLE_PLAY_URL); } if (!options || !options.platform || this.$mobileHelper.isiOSPlatform(options.platform)) { - result.ios = await this.getQrCodeImageData(PlaygroundStoreUrls.APP_STORE_URL); + result.ios = await this.getLiveSyncQrCode(PlaygroundStoreUrls.APP_STORE_URL); } return result; } + public async getLiveSyncQrCode(url: string): Promise { + const shortenUrl = await this.getShortenUrl(url); + const imageData = await this.$qr.generateDataUri(shortenUrl); + return { + originalUrl: url, + shortenUrl, + imageData + }; + } + public async printLiveSyncQrCode(options: IPrintLiveSyncOptions): Promise { const qrCodeUrl = this.$previewSdkService.getQrCodeUrl(options); 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 return url; } - - private async getQrCodeImageData(url: string): Promise { - const shortenUrl = await this.getShortenUrl(url); - const imageData = await this.$qr.generateDataUri(shortenUrl); - return { - originalUrl: url, - shortenUrl, - imageData - }; - } } $injector.register("previewQrCodeService", PreviewQrCodeService); From e05af32720675ef29bb3f608ce15a418667be99b Mon Sep 17 00:00:00 2001 From: fatme Date: Sun, 4 Nov 2018 09:51:29 +0200 Subject: [PATCH 2/2] chore: fix unit tests --- test/services/livesync-service.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/services/livesync-service.ts b/test/services/livesync-service.ts index 93d26f351d..65e4284b86 100644 --- a/test/services/livesync-service.ts +++ b/test/services/livesync-service.ts @@ -38,6 +38,8 @@ const createTestInjector = (): IInjector => { } }); testInjector.register("previewAppLiveSyncService", {}); + testInjector.register("previewQrCodeService", {}); + testInjector.register("previewSdkService", {}); return testInjector; }; @@ -60,6 +62,8 @@ class LiveSyncServiceInheritor extends LiveSyncService { $usbLiveSyncService: DeprecatedUsbLiveSyncService, $injector: IInjector, $previewAppLiveSyncService: IPreviewAppLiveSyncService, + $previewQrCodeService: IPreviewQrCodeService, + $previewSdkService: IPreviewSdkService, $hmrStatusService: IHmrStatusService, $platformsData: IPlatformsData) { @@ -80,6 +84,8 @@ class LiveSyncServiceInheritor extends LiveSyncService { $analyticsService, $usbLiveSyncService, $previewAppLiveSyncService, + $previewQrCodeService, + $previewSdkService, $hmrStatusService, $injector );