microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.1.5

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/intellisenseHelper.ts

253lines · 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): boolean {
133 let typeScriptLibPath: string = path.resolve(IntellisenseHelper.getTypeScriptInstallPath(), "lib");
134 const tsdkPath = SettingsHelper.getTypeScriptTsdk();
135
136 if (IntellisenseHelper.isSalsaSupported()) {
137 if (tsdkPath === typeScriptLibPath) {
138 // Note: In previous releases of VSCode (< 0.10.10) the Salsa TypeScript
139 // IntelliSense was not enabled by default, this extension would install
140 // Salsa itself, and update the settings to point at that. Here we
141 // attempt to reset that value to null if it still points to the previous
142 // installed (and no longer valid) version of TypeScript.
143 SettingsHelper.notifyUserToRemoveTSDKFromSettingsJson(tsdkPath);
144 // We are already telling the user to restart. No need to show another message.
145 return false;
146 }
147 } else {
148 if (tsdkPath === null) {
149 SettingsHelper.notifyUserToAddTSDKInSettingsJson(typeScriptLibPath);
150 // We are already telling the user to restart. No need to show another message.
151 return false;
152 }
153 }
154 return isRestartRequired;
155 }
156
157 public static warnIfRestartIsRequired(isRestartRequired: boolean): Q.Promise<void> {
158 if (isRestartRequired) {
159 vscode.window.showInformationMessage("React Native intellisense was successfully configured for this project. Restart to enable it.");
160 }
161
162 return;
163 }
164
165 /**
166 * Helper method that sets the environment variable and informs the user they need to restart
167 * in order to enable the Salsa intellisense.
168 */
169 public static enableSalsa(isRestartRequired: boolean): Q.Promise<boolean> {
170 if (!IntellisenseHelper.isSalsaSupported() && !process.env.VSCODE_TSJS) {
171 return Q({})
172 .then(() => HostPlatform.setEnvironmentVariable("VSCODE_TSJS", "1"))
173 .then(() => { return true; });
174 }
175
176 return Q(isRestartRequired);
177 }
178
179 /**
180 * Simple check to see if the TypeScript package is in the expected location (where we installed it)
181 */
182 private static isTypeScriptInstalled(): Q.Promise<boolean> {
183 let fileSystem: FileSystem = new FileSystem();
184 let installPath: string = path.join(IntellisenseHelper.getTypeScriptInstallPath(), "lib");
185 return fileSystem.exists(installPath);
186 }
187
188 /**
189 * Checks for the existance of our installed TypeScript package, if it exists also determine its version
190 */
191 private static getInstalledTypeScriptVersion(): Q.Promise<IInstallProps> {
192 return IntellisenseHelper.isTypeScriptInstalled()
193 .then((installed: boolean) => {
194 let installProps: IInstallProps = {
195 installed: installed,
196 version: "",
197 };
198
199 if (installed === true) {
200 Log.logInternalMessage(LogLevel.Debug, "TypeScript is installed - checking version");
201 return IntellisenseHelper.readPackageJson()
202 .then((version: string) => {
203 installProps.version = version;
204 return installProps;
205 });
206 } else {
207 return installProps;
208 }
209 });
210 }
211
212 /**
213 * Read the package.json from the TypeScript install path and return the version if it's available
214 */
215 private static readPackageJson(): Q.Promise<string> {
216 let packageFilePath: string = path.join(IntellisenseHelper.getTypeScriptInstallPath(), "package.json");
217 let fileSystem = new FileSystem();
218
219 return fileSystem.exists(packageFilePath)
220 .then(function(exists: boolean): Q.Promise<string> {
221 if (!exists) {
222 return Q.reject<string>("package.json not found at:" + packageFilePath);
223 }
224
225 return fileSystem.readFile(packageFilePath, "utf-8");
226 })
227 .then(function(jsonContents: string): Q.Promise<any> {
228 let data = JSON.parse(jsonContents);
229 return data.version;
230 })
231 .catch((err: any) => {
232 Log.logError("Error while processing package.json: " + err);
233 return "0.0.0";
234 });
235 }
236
237 /**
238 * Simple helper to get the TypeScript install path
239 */
240 private static getTypeScriptInstallPath(): string {
241
242 let codePath: string = path.resolve(HostPlatform.getUserHomePath(), ".vscode");
243 let typeScriptLibPath: string = path.join(codePath, "node_modules", "typescript");
244 return typeScriptLibPath;
245 }
246
247 /**
248 * Simple helper to determine if the current version of VSCode supports TypeScript (Salsa) or better
249 */
250 private static isSalsaSupported(): boolean {
251 return semver.gte(vscode.version, IntellisenseHelper.s_vsCodeVersion, true);
252 }
253}