microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
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 | |
| 4 | import * as path from "path"; |
| 5 | import * as vscode from "vscode"; |
| 6 | import {Package} from "./node/package"; |
| 7 | import {FileSystem} from "./node/fileSystem"; |
| 8 | |
| 9 | export 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} |
| 26 | try { |
| 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 | } |