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