microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
bb77358c8dc7ea46fae9d6aa601a11fde8eed0fd

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/intellisenseHelper.ts

257lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import {FileSystem} from "../common/node/fileSystem";
5import * as path from "path";
6import * as Q from "q";
7import * as vscode from "vscode";
8import * as semver from "semver";
9import {Telemetry} from "../common/telemetry";
10import {TelemetryHelper} from "../common/telemetryHelper";
11import {CommandExecutor} from "../common/commandExecutor";
12import {TsConfigHelper} from "./tsconfigHelper";
13import {SettingsHelper} from "./settingsHelper";
14import {HostPlatform} from "../common/hostPlatform";
15import {Log} from "../common/log/log";
16import {LogLevel} from "../common/log/logHelper";
17
18
19
20interface IInstallProps {
21 installed: boolean;
22 version: string;
23}
24
25export class IntellisenseHelper {
26
27 private static s_typeScriptVersion = "1.8.2"; // preferred version of TypeScript for legacy VSCode installs
28 private static s_vsCodeVersion = "0.10.10-insider"; // preferred version of VSCode (current is 0.10.9, 0.10.10-insider+ will include native TypeScript support)
29 // note: semver considers "x.x.x-<string>" to be < "x.x.x"" - so we include insider here as the
30 // insider build is less than the release build of 0.10.10 and we will support it.
31
32 /**
33 * Helper method that configures the workspace for Salsa intellisense.
34 */
35 public static setupReactNativeIntellisense(): Q.Promise<void> {
36 // Telemetry - Send Salsa Environment setup information
37 const tsSalsaEnvSetup = TelemetryHelper.createTelemetryEvent("RNIntellisense");
38 TelemetryHelper.addTelemetryEventProperty(tsSalsaEnvSetup, "TsSalsaEnvSetup", !!process.env.VSCODE_TSJS, false);
39 Telemetry.send(tsSalsaEnvSetup);
40
41 const configureWorkspace = Q({})
42 .then(() => TsConfigHelper.allowJs(true))
43 .then(() => TsConfigHelper.addExcludePaths(["node_modules"]))
44 .then(() => IntellisenseHelper.installReactNativeTypings());
45
46 // The actions taken in the promise chain below may result in requring a restart.
47 const configureTypescript = Q(false)
48 .then((isRestartRequired: boolean) => IntellisenseHelper.enableSalsa(isRestartRequired))
49 .then((isRestartRequired: boolean) => IntellisenseHelper.verifyInstallTypeScript(isRestartRequired))
50 .then((isRestartRequired: boolean) => IntellisenseHelper.configureWorkspaceSettings(isRestartRequired))
51 .then((isRestartRequired: boolean) => IntellisenseHelper.warnIfRestartIsRequired(isRestartRequired))
52 .catch((err: any) => {
53 Log.logError("Error while setting up IntelliSense: " + err);
54 return Q.reject<void>(err);
55 });
56
57 /* TODO #83: Refactor this code to
58 Q.all([enableSalsa(), installTypescript(), configureWorkspace()])
59 .then((result) => warnIfRestartIsRequired(result.any((x) => x)))
60 */
61 return Q.all([configureWorkspace, configureTypescript]).then(() => { });
62 }
63
64 /**
65 * Helper method that install typings for React Native.
66 */
67 public static installReactNativeTypings(): Q.Promise<void> {
68 let reactTypingsSource = path.resolve(__dirname, "..", "..", "ReactTypings");
69 let reactTypingsDest = path.resolve(vscode.workspace.rootPath, ".vscode", "typings");
70 let fileSystem = new FileSystem();
71
72 return fileSystem.copyRecursive(reactTypingsSource, reactTypingsDest);
73 }
74
75 /**
76 * Helper method that verifies the correct version of TypeScript is installed.
77 * If using a newer version of VSCode TypeScript is installed by default and no
78 * action is needed. If using an older version, verify that the correct TS version is
79 * installed, if not install it.
80 */
81 public static verifyInstallTypeScript(isRestartRequired: boolean): Q.Promise<boolean> {
82
83 if (IntellisenseHelper.isSalsaSupported()) {
84 // this is the correct version of vscode, which includes TypeScript (Salsa) support, nothing to do here
85 return Q.resolve<boolean>(isRestartRequired);
86 }
87
88 return IntellisenseHelper.getInstalledTypeScriptVersion()
89 .then(function(installProps: IInstallProps) {
90
91 if (installProps.installed === true) {
92
93 if (semver.neq(IntellisenseHelper.s_typeScriptVersion, installProps.version)) {
94 Log.logInternalMessage(LogLevel.Debug, "TypeScript is installed with the wrong version: " + installProps.version);
95 return true;
96 } else {
97 Log.logInternalMessage(LogLevel.Debug, "Installed TypeScript version is correct");
98 return false;
99 }
100 } else {
101 Log.logInternalMessage(LogLevel.Debug, "TypeScript is not installed");
102 return true;
103 }
104 })
105 .then((install: boolean) => {
106
107 if (install) {
108 let installPath: string = path.resolve(HostPlatform.getUserHomePath(), ".vscode");
109 let runArguments: string[] = [];
110 let npmCommand: string = HostPlatform.getNpmCliCommand("npm");
111 runArguments.push("install");
112 runArguments.push("--prefix " + installPath);
113 runArguments.push("typescript@" + IntellisenseHelper.s_typeScriptVersion);
114
115 return new CommandExecutor(installPath).spawn(npmCommand, runArguments)
116 .then(() => {
117 return true;
118 })
119 .catch((err: any) => {
120 Log.logError("Error attempting to install TypeScript: " + err);
121 return Q.reject<boolean>(err);
122 });
123
124 } else {
125 return isRestartRequired;
126 }
127 });
128 }
129
130
131
132 public static configureWorkspaceSettings(isRestartRequired: boolean): Q.Promise<boolean> {
133 let typeScriptLibPath: string = path.resolve(IntellisenseHelper.getTypeScriptInstallPath(), "lib");
134
135 return SettingsHelper.getTypeScriptTsdk()
136 .then((tsdkPath: string) => {
137
138 if (IntellisenseHelper.isSalsaSupported()) {
139 if (tsdkPath !== null &&
140 tsdkPath === typeScriptLibPath) {
141 // Note: In previous releases of VSCode (< 0.10.10) the Salsa TypeScript
142 // IntelliSense was not enabled by default, this extension would install
143 // Salsa itself, and update the settings to point at that. Here we
144 // attempt to reset that value to null if it still points to the previous
145 // installed (and no longer valid) version of TypeScript.
146 return SettingsHelper.removeTypeScriptTsdk()
147 .then(() => { return true; });
148 }
149 } else {
150 if (tsdkPath === null) {
151 return SettingsHelper.setTypeScriptTsdk(typeScriptLibPath)
152 .then(() => { return true; });
153 }
154 }
155
156 return isRestartRequired;
157 });
158 }
159
160 public static warnIfRestartIsRequired(isRestartRequired: boolean): Q.Promise<void> {
161 if (isRestartRequired) {
162 vscode.window.showInformationMessage("React Native intellisense was successfully configured for this project. Restart to enable it.");
163 }
164
165 return;
166 }
167
168 /**
169 * Helper method that sets the environment variable and informs the user they need to restart
170 * in order to enable the Salsa intellisense.
171 */
172 public static enableSalsa(isRestartRequired: boolean): Q.Promise<boolean> {
173 if (!process.env.VSCODE_TSJS) {
174
175 return Q({})
176 .then(() => HostPlatform.setEnvironmentVariable("VSCODE_TSJS", "1"))
177 .then(() => { return true; });
178 }
179
180 return Q(isRestartRequired);
181 }
182
183 /**
184 * Simple check to see if the TypeScript package is in the expected location (where we installed it)
185 */
186 private static isTypeScriptInstalled(): Q.Promise<boolean> {
187 let fileSystem: FileSystem = new FileSystem();
188 let installPath: string = path.join(IntellisenseHelper.getTypeScriptInstallPath(), "lib");
189 return fileSystem.exists(installPath);
190 }
191
192 /**
193 * Checks for the existance of our installed TypeScript package, if it exists also determine its version
194 */
195 private static getInstalledTypeScriptVersion(): Q.Promise<IInstallProps> {
196 return IntellisenseHelper.isTypeScriptInstalled()
197 .then((installed: boolean) => {
198 let installProps: IInstallProps = {
199 installed: installed,
200 version: "",
201 };
202
203 if (installed === true) {
204 Log.logInternalMessage(LogLevel.Debug, "TypeScript is installed - checking version");
205 return IntellisenseHelper.readPackageJson()
206 .then((version: string) => {
207 installProps.version = version;
208 return installProps;
209 });
210 } else {
211 return installProps;
212 }
213 });
214 }
215
216 /**
217 * Read the package.json from the TypeScript install path and return the version if it's available
218 */
219 private static readPackageJson(): Q.Promise<string> {
220 let packageFilePath: string = path.join(IntellisenseHelper.getTypeScriptInstallPath(), "package.json");
221 let fileSystem = new FileSystem();
222
223 return fileSystem.exists(packageFilePath)
224 .then(function(exists: boolean): Q.Promise<string> {
225 if (!exists) {
226 return Q.reject<string>("package.json not found at:" + packageFilePath);
227 }
228
229 return fileSystem.readFile(packageFilePath, "utf-8");
230 })
231 .then(function(jsonContents: string): Q.Promise<any> {
232 let data = JSON.parse(jsonContents);
233 return data.version;
234 })
235 .catch((err: any) => {
236 Log.logError("Error while processing package.json: " + err);
237 return "0.0.0";
238 });
239 }
240
241 /**
242 * Simple helper to get the TypeScript install path
243 */
244 private static getTypeScriptInstallPath(): string {
245
246 let codePath: string = path.resolve(HostPlatform.getUserHomePath(), ".vscode");
247 let typeScriptLibPath: string = path.join(codePath, "node_modules", "typescript");
248 return typeScriptLibPath;
249 }
250
251 /**
252 * Simple helper to determine if the current version of VSCode supports TypeScript (Salsa) or better
253 */
254 private static isSalsaSupported(): boolean {
255 return semver.gte(vscode.version, IntellisenseHelper.s_vsCodeVersion, true);
256 }
257}