microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
51769beae612e2c4f64230f610cc350c83156e88

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/packageJsonWatcher.ts

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