Skip to content

Instantly share code, notes, and snippets.

@Mitko-Kerezov
Created March 17, 2016 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mitko-Kerezov/2036e9c5eaf9e445b586 to your computer and use it in GitHub Desktop.
Save Mitko-Kerezov/2036e9c5eaf9e445b586 to your computer and use it in GitHub Desktop.
Doctor Service
///<reference path="../.d.ts"/>
"use strict";
var os_1 = require("os");
var semver = require("semver");
var path = require("path");
var clui = require("clui");
var DoctorService = (function () {
function DoctorService($analyticsService, $androidToolsInfo, $hostInfo, $logger, $progressIndicator, $sysInfo, $childProcess, $config, $npm, $opener, $prompter, $fs) {
this.$analyticsService = $analyticsService;
this.$androidToolsInfo = $androidToolsInfo;
this.$hostInfo = $hostInfo;
this.$logger = $logger;
this.$progressIndicator = $progressIndicator;
this.$sysInfo = $sysInfo;
this.$childProcess = $childProcess;
this.$config = $config;
this.$npm = $npm;
this.$opener = $opener;
this.$prompter = $prompter;
this.$fs = $fs;
}
DoctorService.prototype.printWarnings = function (configOptions) {
var result = false;
var sysInfo = this.$sysInfo.getSysInfo(path.join(__dirname, "..", "..", "package.json")).wait();
if (!sysInfo.adbVer) {
this.$logger.warn("WARNING: adb from the Android SDK is not installed or is not configured properly.");
this.$logger.out("For Android-related operations, the NativeScript CLI will use a built-in version of adb." + os_1.EOL
+ "To avoid possible issues with the native Android emulator, Genymotion or connected" + os_1.EOL
+ "Android devices, verify that you have installed the latest Android SDK and" + os_1.EOL
+ "its dependencies as described in http://developer.android.com/sdk/index.html#Requirements" + os_1.EOL);
this.printPackageManagerTip();
result = true;
}
if (!sysInfo.androidInstalled) {
this.$logger.warn("WARNING: The Android SDK is not installed or is not configured properly.");
this.$logger.out("You will not be able to build your projects for Android and run them in the native emulator." + os_1.EOL
+ "To be able to build for Android and run apps in the native emulator, verify that you have" + os_1.EOL
+ "installed the latest Android SDK and its dependencies as described in http://developer.android.com/sdk/index.html#Requirements" + os_1.EOL);
this.printPackageManagerTip();
result = true;
}
if (this.$hostInfo.isDarwin) {
if (!sysInfo.xcodeVer) {
this.$logger.warn("WARNING: Xcode is not installed or is not configured properly.");
this.$logger.out("You will not be able to build your projects for iOS or run them in the iOS Simulator." + os_1.EOL
+ "To be able to build for iOS and run apps in the native emulator, verify that you have installed Xcode." + os_1.EOL);
result = true;
}
if (!sysInfo.cocoapodVer) {
this.$logger.warn("WARNING: CocoaPods is not installed or is not configured properly.");
this.$logger.out("You will not be able to build your projects for iOS if they contain plugin with CocoaPod file." + os_1.EOL
+ "To be able to build such projects, verify that you have installed CocoaPods.");
result = true;
}
if (sysInfo.xcodeVer && sysInfo.cocoapodVer) {
var problemWithCocoaPods = this.verifyCocoaPods();
if (problemWithCocoaPods) {
this.$logger.warn("WARNING: There was a problem with CocoaPods");
this.$logger.out("Verify that CocoaPods are configured properly.");
result = true;
}
}
if (sysInfo.cocoapodVer && semver.valid(sysInfo.cocoapodVer) === null) {
this.$logger.warn("WARNING: CocoaPods version is not a valid semver version.");
this.$logger.out("You will not be able to build your projects for iOS if they contain plugin with CocoaPod file." + os_1.EOL
+ ("To be able to build such projects, verify that you have at least " + DoctorService.MIN_SUPPORTED_POD_VERSION + " version installed."));
result = true;
}
if (sysInfo.cocoapodVer && semver.valid(sysInfo.cocoapodVer) && semver.lt(sysInfo.cocoapodVer, DoctorService.MIN_SUPPORTED_POD_VERSION)) {
this.$logger.warn("WARNING: CocoaPods version is lower than " + DoctorService.MIN_SUPPORTED_POD_VERSION + ".");
this.$logger.out("You will not be able to build your projects for iOS if they contain plugin with CocoaPod file." + os_1.EOL
+ ("To be able to build such projects, verify that you have at least " + DoctorService.MIN_SUPPORTED_POD_VERSION + " version installed."));
result = true;
}
}
else {
this.$logger.out("NOTE: You can develop for iOS only on Mac OS X systems.");
this.$logger.out("To be able to work with iOS devices and projects, you need Mac OS X Mavericks or later." + os_1.EOL);
}
var androidToolsIssues = this.$androidToolsInfo.validateInfo().wait();
var javaVersionIssue = this.$androidToolsInfo.validateJavacVersion(sysInfo.javacVersion).wait();
var doctorResult = result || androidToolsIssues || javaVersionIssue;
if (!configOptions || configOptions.trackResult) {
this.$analyticsService.track("DoctorEnvironmentSetup", doctorResult ? "incorrect" : "correct").wait();
}
if (doctorResult) {
this.$logger.info("There seem to be issues with your configuration.");
if (this.$hostInfo.isDarwin) {
this.promptForHelp(DoctorService.DarwinSetupDocsLink, DoctorService.DarwinSetupScriptLocation, []).wait();
}
else if (this.$hostInfo.isWindows) {
this.promptForHelp(DoctorService.WindowsSetupDocsLink, DoctorService.WindowsSetupScriptExecutable, DoctorService.WindowsSetupScriptArguments).wait();
}
else {
this.promptForDocs(DoctorService.LinuxSetupDocsLink).wait();
}
}
return doctorResult;
};
DoctorService.prototype.promptForDocs = function (link) {
var _this = this;
return (function () {
if (_this.$prompter.confirm("Do you want to visit the official documentation?", function () { return false; }).wait()) {
_this.$opener.open(link);
}
}).future()();
};
DoctorService.prototype.promptForHelp = function (link, commandName, commandArguments) {
var _this = this;
return (function () {
_this.promptForDocs(link).wait();
if (_this.$prompter.confirm("Do you want to run the setup script?", function () { return false; }).wait()) {
_this.$childProcess.spawnFromEvent(commandName, commandArguments, "close", { stdio: "inherit" }).wait();
}
}).future()();
};
DoctorService.prototype.printPackageManagerTip = function () {
if (this.$hostInfo.isWindows) {
this.$logger.out("TIP: To avoid setting up the necessary environment variables, you can use the chocolatey package manager to install the Android SDK and its dependencies." + os_1.EOL);
}
else if (this.$hostInfo.isDarwin) {
this.$logger.out("TIP: To avoid setting up the necessary environment variables, you can use the Homebrew package manager to install the Android SDK and its dependencies." + os_1.EOL);
}
};
DoctorService.prototype.verifyCocoaPods = function () {
this.$logger.out("Verifying CocoaPods. This may take more than a minute, please be patient.");
var temp = require("temp");
temp.track();
var projDir = temp.mkdirSync("nativescript-check-cocoapods");
var spinner = new clui.Spinner("Installing iOS runtime.");
try {
spinner.start();
this.$npm.install("tns-ios", projDir, { "ignore-scripts": true, production: true }).wait();
spinner.stop();
var iosDir = path.join(projDir, "node_modules", "tns-ios", "framework");
this.$fs.writeFile(path.join(iosDir, "Podfile"), "pod 'AFNetworking', '~> 1.0'\n").wait();
spinner.message("Verifying CocoaPods. This may take some time, please be patient.");
spinner.start();
var future = this.$childProcess.spawnFromEvent(this.$config.USE_POD_SANDBOX ? "sandbox-pod" : "pod", ["install"], "exit", { stdio: "inherit", cwd: iosDir }, { throwError: true });
this.$progressIndicator.showProgressIndicator(future, 5000).wait();
return !(this.$fs.exists(path.join(iosDir, "__PROJECT_NAME__.xcworkspace")).wait());
}
catch (err) {
this.$logger.trace("verifyCocoaPods error: " + err);
return true;
}
finally {
spinner.stop();
}
};
DoctorService.MIN_SUPPORTED_POD_VERSION = "0.38.2";
DoctorService.DarwinSetupScriptLocation = path.join(__dirname, "..", "..", "setup", "mac-startup-shell-script.sh");
DoctorService.DarwinSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-os-x";
DoctorService.WindowsSetupScriptExecutable = "powershell.exe";
DoctorService.WindowsSetupScriptArguments = ["start-process", "-FilePath", "PowerShell.exe", "-NoNewWindow", "-Wait", "-ArgumentList", '"-NoProfile -ExecutionPolicy Bypass -Command iex ((new-object net.webclient).DownloadString(\'https://raw.githubusercontent.com/NativeScript/nativescript-cli/production/setup/native-script.ps1\'))"'];
DoctorService.WindowsSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-win";
DoctorService.LinuxSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-linux";
return DoctorService;
}());
$injector.register("doctorService", DoctorService);
//# sourceMappingURL=doctor-service.js.map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment