From 34aead21578e495a2ac5a9c3dd9143af9767816d Mon Sep 17 00:00:00 2001 From: fatme Date: Fri, 2 Nov 2018 11:48:16 +0200 Subject: [PATCH 1/4] refactor(preview): rename playgroundQrCodeGenerator to previewQrCodeService --- lib/bootstrap.ts | 2 +- lib/commands/preview.ts | 4 ++-- lib/definitions/preview-app-livesync.d.ts | 2 +- .../{qr-code-generator.ts => preview-qr-code-service.ts} | 4 ++-- lib/services/platform-environment-requirements.ts | 4 ++-- test/services/platform-environment-requirements.ts | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) rename lib/services/livesync/playground/{qr-code-generator.ts => preview-qr-code-service.ts} (90%) diff --git a/lib/bootstrap.ts b/lib/bootstrap.ts index adbc81da63..c7eee58e08 100644 --- a/lib/bootstrap.ts +++ b/lib/bootstrap.ts @@ -135,7 +135,7 @@ $injector.require("previewAppLiveSyncService", "./services/livesync/playground/p $injector.require("previewAppPluginsService", "./services/livesync/playground/preview-app-plugins-service"); $injector.require("previewSdkService", "./services/livesync/playground/preview-sdk-service"); $injector.requirePublicClass("previewDevicesService", "./services/livesync/playground/devices/preview-devices-service"); -$injector.require("playgroundQrCodeGenerator", "./services/livesync/playground/qr-code-generator"); +$injector.require("previewQrCodeService", "./services/livesync/playground/preview-qr-code-service"); $injector.requirePublic("sysInfo", "./sys-info"); $injector.require("iOSNotificationService", "./services/ios-notification-service"); diff --git a/lib/commands/preview.ts b/lib/commands/preview.ts index 1926fcf8bf..a0dd1ac865 100644 --- a/lib/commands/preview.ts +++ b/lib/commands/preview.ts @@ -8,7 +8,7 @@ export class PreviewCommand implements ICommand { private $networkConnectivityValidator: INetworkConnectivityValidator, private $projectData: IProjectData, private $options: IOptions, - private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator) { } + private $previewQrCodeService: IPreviewQrCodeService) { } public async execute(): Promise { await this.$liveSyncService.liveSync([], { @@ -24,7 +24,7 @@ export class PreviewCommand implements ICommand { useHotModuleReload: this.$options.hmr }); - await this.$playgroundQrCodeGenerator.generateQrCode({ useHotModuleReload: this.$options.hmr, link: this.$options.link }); + await this.$previewQrCodeService.generateQrCode({ useHotModuleReload: this.$options.hmr, link: this.$options.link }); } public async canExecute(args: string[]): Promise { diff --git a/lib/definitions/preview-app-livesync.d.ts b/lib/definitions/preview-app-livesync.d.ts index 23bcbdb491..d8b75c1aa1 100644 --- a/lib/definitions/preview-app-livesync.d.ts +++ b/lib/definitions/preview-app-livesync.d.ts @@ -22,7 +22,7 @@ declare global { getExternalPlugins(device: Device): string[]; } - interface IPlaygroundQrCodeGenerator { + interface IPreviewQrCodeService { generateQrCode(options: IGenerateQrCodeOptions): Promise; } diff --git a/lib/services/livesync/playground/qr-code-generator.ts b/lib/services/livesync/playground/preview-qr-code-service.ts similarity index 90% rename from lib/services/livesync/playground/qr-code-generator.ts rename to lib/services/livesync/playground/preview-qr-code-service.ts index cea5810478..8a57058814 100644 --- a/lib/services/livesync/playground/qr-code-generator.ts +++ b/lib/services/livesync/playground/preview-qr-code-service.ts @@ -2,7 +2,7 @@ import * as util from "util"; import { EOL } from "os"; import { PlaygroundStoreUrls } from "./preview-app-constants"; -export class PlaygroundQrCodeGenerator implements IPlaygroundQrCodeGenerator { +export class PreviewQrCodeService implements IPreviewQrCodeService { constructor(private $previewSdkService: IPreviewSdkService, private $httpClient: Server.IHttpClient, private $qrCodeTerminalService: IQrCodeTerminalService, @@ -39,4 +39,4 @@ To scan the QR code and deploy your app on a device, you need to have the \`Nati } } } -$injector.register("playgroundQrCodeGenerator", PlaygroundQrCodeGenerator); +$injector.register("previewQrCodeService", PreviewQrCodeService); diff --git a/lib/services/platform-environment-requirements.ts b/lib/services/platform-environment-requirements.ts index 284a399006..130b8256b3 100644 --- a/lib/services/platform-environment-requirements.ts +++ b/lib/services/platform-environment-requirements.ts @@ -13,7 +13,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ private $staticConfig: IStaticConfig, private $analyticsService: IAnalyticsService, private $injector: IInjector, - private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator) { } + private $previewQrCodeService: IPreviewQrCodeService) { } @cache() private get $liveSyncService(): ILiveSyncService { @@ -194,7 +194,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ useHotModuleReload: options.hmr }); - await this.$playgroundQrCodeGenerator.generateQrCode({ useHotModuleReload: options.hmr, link: options.link }); + await this.$previewQrCodeService.generateQrCode({ useHotModuleReload: options.hmr, link: options.link }); } } diff --git a/test/services/platform-environment-requirements.ts b/test/services/platform-environment-requirements.ts index dce5ab3806..5dab5825ff 100644 --- a/test/services/platform-environment-requirements.ts +++ b/test/services/platform-environment-requirements.ts @@ -28,7 +28,7 @@ function createTestInjector() { testInjector.register("platformEnvironmentRequirements", PlatformEnvironmentRequirements); testInjector.register("staticConfig", { SYS_REQUIREMENTS_LINK: "" }); testInjector.register("nativeScriptCloudExtensionService", {}); - testInjector.register("playgroundQrCodeGenerator", {}); + testInjector.register("previewQrCodeService", {}); return testInjector; } From 636d6194d1561766caf6fb91b842f69a12899374 Mon Sep 17 00:00:00 2001 From: fatme Date: Fri, 2 Nov 2018 11:55:07 +0200 Subject: [PATCH 2/4] refactor(preview): rename method generateQrCode to printLiveSyncQrCode --- lib/commands/preview.ts | 2 +- lib/definitions/preview-app-livesync.d.ts | 2 +- lib/services/livesync/playground/preview-qr-code-service.ts | 2 +- lib/services/platform-environment-requirements.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/commands/preview.ts b/lib/commands/preview.ts index a0dd1ac865..bc42413b3f 100644 --- a/lib/commands/preview.ts +++ b/lib/commands/preview.ts @@ -24,7 +24,7 @@ export class PreviewCommand implements ICommand { useHotModuleReload: this.$options.hmr }); - await this.$previewQrCodeService.generateQrCode({ useHotModuleReload: this.$options.hmr, link: this.$options.link }); + await this.$previewQrCodeService.printLiveSyncQrCode({ useHotModuleReload: this.$options.hmr, link: this.$options.link }); } public async canExecute(args: string[]): Promise { diff --git a/lib/definitions/preview-app-livesync.d.ts b/lib/definitions/preview-app-livesync.d.ts index d8b75c1aa1..98a950eaa2 100644 --- a/lib/definitions/preview-app-livesync.d.ts +++ b/lib/definitions/preview-app-livesync.d.ts @@ -23,7 +23,7 @@ declare global { } interface IPreviewQrCodeService { - generateQrCode(options: IGenerateQrCodeOptions): Promise; + printLiveSyncQrCode(options: IGenerateQrCodeOptions): Promise; } interface IGenerateQrCodeOptions extends IHasUseHotModuleReloadOption { diff --git a/lib/services/livesync/playground/preview-qr-code-service.ts b/lib/services/livesync/playground/preview-qr-code-service.ts index 8a57058814..a9cda830bd 100644 --- a/lib/services/livesync/playground/preview-qr-code-service.ts +++ b/lib/services/livesync/playground/preview-qr-code-service.ts @@ -10,7 +10,7 @@ export class PreviewQrCodeService implements IPreviewQrCodeService { private $logger: ILogger) { } - public async generateQrCode(options: IGenerateQrCodeOptions): Promise { + public async printLiveSyncQrCode(options: IGenerateQrCodeOptions): Promise { let url = this.$previewSdkService.getQrCodeUrl(options); const shortenUrlEndpoint = util.format(this.$config.SHORTEN_URL_ENDPOINT, encodeURIComponent(url)); try { diff --git a/lib/services/platform-environment-requirements.ts b/lib/services/platform-environment-requirements.ts index 130b8256b3..4cbd86c4ad 100644 --- a/lib/services/platform-environment-requirements.ts +++ b/lib/services/platform-environment-requirements.ts @@ -194,7 +194,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ useHotModuleReload: options.hmr }); - await this.$previewQrCodeService.generateQrCode({ useHotModuleReload: options.hmr, link: options.link }); + await this.$previewQrCodeService.printLiveSyncQrCode({ useHotModuleReload: options.hmr, link: options.link }); } } From ced06d19e4f2196f1d20cebccaccd633874b4734 Mon Sep 17 00:00:00 2001 From: fatme Date: Fri, 2 Nov 2018 12:48:33 +0200 Subject: [PATCH 3/4] feat(preview-api): expose public method for getting qr code of playground app --- lib/bootstrap.ts | 2 +- lib/common/declarations.d.ts | 15 +++++ lib/definitions/preview-app-livesync.d.ts | 5 ++ .../playground/preview-qr-code-service.ts | 59 +++++++++++++++---- 4 files changed, 68 insertions(+), 13 deletions(-) diff --git a/lib/bootstrap.ts b/lib/bootstrap.ts index c7eee58e08..b6283ba8f9 100644 --- a/lib/bootstrap.ts +++ b/lib/bootstrap.ts @@ -135,7 +135,7 @@ $injector.require("previewAppLiveSyncService", "./services/livesync/playground/p $injector.require("previewAppPluginsService", "./services/livesync/playground/preview-app-plugins-service"); $injector.require("previewSdkService", "./services/livesync/playground/preview-sdk-service"); $injector.requirePublicClass("previewDevicesService", "./services/livesync/playground/devices/preview-devices-service"); -$injector.require("previewQrCodeService", "./services/livesync/playground/preview-qr-code-service"); +$injector.requirePublic("previewQrCodeService", "./services/livesync/playground/preview-qr-code-service"); $injector.requirePublic("sysInfo", "./sys-info"); $injector.require("iOSNotificationService", "./services/ios-notification-service"); diff --git a/lib/common/declarations.d.ts b/lib/common/declarations.d.ts index 8aedd1092c..06bd4e3061 100644 --- a/lib/common/declarations.d.ts +++ b/lib/common/declarations.d.ts @@ -969,6 +969,21 @@ interface IQrCodeGenerator { generateDataUri(data: string): Promise; } +interface IQrCodeImageData { + /** + * The original URL used for generating QR code image. + */ + originalUrl: string; + /** + * The shorten URL used for generating QR code image. + */ + shortenUrl: string; + /** + * Base64 encoded data used for generating QR code image. + */ + imageData: string; +} + interface IDynamicHelpProvider { /** * Checks if current project's framework is one of the specified as arguments. diff --git a/lib/definitions/preview-app-livesync.d.ts b/lib/definitions/preview-app-livesync.d.ts index 98a950eaa2..18d61ac080 100644 --- a/lib/definitions/preview-app-livesync.d.ts +++ b/lib/definitions/preview-app-livesync.d.ts @@ -23,9 +23,14 @@ declare global { } interface IPreviewQrCodeService { + getPlaygroundAppQrCode(options?: IPlaygroundAppQrCodeOptions): Promise>; printLiveSyncQrCode(options: IGenerateQrCodeOptions): Promise; } + interface IPlaygroundAppQrCodeOptions { + platform?: string; + } + interface IGenerateQrCodeOptions extends IHasUseHotModuleReloadOption { /** * If set to true, a link will be shown on console instead of QR code diff --git a/lib/services/livesync/playground/preview-qr-code-service.ts b/lib/services/livesync/playground/preview-qr-code-service.ts index a9cda830bd..b9bd64e777 100644 --- a/lib/services/livesync/playground/preview-qr-code-service.ts +++ b/lib/services/livesync/playground/preview-qr-code-service.ts @@ -1,25 +1,37 @@ import * as util from "util"; import { EOL } from "os"; import { PlaygroundStoreUrls } from "./preview-app-constants"; +import { exported } from "../../../common/decorators"; export class PreviewQrCodeService implements IPreviewQrCodeService { - constructor(private $previewSdkService: IPreviewSdkService, + constructor( + private $config: IConfiguration, private $httpClient: Server.IHttpClient, + private $logger: ILogger, + private $mobileHelper: Mobile.IMobileHelper, + private $previewSdkService: IPreviewSdkService, private $qrCodeTerminalService: IQrCodeTerminalService, - private $config: IConfiguration, - private $logger: ILogger) { + private $qr: IQrCodeGenerator + ) { } + + @exported("previewQrCodeService") + public async getPlaygroundAppQrCode(options?: IPlaygroundAppQrCodeOptions): Promise> { + const result = Object.create(null); + + if (!options || !options.platform || this.$mobileHelper.isAndroidPlatform(options.platform)) { + result.android = await this.getQrCodeImageData(PlaygroundStoreUrls.GOOGLE_PLAY_URL); + } + + if (!options || !options.platform || this.$mobileHelper.isiOSPlatform(options.platform)) { + result.ios = await this.getQrCodeImageData(PlaygroundStoreUrls.APP_STORE_URL); + } + + return result; } public async printLiveSyncQrCode(options: IGenerateQrCodeOptions): Promise { - let url = this.$previewSdkService.getQrCodeUrl(options); - const shortenUrlEndpoint = util.format(this.$config.SHORTEN_URL_ENDPOINT, encodeURIComponent(url)); - try { - const response = await this.$httpClient.httpRequest(shortenUrlEndpoint); - const responseBody = JSON.parse(response.body); - url = responseBody.shortURL || url; - } catch (e) { - // use the longUrl - } + const qrCodeUrl = this.$previewSdkService.getQrCodeUrl(options); + const url = await this.getShortenUrl(qrCodeUrl); this.$logger.info(); const message = `${EOL} Generating qrcode for url ${url}.`; @@ -38,5 +50,28 @@ To scan the QR code and deploy your app on a device, you need to have the \`Nati Google Play (Android): ${PlaygroundStoreUrls.GOOGLE_PLAY_URL}`); } } + + private async getShortenUrl(url: string): Promise { + const shortenUrlEndpoint = util.format(this.$config.SHORTEN_URL_ENDPOINT, encodeURIComponent(url)); + try { + const response = await this.$httpClient.httpRequest(shortenUrlEndpoint); + const responseBody = JSON.parse(response.body); + url = responseBody.shortURL || url; + } catch (e) { + // use the longUrl + } + + 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 2c14c9692e4d58076bc03e1b88196bf94b0ed6d4 Mon Sep 17 00:00:00 2001 From: fatme Date: Fri, 2 Nov 2018 12:51:44 +0200 Subject: [PATCH 4/4] refactor(preview): rename IGenerateQrCodeOptions to IPrintLiveSyncOptions --- lib/definitions/preview-app-livesync.d.ts | 4 ++-- lib/services/livesync/playground/preview-qr-code-service.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/definitions/preview-app-livesync.d.ts b/lib/definitions/preview-app-livesync.d.ts index 18d61ac080..bbee82aa56 100644 --- a/lib/definitions/preview-app-livesync.d.ts +++ b/lib/definitions/preview-app-livesync.d.ts @@ -24,14 +24,14 @@ declare global { interface IPreviewQrCodeService { getPlaygroundAppQrCode(options?: IPlaygroundAppQrCodeOptions): Promise>; - printLiveSyncQrCode(options: IGenerateQrCodeOptions): Promise; + printLiveSyncQrCode(options: IPrintLiveSyncOptions): Promise; } interface IPlaygroundAppQrCodeOptions { platform?: string; } - interface IGenerateQrCodeOptions extends IHasUseHotModuleReloadOption { + interface IPrintLiveSyncOptions extends IHasUseHotModuleReloadOption { /** * If set to true, a link will be shown on console instead of QR code * Default value is false. diff --git a/lib/services/livesync/playground/preview-qr-code-service.ts b/lib/services/livesync/playground/preview-qr-code-service.ts index b9bd64e777..3421fe4903 100644 --- a/lib/services/livesync/playground/preview-qr-code-service.ts +++ b/lib/services/livesync/playground/preview-qr-code-service.ts @@ -29,7 +29,7 @@ export class PreviewQrCodeService implements IPreviewQrCodeService { return result; } - public async printLiveSyncQrCode(options: IGenerateQrCodeOptions): Promise { + public async printLiveSyncQrCode(options: IPrintLiveSyncOptions): Promise { const qrCodeUrl = this.$previewSdkService.getQrCodeUrl(options); const url = await this.getShortenUrl(qrCodeUrl);