microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/utils/packageJsonWatcher.ts
67lines · 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 | |
| 7 | import {TsConfigHelper} from "./tsconfigHelper"; |
| 8 | import {TsdHelper} from "./tsdHelper"; |
| 9 | import {FileSystem} from "./node/fileSystem"; |
| 10 | import {Package} from "./node/package"; |
| 11 | |
| 12 | export class PackageJsonWatcher { |
| 13 | private fileSystemWatcher: vscode.FileSystemWatcher; |
| 14 | constructor() { |
| 15 | this.fileSystemWatcher = vscode.workspace.createFileSystemWatcher("package.json"); |
| 16 | } |
| 17 | |
| 18 | public startWatching(): void { |
| 19 | this.fileSystemWatcher.onDidChange((changeEvent: vscode.Uri) => this.configureReactNativeWorkspace()); |
| 20 | this.fileSystemWatcher.onDidCreate((changeEvent: vscode.Uri) => this.configureReactNativeWorkspace()); |
| 21 | this.configureReactNativeWorkspace(); |
| 22 | } |
| 23 | |
| 24 | private dropDebuggerStub(): void { |
| 25 | let launcherPath = require.resolve("../debugger/launcher"); |
| 26 | const extensionVersionNumber = require("../../package.json").version; |
| 27 | let debuggerEntryCode = |
| 28 | `// This file is automatically generated. version:${extensionVersionNumber} |
| 29 | try { |
| 30 | var path = require("path"); |
| 31 | var Launcher = require(${JSON.stringify(launcherPath)}).Launcher; |
| 32 | new 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 FileSystem(); |
| 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 currentPackage = new Package(vscode.workspace.rootPath); |
| 50 | currentPackage.dependencies().then(dependencies => { |
| 51 | if (dependencies && dependencies["react-native"]) { |
| 52 | // Looks like a react native project: Set it up for debugging |
| 53 | this.dropDebuggerStub(); |
| 54 | |
| 55 | // Enable JavaScript intellisense through Salsa language service |
| 56 | TsConfigHelper.compileJavaScript(true).done(); |
| 57 | |
| 58 | // Add typings for React and React Native |
| 59 | var reactTypeDefsPath = path.resolve(__dirname, "..", "..", "reactTypings.json"); |
| 60 | var typeDefsToInstall:string[] = require(reactTypeDefsPath); |
| 61 | TsdHelper.installTypings(TsdHelper.getOrCreateTypingsTargetPath(vscode.workspace.rootPath),typeDefsToInstall).done(); |
| 62 | } |
| 63 | }).catch(() => { }); |
| 64 | // If the readFile fails, or the JSON.parse fails, then we ignore the exception |
| 65 | // and assume this is not a react-native project. |
| 66 | } |
| 67 | } |