Skip to content

Commit 466b450

Browse files
author
Fatme
authored
Merge pull request #3990 from NativeScript/fatme/ipp
fix: improve initial page of `tns preview` command
2 parents 21568cc + aa44a3d commit 466b450

File tree

11 files changed

+37
-281
lines changed

11 files changed

+37
-281
lines changed

lib/bootstrap.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ $injector.requireCommand("platform|clean", "./commands/platform-clean");
123123
$injector.require("bundleValidatorHelper", "./helpers/bundle-validator-helper");
124124
$injector.require("liveSyncCommandHelper", "./helpers/livesync-command-helper");
125125
$injector.require("deployCommandHelper", "./helpers/deploy-command-helper");
126-
$injector.require("previewCommandHelper", "./helpers/preview-command-helper");
127126

128127
$injector.requirePublicClass("localBuildService", "./services/local-build-service");
129128
$injector.requirePublicClass("liveSyncService", "./services/livesync/livesync-service");

lib/commands/preview.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ export class PreviewCommand implements ICommand {
77
private $networkConnectivityValidator: INetworkConnectivityValidator,
88
private $projectData: IProjectData,
99
private $options: IOptions,
10-
private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator,
11-
private $previewCommandHelper: IPreviewCommandHelper) { }
12-
13-
public async execute(args: string[]): Promise<void> {
14-
this.$previewCommandHelper.run();
10+
private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator) { }
1511

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

29-
await this.$playgroundQrCodeGenerator.generateQrCodeForCurrentApp({ useHotModuleReload: this.$options.hmr });
26+
await this.$playgroundQrCodeGenerator.generateQrCode({ useHotModuleReload: this.$options.hmr, link: this.$options.link });
3027
}
3128

3229
public async canExecute(args: string[]): Promise<boolean> {

lib/declarations.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ interface IOptions extends IRelease, IDeviceIdentifier, IJustLaunch, IAvd, IAvai
523523
inspector: boolean; // the counterpart to --chrome
524524
background: string;
525525
hmr: boolean;
526-
526+
link: boolean;
527527
}
528528

