microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/intellisenseHelper.ts
304lines · modeblame
bfd09d23Joshua Skelton10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
| 4 | import {FileSystem} from "../common/node/fileSystem"; | |
| 5 | import * as path from "path"; | |
76176919Joshua Skelton10 years ago | 6 | import * as Q from "q"; |
bfd09d23Joshua Skelton10 years ago | 7 | import * as vscode from "vscode"; |
8a9925eeEric Hamilton10 years ago | 8 | import * as semver from "semver"; |
88e05f1bJoshua Skelton10 years ago | 9 | import {Telemetry} from "../common/telemetry"; |
| 10 | import {TelemetryHelper} from "../common/telemetryHelper"; | |
b1df983eEric Hamilton10 years ago | 11 | import {CommandExecutor} from "../common/commandExecutor"; |
8bde1c02Vladimir Kotikov9 years ago | 12 | import {JsConfigHelper} from "./tsconfigHelper"; |
bfd09d23Joshua Skelton10 years ago | 13 | import {SettingsHelper} from "./settingsHelper"; |
a1005420dlebu10 years ago | 14 | import {HostPlatform} from "../common/hostPlatform"; |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 15 | import {Log} from "../common/log/log"; |
| 16 | import {LogLevel} from "../common/log/logHelper"; | |
8a9925eeEric Hamilton10 years ago | 17 | |
| 18 | | |
b1df983eEric Hamilton10 years ago | 19 | |
9948de5dEric Hamilton10 years ago | 20 | interface IInstallProps { |
8a9925eeEric Hamilton10 years ago | 21 | installed: boolean; |
| 22 | version: string; | |
| 23 | } | |
bfd09d23Joshua Skelton10 years ago | 24 | |
| 25 | export class IntellisenseHelper { | |
8a9925eeEric Hamilton10 years ago | 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) | |
b1df983eEric Hamilton10 years ago | 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. | |
8bde1c02Vladimir Kotikov9 years ago | 31 | private static VSCODE_SUPPORTS_ATA_SINCE = "1.7.2-insider"; |
8a9925eeEric Hamilton10 years ago | 32 | |
5e53b45aJoshua Skelton10 years ago | 33 | /** |
9dc82fe8Joshua Skelton10 years ago | 34 | * Helper method that configures the workspace for Salsa intellisense. |
5e53b45aJoshua Skelton10 years ago | 35 | */ |
10873e11digeff10 years ago | 36 | public static setupReactNativeIntellisense(): Q.Promise<void> { |
88e05f1bJoshua Skelton10 years ago | 37 | // Telemetry - Send Salsa Environment setup information |
10873e11digeff10 years ago | 38 | const tsSalsaEnvSetup = TelemetryHelper.createTelemetryEvent("RNIntellisense"); |
88e05f1bJoshua Skelton10 years ago | 39 | TelemetryHelper.addTelemetryEventProperty(tsSalsaEnvSetup, "TsSalsaEnvSetup", !!process.env.VSCODE_TSJS, false); |
| 40 | Telemetry.send(tsSalsaEnvSetup); | |
| 41 | | |
8bde1c02Vladimir Kotikov9 years ago | 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 | }); | |
bfd09d23Joshua Skelton10 years ago | 50 | |
cdbc4389Joshua Skelton10 years ago | 51 | // The actions taken in the promise chain below may result in requring a restart. |
10873e11digeff10 years ago | 52 | const configureTypescript = Q(false) |
cdbc4389Joshua Skelton10 years ago | 53 | .then((isRestartRequired: boolean) => IntellisenseHelper.enableSalsa(isRestartRequired)) |
8a9925eeEric Hamilton10 years ago | 54 | .then((isRestartRequired: boolean) => IntellisenseHelper.verifyInstallTypeScript(isRestartRequired)) |
bed90dbdJoshua Skelton10 years ago | 55 | .then((isRestartRequired: boolean) => IntellisenseHelper.configureWorkspaceSettings(isRestartRequired)) |
8a9925eeEric Hamilton10 years ago | 56 | .then((isRestartRequired: boolean) => IntellisenseHelper.warnIfRestartIsRequired(isRestartRequired)) |
b1df983eEric Hamilton10 years ago | 57 | .catch((err: any) => { |
| 58 | Log.logError("Error while setting up IntelliSense: " + err); | |
| 59 | return Q.reject<void>(err); | |
8a9925eeEric Hamilton10 years ago | 60 | }); |
10873e11digeff10 years ago | 61 | |
65fd8e85digeff10 years ago | 62 | /* TODO #83: Refactor this code to |
98bdec43digeff10 years ago | 63 | Q.all([enableSalsa(), installTypescript(), configureWorkspace()]) |
| 64 | .then((result) => warnIfRestartIsRequired(result.any((x) => x))) | |
| 65 | */ | |
| 66 | return Q.all([configureWorkspace, configureTypescript]).then(() => { }); | |
5e53b45aJoshua Skelton10 years ago | 67 | } |
| 68 | | |
| 69 | /** | |
| 70 | * Helper method that install typings for React Native. | |
| 71 | */ | |
cdbc4389Joshua Skelton10 years ago | 72 | public static installReactNativeTypings(): Q.Promise<void> { |
986196e7Patricio Beltran9 years ago | 73 | const typingsSource = path.resolve(__dirname, "..", "..", "ReactTypings"); |
| 74 | const reactTypings = path.resolve(typingsSource, "react"); | |
| 75 | const reactNativeTypings = path.resolve(typingsSource, "react-native"); | |
274b561cVladimir Kotikov9 years ago | 76 | const typingsIndex = path.resolve(typingsSource, "react-native.d.ts.index"); |
986196e7Patricio Beltran9 years ago | 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"); | |
274b561cVladimir Kotikov9 years ago | 81 | const typingsIndexDestination = path.resolve(vscode.workspace.rootPath, "typings"); |
| 82 | const typingIndexFinalPath = path.resolve(typingsIndexDestination, "react-native.d.ts"); | |
986196e7Patricio Beltran9 years ago | 83 | |
bfd09d23Joshua Skelton10 years ago | 84 | let fileSystem = new FileSystem(); |
| 85 | | |
81f88231Patricio Beltran9 years ago | 86 | const createTypingsDirectoryIfNeeded = fileSystem.directoryExists(typingsDestination). |
| 87 | then((exists) => { | |
| 88 | if (!exists) { | |
| 89 | return fileSystem.makeDirectoryRecursiveSync(typingsDestination); | |
| 90 | } | |
| 91 | }); | |
986196e7Patricio Beltran9 years ago | 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 | }); | |
274b561cVladimir Kotikov9 years ago | 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)) | |
ae278043Vladimir Kotikov9 years ago | 112 | .then((exists) => { |
| 113 | if (!exists) { | |
274b561cVladimir Kotikov9 years ago | 114 | return fileSystem.copyFile(typingsIndex, typingIndexFinalPath); |
ae278043Vladimir Kotikov9 years ago | 115 | } |
| 116 | }); | |
| 117 | | |
| 118 | return Q.all([ | |
| 119 | createTypingsDirectoryIfNeeded, | |
| 120 | copyReactTypingsIfNeeded, | |
| 121 | copyReactNativeTypingsIfNeeded, | |
| 122 | copyTypingsIndexIfNeeded, | |
| 123 | ]).then(() => { }); | |
bfd09d23Joshua Skelton10 years ago | 124 | } |
| 125 | | |
5e53b45aJoshua Skelton10 years ago | 126 | /** |
8a9925eeEric Hamilton10 years ago | 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. | |
5e53b45aJoshua Skelton10 years ago | 131 | */ |
8a9925eeEric Hamilton10 years ago | 132 | public static verifyInstallTypeScript(isRestartRequired: boolean): Q.Promise<boolean> { |
bed90dbdJoshua Skelton10 years ago | 133 | |
8a9925eeEric Hamilton10 years ago | 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() | |
9948de5dEric Hamilton10 years ago | 140 | .then(function(installProps: IInstallProps) { |
8a9925eeEric Hamilton10 years ago | 141 | |
b1df983eEric Hamilton10 years ago | 142 | if (installProps.installed === true) { |
8a9925eeEric Hamilton10 years ago | 143 | |
b1df983eEric Hamilton10 years ago | 144 | if (semver.neq(IntellisenseHelper.s_typeScriptVersion, installProps.version)) { |
8a9925eeEric Hamilton10 years ago | 145 | Log.logInternalMessage(LogLevel.Debug, "TypeScript is installed with the wrong version: " + installProps.version); |
b1df983eEric Hamilton10 years ago | 146 | return true; |
| 147 | } else { | |
| 148 | Log.logInternalMessage(LogLevel.Debug, "Installed TypeScript version is correct"); | |
| 149 | return false; | |
8a9925eeEric Hamilton10 years ago | 150 | } |
b1df983eEric Hamilton10 years ago | 151 | } else { |
| 152 | Log.logInternalMessage(LogLevel.Debug, "TypeScript is not installed"); | |
| 153 | return true; | |
bed90dbdJoshua Skelton10 years ago | 154 | } |
8a9925eeEric Hamilton10 years ago | 155 | }) |
b1df983eEric Hamilton10 years ago | 156 | .then((install: boolean) => { |
8a9925eeEric Hamilton10 years ago | 157 | |
b1df983eEric Hamilton10 years ago | 158 | if (install) { |
a1005420dlebu10 years ago | 159 | let installPath: string = path.resolve(HostPlatform.getUserHomePath(), ".vscode"); |
b1df983eEric Hamilton10 years ago | 160 | let runArguments: string[] = []; |
a1005420dlebu10 years ago | 161 | let npmCommand: string = HostPlatform.getNpmCliCommand("npm"); |
b1df983eEric Hamilton10 years ago | 162 | runArguments.push("install"); |
| 163 | runArguments.push("--prefix " + installPath); | |
| 164 | runArguments.push("typescript@" + IntellisenseHelper.s_typeScriptVersion); | |
| 165 | | |
323a3cc0Meena Kunnathur Balakrishnan10 years ago | 166 | return new CommandExecutor(installPath).spawn(npmCommand, runArguments) |
8a9925eeEric Hamilton10 years ago | 167 | .then(() => { |
b1df983eEric Hamilton10 years ago | 168 | return true; |
8a9925eeEric Hamilton10 years ago | 169 | }) |
b1df983eEric Hamilton10 years ago | 170 | .catch((err: any) => { |
8a9925eeEric Hamilton10 years ago | 171 | Log.logError("Error attempting to install TypeScript: " + err); |
b1df983eEric Hamilton10 years ago | 172 | return Q.reject<boolean>(err); |
8a9925eeEric Hamilton10 years ago | 173 | }); |
b1df983eEric Hamilton10 years ago | 174 | |
| 175 | } else { | |
| 176 | return isRestartRequired; | |
8a9925eeEric Hamilton10 years ago | 177 | } |
bed90dbdJoshua Skelton10 years ago | 178 | }); |
| 179 | } | |
| 180 | | |
| 181 | | |
| 182 | | |
9e81e40fPatricio Beltran10 years ago | 183 | public static configureWorkspaceSettings(isRestartRequired: boolean): boolean { |
8a9925eeEric Hamilton10 years ago | 184 | let typeScriptLibPath: string = path.resolve(IntellisenseHelper.getTypeScriptInstallPath(), "lib"); |
9e81e40fPatricio Beltran10 years ago | 185 | const tsdkPath = SettingsHelper.getTypeScriptTsdk(); |
bfd09d23Joshua Skelton10 years ago | 186 | |
9e81e40fPatricio Beltran10 years ago | 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; | |
bfd09d23Joshua Skelton10 years ago | 206 | } |
| 207 | | |
bed90dbdJoshua Skelton10 years ago | 208 | public static warnIfRestartIsRequired(isRestartRequired: boolean): Q.Promise<void> { |
| 209 | if (isRestartRequired) { | |
88e05f1bJoshua Skelton10 years ago | 210 | vscode.window.showInformationMessage("React Native intellisense was successfully configured for this project. Restart to enable it."); |
bed90dbdJoshua Skelton10 years ago | 211 | } |
| 212 | | |
| 213 | return; | |
| 214 | } | |
| 215 | | |
5e53b45aJoshua Skelton10 years ago | 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 | */ | |
9dc82fe8Joshua Skelton10 years ago | 220 | public static enableSalsa(isRestartRequired: boolean): Q.Promise<boolean> { |
e2216684Patricio Beltran10 years ago | 221 | if (!IntellisenseHelper.isSalsaSupported() && !process.env.VSCODE_TSJS) { |
9dc82fe8Joshua Skelton10 years ago | 222 | return Q({}) |
a1005420dlebu10 years ago | 223 | .then(() => HostPlatform.setEnvironmentVariable("VSCODE_TSJS", "1")) |
02cbb151Joshua Skelton10 years ago | 224 | .then(() => { return true; }); |
bfd09d23Joshua Skelton10 years ago | 225 | } |
9dc82fe8Joshua Skelton10 years ago | 226 | |
| 227 | return Q(isRestartRequired); | |
bfd09d23Joshua Skelton10 years ago | 228 | } |
8a9925eeEric Hamilton10 years ago | 229 | |
| 230 | /** | |
| 231 | * Simple check to see if the TypeScript package is in the expected location (where we installed it) | |
| 232 | */ | |
b1df983eEric Hamilton10 years ago | 233 | private static isTypeScriptInstalled(): Q.Promise<boolean> { |
8a9925eeEric Hamilton10 years ago | 234 | let fileSystem: FileSystem = new FileSystem(); |
| 235 | let installPath: string = path.join(IntellisenseHelper.getTypeScriptInstallPath(), "lib"); | |
b1df983eEric Hamilton10 years ago | 236 | return fileSystem.exists(installPath); |
8a9925eeEric Hamilton10 years ago | 237 | } |
| 238 | | |
| 239 | /** | |
| 240 | * Checks for the existance of our installed TypeScript package, if it exists also determine its version | |
| 241 | */ | |
9948de5dEric Hamilton10 years ago | 242 | private static getInstalledTypeScriptVersion(): Q.Promise<IInstallProps> { |
8a9925eeEric Hamilton10 years ago | 243 | return IntellisenseHelper.isTypeScriptInstalled() |
b1df983eEric Hamilton10 years ago | 244 | .then((installed: boolean) => { |
9948de5dEric Hamilton10 years ago | 245 | let installProps: IInstallProps = { |
b1df983eEric Hamilton10 years ago | 246 | installed: installed, |
cdf34447digeff10 years ago | 247 | version: "", |
b1df983eEric Hamilton10 years ago | 248 | }; |
8a9925eeEric Hamilton10 years ago | 249 | |
| 250 | if (installed === true) { | |
| 251 | Log.logInternalMessage(LogLevel.Debug, "TypeScript is installed - checking version"); | |
| 252 | return IntellisenseHelper.readPackageJson() | |
b1df983eEric Hamilton10 years ago | 253 | .then((version: string) => { |
| 254 | installProps.version = version; | |
| 255 | return installProps; | |
| 256 | }); | |
| 257 | } else { | |
| 258 | return installProps; | |
8a9925eeEric Hamilton10 years ago | 259 | } |
| 260 | }); | |
| 261 | } | |
| 262 | | |
| 263 | /** | |
| 264 | * Read the package.json from the TypeScript install path and return the version if it's available | |
| 265 | */ | |
b1df983eEric Hamilton10 years ago | 266 | private static readPackageJson(): Q.Promise<string> { |
8a9925eeEric Hamilton10 years ago | 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> { | |
b1df983eEric Hamilton10 years ago | 279 | let data = JSON.parse(jsonContents); |
8a9925eeEric Hamilton10 years ago | 280 | return data.version; |
| 281 | }) | |
b1df983eEric Hamilton10 years ago | 282 | .catch((err: any) => { |
ccce3f36Joshua Skelton10 years ago | 283 | Log.logError("Error while processing package.json: " + err); |
b1df983eEric Hamilton10 years ago | 284 | return "0.0.0"; |
8a9925eeEric Hamilton10 years ago | 285 | }); |
| 286 | } | |
| 287 | | |
| 288 | /** | |
| 289 | * Simple helper to get the TypeScript install path | |
| 290 | */ | |
b1df983eEric Hamilton10 years ago | 291 | private static getTypeScriptInstallPath(): string { |
8a9925eeEric Hamilton10 years ago | 292 | |
a1005420dlebu10 years ago | 293 | let codePath: string = path.resolve(HostPlatform.getUserHomePath(), ".vscode"); |
8a9925eeEric Hamilton10 years ago | 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 | } | |
f16656e9frogcjn10 years ago | 304 | } |