microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
bedf110fbb4cf82fb995f4cf2770e8339d5adbea

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/packageJsonWatcher.ts

60lines · 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 {FileSystem} from "../utils/node/fileSystem";
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 launcherPath = require.resolve("../debugger/launcher");
25 const extensionVersionNumber = require("../../package.json").version;
26 let debuggerEntryCode =
27`// This file is automatically generated. version:${extensionVersionNumber}
28try {
29 var path = require("path");
30 var Launcher = require(${JSON.stringify(launcherPath)}).Launcher;
31 new Launcher(path.resolve(__dirname, "..")).launch();
32} catch (e) {
33 throw new Error("Unable to launch application. Try deleting .vscode/launchReactNative.js and restarting vscode.");
34}`;
35 let vscodeFolder = path.join(vscode.workspace.rootPath, ".vscode");
36 let debugStub = path.join(vscodeFolder, "launchReactNative.js");
37
38 let fsUtil = new FileSystem();
39
40 fsUtil.ensureDirectory(vscodeFolder).then(() => {
41 fsUtil.ensureFileWithContents(debugStub, debuggerEntryCode);
42 }).catch((err: Error) => {
43 vscode.window.showErrorMessage(err.message);
44 });
45 }
46
47 private configureReactNativeWorkspace(): void {
48 let packageJsonPath = path.join(vscode.workspace.rootPath, "package.json");
49 Q.nfcall(fs.readFile, packageJsonPath, "utf-8").then((contents: string) => {
50 let packageJsonContents = JSON.parse(contents);
51 if (packageJsonContents && packageJsonContents.dependencies
52 && "react-native" in packageJsonContents.dependencies) {
53 // Looks like a react native project: Set it up for debugging
54 this.dropDebuggerStub();
55 }
56 }).catch(() => {});
57 // If the readFile fails, or the JSON.parse fails, then we ignore the exception
58 // and assume this is not a react-native project.
59 }
60}