529529
interface IEnvOptions {

lib/definitions/preview-app-livesync.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ declare global {
2323
getExternalPlugins(device: Device): string[];
2424
}
2525

26-
interface IPreviewCommandHelper {
27-
run(): void;
26+
interface IPlaygroundQrCodeGenerator {
27+
generateQrCode(options: IGenerateQrCodeOptions): Promise<void>;
2828
}
2929

30-
interface IPlaygroundQrCodeGenerator {
31-
generateQrCodeForiOS(): Promise<void>;
32-
generateQrCodeForAndroid(): Promise<void>;
33-
generateQrCodeForCurrentApp(options: IHasUseHotModuleReloadOption): Promise<void>;
30+
interface IGenerateQrCodeOptions extends IHasUseHotModuleReloadOption {
31+
/**
32+
* If set to true, a link will be shown on console instead of QR code
33+
* Default value is false.
34+
*/
35+
link: boolean;
3436
}
3537
}

lib/definitions/qr-code.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
interface IQrCodeTerminalService {
2-
generate(url: string, message: string): void;
2+
generate(url: string): void;
33
}

lib/helpers/preview-command-helper.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.

lib/options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ export class Options {
113113
var: { type: OptionType.Object },
114114
default: { type: OptionType.Boolean },
115115
count: { type: OptionType.Number },
116-
hooks: { type: OptionType.Boolean, default: true }
116+
hooks: { type: OptionType.Boolean, default: true },
117+
link: { type: OptionType.Boolean, default: false }
117118
};
118119
}
119120

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { PlaygroundStoreUrls } from "./preview-app-constants";
21
import * as util from "util";
3-
const chalk = require("chalk");
2+
import { EOL } from "os";
3+
import { PlaygroundStoreUrls } from "./preview-app-constants";
44

55
export class PlaygroundQrCodeGenerator implements IPlaygroundQrCodeGenerator {
66
constructor(private $previewSdkService: IPreviewSdkService,
@@ -10,27 +10,8 @@ export class PlaygroundQrCodeGenerator implements IPlaygroundQrCodeGenerator {
1010
private $logger: ILogger) {
1111
}
1212

13-
public async generateQrCodeForiOS(): Promise<void> {
14-
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}.`;
15-
await this.generateQrCode(PlaygroundStoreUrls.APP_STORE_URL, message);
16-
}
17-
18-
public async generateQrCodeForAndroid(): Promise<void> {
19-
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}.`;
20-
await this.generateQrCode(PlaygroundStoreUrls.GOOGLE_PLAY_URL, message);
21-
}
22-
23-
public async generateQrCodeForCurrentApp(options: IHasUseHotModuleReloadOption): Promise<void> {
24-
const message = `Use ${"NativeScript Playground app".underline.bold} and scan the QR code below to preview the application on your device.`;
25-
await this.generateQrCode(this.$previewSdkService.getQrCodeUrl(options), message);
26-
}
27-
28-
private async generateQrCode(url: string, message: string): Promise<void> {
29-
await this.generateQrCodeCore(url, message);
30-
this.printUsage();
31-
}
32-
33-
private async generateQrCodeCore(url: string, message: string): Promise<void> {
13+
public async generateQrCode(options: IGenerateQrCodeOptions): Promise<void> {
14+
let url = this.$previewSdkService.getQrCodeUrl(options);
3415
const shortenUrlEndpoint = util.format(this.$config.SHORTEN_URL_ENDPOINT, encodeURIComponent(url));
3516
try {
3617
const response = await this.$httpClient.httpRequest(shortenUrlEndpoint);
@@ -40,20 +21,22 @@ export class PlaygroundQrCodeGenerator implements IPlaygroundQrCodeGenerator {
4021
// use the longUrl
4122
}
4223

43-
this.$qrCodeTerminalService.generate(url, message);
44-
}
45-
46-
private printUsage(): void {
47-
this.$logger.info(`
48-
-> Press ${this.underlineBoldCyan("a")} to get a link to NativeScript Playground app for ${this.underlineBoldCyan("Android")} on Google Play
49-
-> Press ${this.underlineBoldCyan("i")} to get a link to NativeScript Playground app for ${this.underlineBoldCyan("iOS")} on the App Store
50-
-> Press ${this.underlineBoldCyan("c")} to show the QR code of ${this.underlineBoldCyan("your application")}
51-
`);
52-
}
53-
54-
private underlineBoldCyan(str: string) {
55-
const { bold, underline, cyan } = chalk;
56-
return underline(bold(cyan(str)));
24+
this.$logger.info();
25+
const message = `${EOL} Generating qrcode for url ${url}.`;
26+
this.$logger.trace(message);
27+
28+
if (options.link) {
29+
this.$logger.printMarkdown(message);
30+
} else {
31+
this.$qrCodeTerminalService.generate(url);
32+
33+
this.$logger.info();
34+
this.$logger.printMarkdown(`# Use \`NativeScript Playground app\` and scan the \`QR code\` below to preview the application on your device.`);
35+
this.$logger.printMarkdown(`
36+
To scan the QR code and deploy your app on a device, you need to have the \`NativeScript Playground app\`:
37+
App Store (iOS): ${PlaygroundStoreUrls.APP_STORE_URL}
38+
Google Play (Android): ${PlaygroundStoreUrls.GOOGLE_PLAY_URL}`);
39+
}
5740
}
5841
}
5942
$injector.register("playgroundQrCodeGenerator", PlaygroundQrCodeGenerator);

lib/services/platform-environment-requirements.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
1515
private $injector: IInjector,
1616
private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator) { }
1717

18-
@cache()
19-
private get $previewCommandHelper(): IPreviewCommandHelper {
20-
return this.$injector.resolve("previewCommandHelper");
21-
}
22-
2318
@cache()
2419
private get $liveSyncService(): ILiveSyncService {
2520
return this.$injector.resolve("liveSyncService");
@@ -186,7 +181,6 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
186181
this.$errors.failWithoutHelp(`No project found. In order to sync to playground you need to go to project directory or specify --path option.`);
187182
}
188183

189-
this.$previewCommandHelper.run();
190184
await this.$liveSyncService.liveSync([], {
191185
syncToPreviewApp: true,
192186
projectDir,
@@ -200,7 +194,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
200194
useHotModuleReload: options.hmr
201195
});
202196

203-
await this.$playgroundQrCodeGenerator.generateQrCodeForCurrentApp({ useHotModuleReload: options.hmr });
197+
await this.$playgroundQrCodeGenerator.generateQrCode({ useHotModuleReload: options.hmr, link: options.link });
204198
}
205199
}
206200

lib/services/qr-code-terminal-service.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import { EOL } from "os";
2-
31
const qrcode = require("qrcode-terminal");
42

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

8-
public generate(url: string, message: string): void {
9-
this.$logger.info(`${EOL} ${message.blue}`);
10-
this.$logger.info(`${EOL} Generating qrcode for url ${url}.`);
11-
6+
public generate(url: string): void {
127
try {
138
qrcode.generate(url);
149
} catch (err) {

0 commit comments

Comments
 (0)