microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3d2b4b710b80ed7f71637750bf6b62dd4445a51d

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/packageJsonWatcher.ts

67lines · 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 * as path from "path";
5import * as vscode from "vscode";
6
7import {TsConfigHelper} from "./tsconfigHelper";
8import {TsdHelper} from "./tsdHelper";
9import {FileSystem} from "./node/fileSystem";
10import {Package} from "./node/package";
11
12export class PackageJsonWatcher {
13 private fileSystemWatcher: vscode.FileSystemWatcher;
14 constructor() {
15 this.fileSystemWatcher = vscode.workspace.createFileSystemWatcher("package.json");
16 }
17
18 public startWatching(): void {
19 this.fileSystemWatcher.onDidChange((changeEvent: vscode.Uri) => this.configureReactNativeWorkspace());
20 this.fileSystemWatcher.onDidCreate((changeEvent: vscode.Uri) => this.configureReactNativeWorkspace());
21 this.configureReactNativeWorkspace();
22 }
23
24 private dropDebuggerStub(): void {
25 let launcherPath = require.resolve("../debugger/launcher");
26 const extensionVersionNumber = require("../../package.json").version;
27 let debuggerEntryCode =
28 `// This file is automatically generated. version:${extensionVersionNumber}
29try {
30 var path = require("path");
31 var Launcher = require(${JSON.stringify(launcherPath)}).Launcher;
32 new Launcher(path.resolve(__dirname, "..")).launch();
33} catch (e) {
34 throw new Error("Unable to launch application. Try deleting .vscode/launchReactNative.js and restarting vscode.");
35}`;
36 let vscodeFolder = path.join(vscode.workspace.rootPath, ".vscode");
37 let debugStub = path.join(vscodeFolder, "launchReactNative.js");
38
39 let fsUtil = new FileSystem();
40
41 fsUtil.ensureDirectory(vscodeFolder).then(() => {
42 fsUtil.ensureFileWithContents(debugStub, debuggerEntryCode);
43 }).catch((err: Error) => {
44 vscode.window.showErrorMessage(err.message);
45 });
46 }
47
48 private configureReactNativeWorkspace(): void {
49 let currentPackage = new Package(vscode.workspace.rootPath);
50 currentPackage.dependencies().then(dependencies => {
51 if (dependencies && dependencies["react-native"]) {
52 // Looks like a react native project: Set it up for debugging
53 this.dropDebuggerStub();
54
55 // Enable JavaScript intellisense through Salsa language service
56 TsConfigHelper.compileJavaScript(true).done();
57
58 // Add typings for React and React Native
59 var reactTypeDefsPath = path.resolve(__dirname, "..", "..", "reactTypings.json");
60 var typeDefsToInstall:string[] = require(reactTypeDefsPath);
61 TsdHelper.installTypings(TsdHelper.getOrCreateTypingsTargetPath(vscode.workspace.rootPath),typeDefsToInstall).done();
62 }
63 }).catch(() => { });
64 // If the readFile fails, or the JSON.parse fails, then we ignore the exception
65 // and assume this is not a react-native project.
66 }
67}