Skip to content

feat(preview-api): expose public api for starting the livesync operation to preview app #4093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/definitions/livesync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions lib/definitions/preview-app-livesync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ declare global {

interface IPreviewQrCodeService {
getPlaygroundAppQrCode(options?: IPlaygroundAppQrCodeOptions): Promise<IDictionary<IQrCodeImageData>>;
getLiveSyncQrCode(url: string): Promise<IQrCodeImageData>;
printLiveSyncQrCode(options: IPrintLiveSyncOptions): Promise<void>;
}

Expand Down
17 changes: 17 additions & 0 deletions lib/services/livesync/livesync-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -51,6 +53,21 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
await this.liveSyncOperation(deviceDescriptors, liveSyncData, projectData);
}

public async liveSyncToPreviewApp(data: IPreviewAppLiveSyncData): Promise<IQrCodeImageData> {
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<void> {
const liveSyncProcessInfo = this.liveSyncProcessesInfo[projectDir];

Expand Down
24 changes: 12 additions & 12 deletions lib/services/livesync/playground/preview-qr-code-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IQrCodeImageData> {
const shortenUrl = await this.getShortenUrl(url);
const imageData = await this.$qr.generateDataUri(shortenUrl);
return {
originalUrl: url,
shortenUrl,
imageData
};
}

public async printLiveSyncQrCode(options: IPrintLiveSyncOptions): Promise<void> {
const qrCodeUrl = this.$previewSdkService.getQrCodeUrl(options);
const url = await this.getShortenUrl(qrCodeUrl);
Expand Down Expand Up @@ -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<IQrCodeImageData> {
const shortenUrl = await this.getShortenUrl(url);
const imageData = await this.$qr.generateDataUri(shortenUrl);
return {
originalUrl: url,
shortenUrl,
imageData
};
}
}
$injector.register("previewQrCodeService", PreviewQrCodeService);
6 changes: 6 additions & 0 deletions test/services/livesync-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const createTestInjector = (): IInjector => {
}
});
testInjector.register("previewAppLiveSyncService", {});
testInjector.register("previewQrCodeService", {});
testInjector.register("previewSdkService", {});

return testInjector;
};
Expand All @@ -60,6 +62,8 @@ class LiveSyncServiceInheritor extends LiveSyncService {
$usbLiveSyncService: DeprecatedUsbLiveSyncService,
$injector: IInjector,
$previewAppLiveSyncService: IPreviewAppLiveSyncService,
$previewQrCodeService: IPreviewQrCodeService,
$previewSdkService: IPreviewSdkService,
$hmrStatusService: IHmrStatusService,
$platformsData: IPlatformsData) {

Expand All @@ -80,6 +84,8 @@ class LiveSyncServiceInheritor extends LiveSyncService {
$analyticsService,
$usbLiveSyncService,
$previewAppLiveSyncService,
$previewQrCodeService,
$previewSdkService,
$hmrStatusService,
$injector
);
Expand Down