-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathdoctor-service.ts
154 lines (129 loc) · 6.16 KB
/
doctor-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { EOL } from "os";
import * as path from "path";
import * as helpers from "../common/helpers";
import { doctor, constants } from "nativescript-doctor";
class DoctorService implements IDoctorService {
private static DarwinSetupScriptLocation = path.join(__dirname, "..", "..", "setup", "mac-startup-shell-script.sh");
private static DarwinSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-os-x";
private static WindowsSetupScriptExecutable = "powershell.exe";
private static WindowsSetupScriptArguments = ["start-process", "-FilePath", "PowerShell.exe", "-NoNewWindow", "-Wait", "-ArgumentList", '"-NoProfile -ExecutionPolicy Bypass -Command iex ((new-object net.webclient).DownloadString(\'https://www.nativescript.org/setup/win\'))"'];
private static WindowsSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-win";
private static LinuxSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-linux";
constructor(private $analyticsService: IAnalyticsService,
private $hostInfo: IHostInfo,
private $logger: ILogger,
private $childProcess: IChildProcess,
private $opener: IOpener,
private $prompter: IPrompter,
private $terminalSpinnerService: ITerminalSpinnerService,
private $versionsService: IVersionsService) { }
public async printWarnings(configOptions?: { trackResult: boolean }): Promise<void> {
const infos = await this.$terminalSpinnerService.execute<NativeScriptDoctor.IInfo[]>({
text: `Getting environment information ${EOL}`
}, () => doctor.getInfos());
const warnings = infos.filter(info => info.type === constants.WARNING_TYPE_NAME);
const hasWarnings = warnings.length > 0;
const hasAndroidWarnings = warnings.filter(warning => _.includes(warning.platforms, constants.ANDROID_PLATFORM_NAME)).length > 0;
if (hasAndroidWarnings) {
this.printPackageManagerTip();
}
if (!configOptions || configOptions.trackResult) {
await this.$analyticsService.track("DoctorEnvironmentSetup", hasWarnings ? "incorrect" : "correct");
}
this.printInfosCore(infos);
if (hasWarnings) {
this.$logger.info("There seem to be issues with your configuration.");
await this.promptForHelp();
} else {
this.$logger.out("No issues were detected.".bold);
}
try {
await this.$versionsService.checkComponentsForUpdate();
} catch (err) {
this.$logger.error("Cannot get the latest versions information from npm. Please try again later.");
}
}
public runSetupScript(): Promise<ISpawnResult> {
if (this.$hostInfo.isLinux) {
return;
}
this.$logger.out("Running the setup script to try and automatically configure your environment.");
if (this.$hostInfo.isDarwin) {
return this.runSetupScriptCore(DoctorService.DarwinSetupScriptLocation, []);
}
if (this.$hostInfo.isWindows) {
return this.runSetupScriptCore(DoctorService.WindowsSetupScriptExecutable, DoctorService.WindowsSetupScriptArguments);
}
}
public async canExecuteLocalBuild(platform?: string): Promise<boolean> {
const infos = await doctor.getInfos({ platform });
this.printInfosCore(infos);
const warnings = this.filterInfosByType(infos, constants.WARNING_TYPE_NAME);
if (warnings.length > 0) {
this.printInfosCore(infos);
} else {
infos.map(info => this.$logger.trace(info.message));
}
return warnings.length === 0;
}
private async promptForDocs(link: string): Promise<void> {
if (await this.$prompter.confirm("Do you want to visit the official documentation?", () => helpers.isInteractive())) {
this.$opener.open(link);
}
}
private async promptForSetupScript(executablePath: string, setupScriptArgs: string[]): Promise<void> {
if (await this.$prompter.confirm("Do you want to run the setup script?", () => helpers.isInteractive())) {
await this.runSetupScriptCore(executablePath, setupScriptArgs);
}
}
private async promptForHelp(): Promise<void> {
if (this.$hostInfo.isDarwin) {
await this.promptForHelpCore(DoctorService.DarwinSetupDocsLink, DoctorService.DarwinSetupScriptLocation, []);
} else if (this.$hostInfo.isWindows) {
await this.promptForHelpCore(DoctorService.WindowsSetupDocsLink, DoctorService.WindowsSetupScriptExecutable, DoctorService.WindowsSetupScriptArguments);
} else {
await this.promptForDocs(DoctorService.LinuxSetupDocsLink);
}
}
private async promptForHelpCore(link: string, setupScriptExecutablePath: string, setupScriptArgs: string[]): Promise<void> {
await this.promptForDocs(link);
await this.promptForSetupScript(setupScriptExecutablePath, setupScriptArgs);
}
private async runSetupScriptCore(executablePath: string, setupScriptArgs: string[]): Promise<ISpawnResult> {
return this.$childProcess.spawnFromEvent(executablePath, setupScriptArgs, "close", { stdio: "inherit" });
}
private printPackageManagerTip() {
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." + 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." + EOL);
}
}
private printInfosCore(infos: NativeScriptDoctor.IInfo[]): void {
if (!helpers.isInteractive()) {
infos.map(info => {
let message = info.message;
if (info.type === constants.WARNING_TYPE_NAME) {
message = `WARNING: ${info.message.yellow} ${EOL} ${info.additionalInformation} ${EOL}`;
}
this.$logger.out(message);
});
}
infos.filter(info => info.type === constants.INFO_TYPE_NAME)
.map(info => {
const spinner = this.$terminalSpinnerService.createSpinner();
spinner.text = info.message;
spinner.succeed();
});
infos.filter(info => info.type === constants.WARNING_TYPE_NAME)
.map(info => {
const spinner = this.$terminalSpinnerService.createSpinner();
spinner.text = `${info.message.yellow} ${EOL} ${info.additionalInformation} ${EOL}`;
spinner.fail();
});
}
private filterInfosByType(infos: NativeScriptDoctor.IInfo[], type: string): NativeScriptDoctor.IInfo[] {
return infos.filter(info => info.type === type);
}
}
$injector.register("doctorService", DoctorService);