microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c5d308e47ff644d5ded8339fff0d80dc013275a8

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/intellisenseHelper.ts

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