microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/rn-extension.ts
82lines · 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 | import * as fs from "fs"; |
| 4 | import * as path from "path"; |
| 5 | import * as Q from "q"; |
| 6 | import * as vscode from "vscode"; |
| 7 | |
| 8 | import {ReactNativeCommandHelper} from "./utils/reactNativeCommandHelper"; |
| 9 | |
| 10 | function dropDebuggerStub(): void { |
| 11 | let debuggerEntryPath = require.resolve("./debugger/reactNative/reactNative"); |
| 12 | // TODO: Update this stub to point to correct file/class once it is in |
| 13 | let debuggerEntryCode = `var RN = require(${JSON.stringify(debuggerEntryPath)}).ReactNative;\nnew RN.Launcher(${JSON.stringify(vscode.workspace.rootPath)}).launch()`; |
| 14 | let vscodeFolder = path.join(vscode.workspace.rootPath, ".vscode"); |
| 15 | let debugStub = path.join(vscodeFolder, "launchReactNative.js"); |
| 16 | |
| 17 | Q.nfcall(fs.stat, vscodeFolder).then((stat: fs.Stats) => { |
| 18 | if (stat && !stat.isDirectory()) { |
| 19 | // .vscode exists but is not a folder: bail out |
| 20 | throw new Error("Warning: Expected .vscode to be a folder. Debugging requires manual intervention."); |
| 21 | } |
| 22 | }, (err: Error & {code: string}) => { |
| 23 | if (err && err.code === "ENOENT") { |
| 24 | // No .vscode folder: create one |
| 25 | fs.mkdirSync(vscodeFolder); |
| 26 | } else { |
| 27 | throw err; |
| 28 | } |
| 29 | }).then(() => { |
| 30 | // At this point, .vscode folder exists and is a folder |
| 31 | return Q.nfcall(fs.stat, debugStub).then((stat: fs.Stats) => { |
| 32 | if (!stat.isFile()) { |
| 33 | throw Error("Error: Expected .vscode/launchReactNative.js to be a file"); |
| 34 | } |
| 35 | // File exists: lets leave it there and assume it was created by us |
| 36 | }, (err: Error & {code: string}) => { |
| 37 | if (err && err.code === "ENOENT") { |
| 38 | fs.writeFileSync(debugStub, debuggerEntryCode); |
| 39 | } else { |
| 40 | throw err; |
| 41 | } |
| 42 | }); |
| 43 | }).catch((err: Error) => { |
| 44 | vscode.window.showErrorMessage(err.message); |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | function configureReactNativeWorkspace(): void { |
| 49 | try { |
| 50 | let packageJsonPath = path.join(vscode.workspace.rootPath, "package.json"); |
| 51 | let packageJsonContents = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")); |
| 52 | if (packageJsonContents && packageJsonContents.dependencies |
| 53 | && "react-native" in packageJsonContents.dependencies) { |
| 54 | // Looks like a react native project: Set it up for debugging |
| 55 | dropDebuggerStub(); |
| 56 | } |
| 57 | } catch (e) { |
| 58 | // Project was malformed or not a react native project: do nothing. |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | export function activate(context: vscode.ExtensionContext): void { |
| 63 | // TODO: Get the project root (vscode.workspace.rootPath) and return if it is not a react-native project |
| 64 | // check if package.json of user project has dependency on react-native |
| 65 | |
| 66 | // NOTE: This triggers when other files in .vscode are modified as well, e.g. launch.json |
| 67 | // We may need to be more specific in when we drop the debug file |
| 68 | vscode.workspace.onDidChangeConfiguration(configureReactNativeWorkspace); |
| 69 | configureReactNativeWorkspace(); |
| 70 | |
| 71 | // TODO: Change to a foreach if this implementation is appropriate |
| 72 | // Register react native commands |
| 73 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.runAndroid", |
| 74 | () => ReactNativeCommandHelper.executeReactNativeCommand(vscode.workspace.rootPath, "runAndroid"))); |
| 75 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.runIos", |
| 76 | () => ReactNativeCommandHelper.executeReactNativeCommand(vscode.workspace.rootPath, "runIos"))); |
| 77 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.startPackager", |
| 78 | () => ReactNativeCommandHelper.executeReactNativeCommand(vscode.workspace.rootPath, "startPackager"))); |
| 79 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.stopPackager", |
| 80 | () => ReactNativeCommandHelper.executeReactNativeCommand(vscode.workspace.rootPath, "stopPackager"))); |
| 81 | |
| 82 | } |
| 83 | |