Skip to content

chore: check network connectivity when tns preview command is executed #3906

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 1 commit into from
Sep 18, 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: 2 additions & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,5 @@ $injector.require("iOSDebuggerPortService", "./services/ios-debugger-port-servic

$injector.require("pacoteService", "./services/pacote-service");
$injector.require("qrCodeTerminalService", "./services/qr-code-terminal-service");

$injector.require("networkConnectivityValidator", "./helpers/network-connectivity-validator");
2 changes: 2 additions & 0 deletions lib/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export class PreviewCommand implements ICommand {

constructor(private $bundleValidatorHelper: IBundleValidatorHelper,
private $liveSyncService: ILiveSyncService,
private $networkConnectivityValidator: INetworkConnectivityValidator,
private $projectData: IProjectData,
private $options: IOptions,
private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator,
Expand All @@ -27,6 +28,7 @@ export class PreviewCommand implements ICommand {
}

public async canExecute(args: string[]): Promise<boolean> {
await this.$networkConnectivityValidator.validate();
this.$bundleValidatorHelper.validate();
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,4 +865,8 @@ interface IAssetsGenerationService {
interface IRuntimeGradleVersions {
gradleVersion?: string;
gradleAndroidPluginVersion?: string;
}

interface INetworkConnectivityValidator {
validate(): Promise<void>;
}
31 changes: 31 additions & 0 deletions lib/helpers/network-connectivity-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as dns from "dns";

export class NetworkConnectivityValidator implements INetworkConnectivityValidator {
private static DNS_LOOKUP_URL = "play.nativescript.org";
private static NO_INTERNET_ERROR_CODE = "ENOTFOUND";
private static NO_INTERNET_ERROR_MESSAGE = "No internet connection. Check your internet settings and try again.";

constructor(private $errors: IErrors,
private $logger: ILogger) { }

public async validate(): Promise<void> {
const isConnected = await this.isConnected();
if (!isConnected) {
this.$errors.failWithoutHelp(NetworkConnectivityValidator.NO_INTERNET_ERROR_MESSAGE);
}
}

private isConnected(): Promise<boolean> {
return new Promise((resolve, reject) => {
dns.lookup(NetworkConnectivityValidator.DNS_LOOKUP_URL, err => {
this.$logger.trace(`Error from dns.lookup is ${err}.`);
if (err && err.code === NetworkConnectivityValidator.NO_INTERNET_ERROR_CODE) {
resolve(false);
} else {
resolve(true);
}
});
});
}
}
$injector.register("networkConnectivityValidator", NetworkConnectivityValidator);