microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
bc7a32ce7b23591909f256ba627ef8e1fb8116a2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/packageJsonWatcher.ts

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