Skip to content
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: 0 additions & 1 deletion lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ $injector.requireCommand("platform|clean", "./commands/platform-clean");
$injector.require("bundleValidatorHelper", "./helpers/bundle-validator-helper");
$injector.require("liveSyncCommandHelper", "./helpers/livesync-command-helper");
$injector.require("deployCommandHelper", "./helpers/deploy-command-helper");
$injector.require("previewCommandHelper", "./helpers/preview-command-helper");

$injector.requirePublicClass("localBuildService", "./services/local-build-service");
$injector.requirePublicClass("liveSyncService", "./services/livesync/livesync-service");
Expand Down
9 changes: 3 additions & 6 deletions lib/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ export class PreviewCommand implements ICommand {
private $networkConnectivityValidator: INetworkConnectivityValidator,
private $projectData: IProjectData,
private $options: IOptions,
private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator,
private $previewCommandHelper: IPreviewCommandHelper) { }

public async execute(args: string[]): Promise<void> {
this.$previewCommandHelper.run();
private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator) { }

public async execute(): Promise<void> {
await this.$liveSyncService.liveSync([], {
syncToPreviewApp: true,
projectDir: this.$projectData.projectDir,
Expand All @@ -26,7 +23,7 @@ export class PreviewCommand implements ICommand {
useHotModuleReload: this.$options.hmr
});

await this.$playgroundQrCodeGenerator.generateQrCodeForCurrentApp({ useHotModuleReload: this.$options.hmr });
await this.$playgroundQrCodeGenerator.generateQrCode({ useHotModuleReload: this.$options.hmr, link: this.$options.link });
}

public async canExecute(args: string[]): Promise<boolean> {
Expand Down
2 changes: 1 addition & 1 deletion lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ interface IOptions extends IRelease, IDeviceIdentifier, IJustLaunch, IAvd, IAvai
inspector: boolean; // the counterpart to --chrome
background: string;
hmr: boolean;

link: boolean;
}

interface IEnvOptions {
Expand Down
14 changes: 8 additions & 6 deletions lib/definitions/preview-app-livesync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ declare global {
getExternalPlugins(device: Device): string[];
}

interface IPreviewCommandHelper {
run(): void;
interface IPlaygroundQrCodeGenerator {
generateQrCode(options: IGenerateQrCodeOptions): Promise<void>;
}

interface IPlaygroundQrCodeGenerator {
generateQrCodeForiOS(): Promise<void>;
generateQrCodeForAndroid(): Promise<void>;
generateQrCodeForCurrentApp(options: IHasUseHotModuleReloadOption): Promise<void>;
interface IGenerateQrCodeOptions extends IHasUseHotModuleReloadOption {
/**
* If set to true, a link will be shown on console instead of QR code
* Default value is false.
*/
link: boolean;
}
}
2 changes: 1 addition & 1 deletion lib/definitions/qr-code.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
interface IQrCodeTerminalService {
generate(url: string, message: string): void;
generate(url: string): void;
}
64 changes: 0 additions & 64 deletions lib/helpers/preview-command-helper.ts

This file was deleted.

3 changes: 2 additions & 1 deletion lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export class Options {
var: { type: OptionType.Object },
default: { type: OptionType.Boolean },
count: { type: OptionType.Number },
hooks: { type: OptionType.Boolean, default: true }
hooks: { type: OptionType.Boolean, default: true },
link: { type: OptionType.Boolean, default: false }
};
}

Expand Down
57 changes: 20 additions & 37 deletions lib/services/livesync/playground/qr-code-generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PlaygroundStoreUrls } from "./preview-app-constants";
import * as util from "util";
const chalk = require("chalk");
import { EOL } from "os";
import { PlaygroundStoreUrls } from "./preview-app-constants";

export class PlaygroundQrCodeGenerator implements IPlaygroundQrCodeGenerator {
constructor(private $previewSdkService: IPreviewSdkService,
Expand All @@ -10,27 +10,8 @@ export class PlaygroundQrCodeGenerator implements IPlaygroundQrCodeGenerator {
private $logger: ILogger) {
}

public async generateQrCodeForiOS(): Promise<void> {
const message = `Scan the QR code below to install ${"NativeScript Playground app".underline.bold} on your ${"iOS".underline.bold} device or get it from ${PlaygroundStoreUrls.APP_STORE_URL.underline.bold}.`;
await this.generateQrCode(PlaygroundStoreUrls.APP_STORE_URL, message);
}

public async generateQrCodeForAndroid(): Promise<void> {
const message = `Scan the QR code below to install ${"NativeScript Playground app".underline.bold} on your ${"Android".underline.bold} device or get it from ${PlaygroundStoreUrls.GOOGLE_PLAY_URL.underline.bold}.`;
await this.generateQrCode(PlaygroundStoreUrls.GOOGLE_PLAY_URL, message);
}

public async generateQrCodeForCurrentApp(options: IHasUseHotModuleReloadOption): Promise<void> {
const message = `Use ${"NativeScript Playground app".underline.bold} and scan the QR code below to preview the application on your device.`;
await this.generateQrCode(this.$previewSdkService.getQrCodeUrl(options), message);
}

private async generateQrCode(url: string, message: string): Promise<void> {
await this.generateQrCodeCore(url, message);
this.printUsage();
}

private async generateQrCodeCore(url: string, message: string): Promise<void> {
public async generateQrCode(options: IGenerateQrCodeOptions): Promise<void> {
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);
Expand All @@ -40,20 +21,22 @@ export class PlaygroundQrCodeGenerator implements IPlaygroundQrCodeGenerator {
// use the longUrl
}

this.$qrCodeTerminalService.generate(url, message);
}

private printUsage(): void {
this.$logger.info(`
-> Press ${this.underlineBoldCyan("a")} to get a link to NativeScript Playground app for ${this.underlineBoldCyan("Android")} on Google Play
-> Press ${this.underlineBoldCyan("i")} to get a link to NativeScript Playground app for ${this.underlineBoldCyan("iOS")} on the App Store
-> Press ${this.underlineBoldCyan("c")} to show the QR code of ${this.underlineBoldCyan("your application")}
`);
}

private underlineBoldCyan(str: string) {
const { bold, underline, cyan } = chalk;
return underline(bold(cyan(str)));
this.$logger.info();
const message = `${EOL} Generating qrcode for url ${url}.`;
this.$logger.trace(message);

if (options.link) {
this.$logger.printMarkdown(message);
} else {
this.$qrCodeTerminalService.generate(url);

this.$logger.info();
this.$logger.printMarkdown(`# Use \`NativeScript Playground app\` and scan the \`QR code\` below to preview the application on your device.`);
this.$logger.printMarkdown(`
To scan the QR code and deploy your app on a device, you need to have the \`NativeScript Playground app\`:
App Store (iOS): ${PlaygroundStoreUrls.APP_STORE_URL}
Google Play (Android): ${PlaygroundStoreUrls.GOOGLE_PLAY_URL}`);
}
}
}
$injector.register("playgroundQrCodeGenerator", PlaygroundQrCodeGenerator);
8 changes: 1 addition & 7 deletions lib/services/platform-environment-requirements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
private $injector: IInjector,
private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator) { }

@cache()
private get $previewCommandHelper(): IPreviewCommandHelper {
return this.$injector.resolve("previewCommandHelper");
}

@cache()
private get $liveSyncService(): ILiveSyncService {
return this.$injector.resolve("liveSyncService");
Expand Down Expand Up @@ -186,7 +181,6 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
this.$errors.failWithoutHelp(`No project found. In order to sync to playground you need to go to project directory or specify --path option.`);
}

this.$previewCommandHelper.run();
await this.$liveSyncService.liveSync([], {
syncToPreviewApp: true,
projectDir,
Expand All @@ -200,7 +194,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
useHotModuleReload: options.hmr
});

await this.$playgroundQrCodeGenerator.generateQrCodeForCurrentApp({ useHotModuleReload: options.hmr });
await this.$playgroundQrCodeGenerator.generateQrCode({ useHotModuleReload: options.hmr, link: options.link });
}
}

Expand Down
7 changes: 1 addition & 6 deletions lib/services/qr-code-terminal-service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { EOL } from "os";

const qrcode = require("qrcode-terminal");

export class QrCodeTerminalService implements IQrCodeTerminalService {
constructor(private $logger: ILogger) { }

public generate(url: string, message: string): void {
this.$logger.info(`${EOL} ${message.blue}`);
this.$logger.info(`${EOL} Generating qrcode for url ${url}.`);

public generate(url: string): void {
try {
qrcode.generate(url);
} catch (err) {
Expand Down
Loading