microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
771dc596c273a8374ef21888eebdcb2fa3b4e887

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/intellisenseHelper.ts

125lines · 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 child_process from "child_process";
6import * as os from "os";
7import * as path from "path";
8import * as Q from "q";
9import * as vscode from "vscode";
10import {Telemetry} from "../common/telemetry";
11import {TelemetryHelper} from "../common/telemetryHelper";
12import {TsConfigHelper} from "./tsconfigHelper";
13import {SettingsHelper} from "./settingsHelper";
14
15export class IntellisenseHelper {
16 /**
17 * Helper method that configures the workspace for Salsa intellisense.
18 */
19 public static setupReactNativeIntellisense(): void {
20 // Telemetry - Send Salsa Environment setup information
21 let tsSalsaEnvSetup = TelemetryHelper.createTelemetryEvent("RNIntellisense");
22 TelemetryHelper.addTelemetryEventProperty(tsSalsaEnvSetup, "TsSalsaEnvSetup", !!process.env.VSCODE_TSJS, false);
23 Telemetry.send(tsSalsaEnvSetup);
24
25 Q({})
26 .then(() => TsConfigHelper.allowJs(true))
27 .then(() => TsConfigHelper.addExcludePaths(["node_modules"]))
28 .then(() => IntellisenseHelper.installReactNativeTypings())
29 .done();
30
31 // The actions taken in the promise chain below may result in requring a restart.
32 Q(false)
33 .then((isRestartRequired: boolean) => IntellisenseHelper.enableSalsa(isRestartRequired))
34 .then((isRestartRequired: boolean) => IntellisenseHelper.installTypescriptNext(isRestartRequired))
35 .then((isRestartRequired: boolean) => IntellisenseHelper.configureWorkspaceSettings(isRestartRequired))
36 .then((isRestartRequired: boolean) => IntellisenseHelper.warnIfRestartIsRequired(isRestartRequired))
37 .done();
38 }
39
40 /**
41 * Helper method that install typings for React Native.
42 */
43 public static installReactNativeTypings(): Q.Promise<void> {
44 let reactTypingsSource = path.resolve(__dirname, "..", "..", "ReactTypings");
45 let reactTypingsDest = path.resolve(vscode.workspace.rootPath, ".vscode", "typings");
46 let fileSystem = new FileSystem();
47
48 return fileSystem.copyRecursive(reactTypingsSource, reactTypingsDest);
49 }
50
51 /**
52 * Helper method that installs Typescript into a global location.
53 */
54 public static installTypescriptNext(isRestartRequired: boolean): Q.Promise<boolean> {
55 let typeScriptNextDest: string = path.resolve(IntellisenseHelper.getUserHomePath(), ".vscode");
56 let typeScriptNextLibPath: string = path.join(typeScriptNextDest, "node_modules", "typescript", "lib");
57 let fileSystem: FileSystem = new FileSystem();
58
59 return fileSystem.exists(typeScriptNextLibPath)
60 .then(function(exists: boolean) {
61 if (!exists) {
62 return Q.nfcall(child_process.exec, `npm install --prefix ${typeScriptNextDest} typescript@next`)
63 .then(() => { return true; });
64 }
65
66 return isRestartRequired;
67 });
68 }
69
70 public static getUserHomePath(): string {
71 let homeDirectory: string = "";
72
73 if (os.type() === "Darwin") {
74 homeDirectory = process.env.HOME;
75 } else if (os.type() === "Windows_NT") {
76 homeDirectory = process.env.USERPROFILE;
77 }
78
79 return homeDirectory;
80 }
81
82 public static configureWorkspaceSettings(isRestartRequired: boolean): Q.Promise<boolean> {
83 let typeScriptNextDest: string = path.resolve(IntellisenseHelper.getUserHomePath(), ".vscode");
84 let typeScriptNextLibPath: string = path.join(typeScriptNextDest, "node_modules", "typescript", "lib");
85
86 return SettingsHelper.getTypescriptTsdk()
87 .then((tsdkPath: string) => {
88 if (!tsdkPath) {
89 return SettingsHelper.typescriptTsdk(typeScriptNextLibPath)
90 .then(() => { return true; });
91 }
92
93 return isRestartRequired;
94 });
95 }
96
97 public static warnIfRestartIsRequired(isRestartRequired: boolean): Q.Promise<void> {
98 if (isRestartRequired) {
99 vscode.window.showInformationMessage("React Native intellisense was successfully configured for this project. Restart to enable it.");
100 }
101
102 return;
103 }
104
105 /**
106 * Helper method that sets the environment variable and informs the user they need to restart
107 * in order to enable the Salsa intellisense.
108 */
109 public static enableSalsa(isRestartRequired: boolean): Q.Promise<boolean> {
110 if (!process.env.VSCODE_TSJS) {
111 let setEnvironmentVariableCommand: string = "";
112 if (os.type() === "Darwin") {
113 setEnvironmentVariableCommand = "launchctl setenv VSCODE_TSJS 1";
114 } else if (os.type() === "Windows") {
115 setEnvironmentVariableCommand = "setx VSCODE_TSJS 1";
116 }
117
118 return Q({})
119 .then(() => Q.nfcall(child_process.exec, setEnvironmentVariableCommand))
120 .then(() => { return true; });
121 }
122
123 return Q(isRestartRequired);
124 }
125}