Skip to content

feat(preview-api): expose api for getting logs from preview app #4131

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 16, 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
1 change: 1 addition & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ $injector.require("androidLiveSyncService", "./services/livesync/android-livesyn
$injector.require("iOSLiveSyncService", "./services/livesync/ios-livesync-service");
$injector.require("usbLiveSyncService", "./services/livesync/livesync-service"); // The name is used in https://github.com/NativeScript/nativescript-dev-typescript
$injector.require("previewAppLiveSyncService", "./services/livesync/playground/preview-app-livesync-service");
$injector.require("previewAppLogProvider", "./services/livesync/playground/preview-app-log-provider");
$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");
Expand Down
8 changes: 8 additions & 0 deletions lib/commands/preview.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { DEVICE_LOG_EVENT_NAME } from "../common/constants";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it better to add the DEVICE_LOG_EVENT_NAME string in the previewAppLogProvider itself instead of using the constants. In this way it will be also available in the public API and we will not require a hardcoded string (deviceLogData). Also the data suffix in the event name is a little bit strange. It could be just "deviceLog"


export class PreviewCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];
private static MIN_SUPPORTED_WEBPACK_VERSION = "0.17.0";

constructor(private $bundleValidatorHelper: IBundleValidatorHelper,
private $errors: IErrors,
private $liveSyncService: ILiveSyncService,
private $logger: ILogger,
private $networkConnectivityValidator: INetworkConnectivityValidator,
private $projectData: IProjectData,
private $options: IOptions,
private $previewAppLogProvider: IPreviewAppLogProvider,
private $previewQrCodeService: IPreviewQrCodeService) { }

public async execute(): Promise<void> {
this.$previewAppLogProvider.on(DEVICE_LOG_EVENT_NAME, (deviceId: string, message: string) => {
this.$logger.info(message);
});

await this.$liveSyncService.liveSync([], {
syncToPreviewApp: true,
projectDir: this.$projectData.projectDir,
Expand Down
4 changes: 4 additions & 0 deletions lib/definitions/preview-app-livesync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ declare global {
getExternalPlugins(device: Device): string[];
}

interface IPreviewAppLogProvider extends EventEmitter {
logData(log: string, deviceName: string, deviceId: string): void;
}

interface IPreviewQrCodeService {
getPlaygroundAppQrCode(options?: IPlaygroundAppQrCodeOptions): Promise<IDictionary<IQrCodeImageData>>;
getLiveSyncQrCode(url: string): Promise<IQrCodeImageData>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Device } from "nativescript-preview-sdk";
import { EventEmitter } from "events";
import { DeviceDiscoveryEventNames } from "../../../../common/constants";
import { DeviceDiscoveryEventNames, DEVICE_LOG_EVENT_NAME } from "../../../../common/constants";

export class PreviewDevicesService extends EventEmitter implements IPreviewDevicesService {
private connectedDevices: Device[] = [];

constructor(private $previewAppLogProvider: IPreviewAppLogProvider) {
super();

this.initialize();
}

public getConnectedDevices(): Device[] {
return this.connectedDevices;
}
Expand All @@ -27,6 +33,12 @@ export class PreviewDevicesService extends EventEmitter implements IPreviewDevic
return _.filter(this.connectedDevices, { platform: platform.toLowerCase() });
}

private initialize(): void {
this.$previewAppLogProvider.on(DEVICE_LOG_EVENT_NAME, (deviceId: string, message: string) => {
this.emit(DEVICE_LOG_EVENT_NAME, deviceId, message);
});
}

private raiseDeviceFound(device: Device) {
this.emit(DeviceDiscoveryEventNames.DEVICE_FOUND, device);
this.connectedDevices.push(device);
Expand Down
10 changes: 10 additions & 0 deletions lib/services/livesync/playground/preview-app-log-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { EventEmitter } from "events";
import { DEVICE_LOG_EVENT_NAME } from "../../../common/constants";

export class PreviewAppLogProvider extends EventEmitter implements IPreviewAppLogProvider {
public logData(log: string, deviceName: string, deviceId: string): void {
const message = `LOG from device ${deviceName}: ${log}`;
this.emit(DEVICE_LOG_EVENT_NAME, deviceId, message);
}
}
$injector.register("previewAppLogProvider", PreviewAppLogProvider);
8 changes: 3 additions & 5 deletions lib/services/livesync/playground/preview-sdk-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { MessagingService, Config, Device, DeviceConnectedMessage, SdkCallbacks, ConnectedDevices, FilesPayload } from "nativescript-preview-sdk";
import { PubnubKeys } from "./preview-app-constants";
import { EventEmitter } from "events";
import { DEVICE_LOG_EVENT_NAME } from "../../../common/constants";
const pako = require("pako");

export class PreviewSdkService extends EventEmitter implements IPreviewSdkService {
Expand All @@ -12,7 +11,8 @@ export class PreviewSdkService extends EventEmitter implements IPreviewSdkServic
constructor(private $config: IConfiguration,
private $httpClient: Server.IHttpClient,
private $logger: ILogger,
private $previewDevicesService: IPreviewDevicesService) {
private $previewDevicesService: IPreviewDevicesService,
private $previewAppLogProvider: IPreviewAppLogProvider) {
super();
}

Expand Down Expand Up @@ -61,9 +61,7 @@ export class PreviewSdkService extends EventEmitter implements IPreviewSdkServic
this.$logger.trace("Received onLogSdkMessage message: ", log);
},
onLogMessage: (log: string, deviceName: string, deviceId: string) => {
const device = this.$previewDevicesService.getDeviceById(deviceId);
this.emit(DEVICE_LOG_EVENT_NAME, log, deviceId, device ? device.platform : "");
this.$logger.info(`LOG from device ${deviceName}: ${log}`);
this.$previewAppLogProvider.logData(log, deviceName, deviceId);
},
onRestartMessage: () => {
this.$logger.trace("Received onRestartMessage event.");
Expand Down
3 changes: 3 additions & 0 deletions test/services/preview-devices-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ let lostDevices: Device[] = [];
function createTestInjector(): IInjector {
const injector = new Yok();
injector.register("previewDevicesService", PreviewDevicesService);
injector.register("previewAppLogProvider", {
on: () => ({})
});
injector.register("logger", LoggerStub);
return injector;
}
Expand Down
1 change: 1 addition & 0 deletions test/services/preview-sdk-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const getPreviewSdkService = (): IPreviewSdkService => {
testInjector.register("config", {});
testInjector.register("previewSdkService", PreviewSdkService);
testInjector.register("previewDevicesService", {});
testInjector.register("previewAppLogProvider", {});
testInjector.register("httpClient", {
httpRequest: async (options: any, proxySettings?: IProxySettings): Promise<Server.IResponse> => undefined
});
Expand Down