microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
eb7292d2e5164d94d10fa894e858e1bd406417a4

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/packageJsonWatcher.ts

61lines · 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 fs from "fs";
5import * as path from "path";
6import * as Q from "q";
7import * as vscode from "vscode";
8
9import {FileSystemUtil} from "./fileSystemUtil";
10
11export class PackageJsonWatcher {
12 private filesystemWatcher: vscode.FileSystemWatcher;
13 constructor() {
14 this.filesystemWatcher = vscode.workspace.createFileSystemWatcher("package.json");
15 }
16
17 public startWatching(): void {
18 this.filesystemWatcher.onDidChange((changeEvent: vscode.Uri) => this.configureReactNativeWorkspace());
19 this.filesystemWatcher.onDidCreate((changeEvent: vscode.Uri) => this.configureReactNativeWorkspace());
20 this.configureReactNativeWorkspace();
21 }
22
23 private dropDebuggerStub(): void {
24 let debuggerEntryPath = require.resolve("../debugger/debuggerEntry");
25 // TODO: Update this stub to point to correct file/class once it is in
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 RN = require(${JSON.stringify(debuggerEntryPath)}).ReactNative;
32 new RN.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 FileSystemUtil();
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 packageJsonPath = path.join(vscode.workspace.rootPath, "package.json");
50 Q.nfcall(fs.readFile, packageJsonPath, "utf-8").then((contents: string) => {
51 let packageJsonContents = JSON.parse(contents);
52 if (packageJsonContents && packageJsonContents.dependencies
53 && "react-native" in packageJsonContents.dependencies) {
54 // Looks like a react native project: Set it up for debugging
55 this.dropDebuggerStub();
56 }
57 }).catch(() => {});
58 // If the readFile fails, or the JSON.parse fails, then we ignore the exception
59 // and assume this is not a react-native project.
60 }
61}