microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.3.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/intellisenseHelper.ts

304lines · 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 {JsConfigHelper} 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 private static VSCODE_SUPPORTS_ATA_SINCE = "1.7.2-insider";
32
33 /**
34 * Helper method that configures the workspace for Salsa intellisense.
35 */
36 public static setupReactNativeIntellisense(): Q.Promise<void> {
37 // Telemetry - Send Salsa Environment setup information
38 const tsSalsaEnvSetup = TelemetryHelper.createTelemetryEvent("RNIntellisense");
39 TelemetryHelper.addTelemetryEventProperty(tsSalsaEnvSetup, "TsSalsaEnvSetup", !!process.env.VSCODE_TSJS, false);
40 Telemetry.send(tsSalsaEnvSetup);
41
42 const configureWorkspace = JsConfigHelper.createJsConfigIfNotPresent()
43 .then(() => {
44 // VSCode versions >= 1.7.2-insider support ATA and will not require copying
45 // typings into workspace for intellisense
46 if (semver.lt(vscode.version, IntellisenseHelper.VSCODE_SUPPORTS_ATA_SINCE)) {
47 return IntellisenseHelper.installReactNativeTypings();
48 }
49 });
50
51 // The actions taken in the promise chain below may result in requring a restart.
52 const configureTypescript = Q(false)
53 .then((isRestartRequired: boolean) => IntellisenseHelper.enableSalsa(isRestartRequired))
54 .then((isRestartRequired: boolean) => IntellisenseHelper.verifyInstallTypeScript(isRestartRequired))
55 .then((isRestartRequired: boolean) => IntellisenseHelper.configureWorkspaceSettings(isRestartRequired))
56 .then((isRestartRequired: boolean) => IntellisenseHelper.warnIfRestartIsRequired(isRestartRequired))
57 .catch((err: any) => {
58 Log.logError("Error while setting up IntelliSense: " + err);
59 return Q.reject<void>(err);
60 });
61
62 /* TODO #83: Refactor this code to
63 Q.all([enableSalsa(), installTypescript(), configureWorkspace()])
64 .then((result) => warnIfRestartIsRequired(result.any((x) => x)))
65 */
66 return Q.all([configureWorkspace, configureTypescript]).then(() => { });
67 }
68
69 /**
70 * Helper method that install typings for React Native.
71 */
72 public static installReactNativeTypings(): Q.Promise<void> {
73 const typingsSource = path.resolve(__dirname, "..", "..", "ReactTypings");
74 const reactTypings = path.resolve(typingsSource, "react");
75 const reactNativeTypings = path.resolve(typingsSource, "react-native");
76 const typingsIndex = path.resolve(typingsSource, "react-native.d.ts.index");
77
78 const typingsDestination = path.resolve(vscode.workspace.rootPath, ".vscode", "typings");
79 const reactTypingsDestination = path.resolve(typingsDestination, "react");
80 const reactNativeTypingsDestination = path.resolve(typingsDestination, "react-native");
81 const typingsIndexDestination = path.resolve(vscode.workspace.rootPath, "typings");
82 const typingIndexFinalPath = path.resolve(typingsIndexDestination, "react-native.d.ts");
83
84 let fileSystem = new FileSystem();
85
86 const createTypingsDirectoryIfNeeded = fileSystem.directoryExists(typingsDestination).
87 then((exists) => {
88 if (!exists) {
89 return fileSystem.makeDirectoryRecursiveSync(typingsDestination);
90 }
91 });
92 const copyReactTypingsIfNeeded = fileSystem.directoryExists(reactTypingsDestination)
93 .then((exists) => {
94 if (!exists) {
95 return fileSystem.copyRecursive(reactTypings, reactTypingsDestination);
96 }
97 });
98 const copyReactNativeTypingsIfNeeded = fileSystem.directoryExists(reactNativeTypingsDestination)
99 .then((exists) => {
100 if (!exists) {
101 return fileSystem.copyRecursive(reactNativeTypings, reactNativeTypingsDestination);
102 }
103 });
104
105 const copyTypingsIndexIfNeeded = fileSystem.directoryExists(typingsIndexDestination)
106 .then((exists) => {
107 if (!exists) {
108 return fileSystem.makeDirectoryRecursiveSync(typingsIndexDestination);
109 }
110 })
111 .then(() => fileSystem.exists(typingIndexFinalPath))
112 .then((exists) => {
113 if (!exists) {
114 return fileSystem.copyFile(typingsIndex, typingIndexFinalPath);
115 }
116 });
117
118 return Q.all([
119 createTypingsDirectoryIfNeeded,
120 copyReactTypingsIfNeeded,
121 copyReactNativeTypingsIfNeeded,
122 copyTypingsIndexIfNeeded,
123 ]).then(() => { });
124 }
125
126 /**
127 * Helper method that verifies the correct version of TypeScript is installed.
128 * If using a newer version of VSCode TypeScript is installed by default and no
129 * action is needed. If using an older version, verify that the correct TS version is
130 * installed, if not install it.
131 */
132 public static verifyInstallTypeScript(isRestartRequired: boolean): Q.Promise<boolean> {
133
134 if (IntellisenseHelper.isSalsaSupported()) {
135 // this is the correct version of vscode, which includes TypeScript (Salsa) support, nothing to do here
136 return Q.resolve<boolean>(isRestartRequired);
137 }
138
139 return IntellisenseHelper.getInstalledTypeScriptVersion()
140 .then(function(installProps: IInstallProps) {
141
142 if (installProps.installed === true) {
143
144 if (semver.neq(IntellisenseHelper.s_typeScriptVersion, installProps.version)) {
145 Log.logInternalMessage(LogLevel.Debug, "TypeScript is installed with the wrong version: " + installProps.version);
146 return true;
147 } else {
148 Log.logInternalMessage(LogLevel.Debug, "Installed TypeScript version is correct");
149 return false;
150 }
151 } else {
152 Log.logInternalMessage(LogLevel.Debug, "TypeScript is not installed");
153 return true;
154 }
155 })
156 .then((install: boolean) => {
157
158 if (install) {
159 let installPath: string = path.resolve(HostPlatform.getUserHomePath(), ".vscode");
160 let runArguments: string[] = [];
161 let npmCommand: string = HostPlatform.getNpmCliCommand("npm");
162 runArguments.push("install");
163 runArguments.push("--prefix " + installPath);
164 runArguments.push("typescript@" + IntellisenseHelper.s_typeScriptVersion);
165
166 return new CommandExecutor(installPath).spawn(npmCommand, runArguments)
167 .then(() => {
168 return true;
169 })
170 .catch((err: any) => {
171 Log.logError("Error attempting to install TypeScript: " + err);
172 return Q.reject<boolean>(err);
173 });
174
175 } else {
176 return isRestartRequired;
177 }
178 });
179 }
180
181
182
183 public static configureWorkspaceSettings(isRestartRequired: boolean): boolean {
184 let typeScriptLibPath: string = path.resolve(IntellisenseHelper.getTypeScriptInstallPath(), "lib");
185 const tsdkPath = SettingsHelper.getTypeScriptTsdk();
186
187 if (IntellisenseHelper.isSalsaSupported()) {
188 if (tsdkPath === typeScriptLibPath) {
189 // Note: In previous releases of VSCode (< 0.10.10) the Salsa TypeScript
190 // IntelliSense was not enabled by default, this extension would install
191 // Salsa itself, and update the settings to point at that. Here we
192 // attempt to reset that value to null if it still points to the previous
193 // installed (and no longer valid) version of TypeScript.
194 SettingsHelper.notifyUserToRemoveTSDKFromSettingsJson(tsdkPath);
195 // We are already telling the user to restart. No need to show another message.
196 return false;
197 }
198 } else {
199 if (tsdkPath === null) {
200 SettingsHelper.notifyUserToAddTSDKInSettingsJson(typeScriptLibPath);
201 // We are already telling the user to restart. No need to show another message.
202 return false;
203 }
204 }
205 return isRestartRequired;
206 }
207
208 public static warnIfRestartIsRequired(isRestartRequired: boolean): Q.Promise<void> {
209 if (isRestartRequired) {
210 vscode.window.showInformationMessage("React Native intellisense was successfully configured for this project. Restart to enable it.");
211 }
212
213 return;
214 }
215
216 /**
217 * Helper method that sets the environment variable and informs the user they need to restart
218 * in order to enable the Salsa intellisense.
219 */
220 public static enableSalsa(isRestartRequired: boolean): Q.Promise<boolean> {
221 if (!IntellisenseHelper.isSalsaSupported() && !process.env.VSCODE_TSJS) {
222 return Q({})
223 .then(() => HostPlatform.setEnvironmentVariable("VSCODE_TSJS", "1"))
224 .then(() => { return true; });
225 }
226
227 return Q(isRestartRequired);
228 }
229
230 /**
231 * Simple check to see if the TypeScript package is in the expected location (where we installed it)
232 */
233 private static isTypeScriptInstalled(): Q.Promise<boolean> {
234 let fileSystem: FileSystem = new FileSystem();
235 let installPath: string = path.join(IntellisenseHelper.getTypeScriptInstallPath(), "lib");
236 return fileSystem.exists(installPath);
237 }
238
239 /**
240 * Checks for the existance of our installed TypeScript package, if it exists also determine its version
241 */
242 private static getInstalledTypeScriptVersion(): Q.Promise<IInstallProps> {
243 return IntellisenseHelper.isTypeScriptInstalled()
244 .then((installed: boolean) => {
245 let installProps: IInstallProps = {
246 installed: installed,
247 version: "",
248 };
249
250 if (installed === true) {
251 Log.logInternalMessage(LogLevel.Debug, "TypeScript is installed - checking version");
252 return IntellisenseHelper.readPackageJson()
253 .then((version: string) => {
254 installProps.version = version;
255 return installProps;
256 });
257 } else {
258 return installProps;
259 }
260 });
261 }
262
263 /**
264 * Read the package.json from the TypeScript install path and return the version if it's available
265 */
266 private static readPackageJson(): Q.Promise<string> {
267 let packageFilePath: string = path.join(IntellisenseHelper.getTypeScriptInstallPath(), "package.json");
268 let fileSystem = new FileSystem();
269
270 return fileSystem.exists(packageFilePath)
271 .then(function(exists: boolean): Q.Promise<string> {
272 if (!exists) {
273 return Q.reject<string>("package.json not found at:" + packageFilePath);
274 }
275
276 return fileSystem.readFile(packageFilePath, "utf-8");
277 })
278 .then(function(jsonContents: string): Q.Promise<any> {
279 let data = JSON.parse(jsonContents);
280 return data.version;
281 })
282 .catch((err: any) => {
283 Log.logError("Error while processing package.json: " + err);
284 return "0.0.0";
285 });
286 }
287
288 /**
289 * Simple helper to get the TypeScript install path
290 */
291 private static getTypeScriptInstallPath(): string {
292
293 let codePath: string = path.resolve(HostPlatform.getUserHomePath(), ".vscode");
294 let typeScriptLibPath: string = path.join(codePath, "node_modules", "typescript");
295 return typeScriptLibPath;
296 }
297
298 /**
299 * Simple helper to determine if the current version of VSCode supports TypeScript (Salsa) or better
300 */
301 private static isSalsaSupported(): boolean {
302 return semver.gte(vscode.version, IntellisenseHelper.s_vsCodeVersion, true);
303 }
304}