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