microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/openFileAtLocation.ts
61lines · 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 {RemoteExtension} from "../common/remoteExtension"; |
| 5 | import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper"; |
| 6 | import {InternalErrorCode} from "../common/error/internalErrorCode"; |
| 7 | import {ErrorHelper} from "../common/error/errorHelper"; |
| 8 | import * as path from "path"; |
| 9 | import * as Q from "q"; |
| 10 | |
| 11 | /* Usage: |
| 12 | ...path\openFileAtLocation.js filename:lineNumber |
| 13 | ...path\openFileAtLocation.js filename |
| 14 | */ |
| 15 | |
| 16 | if (process.argv.length < 3) { |
| 17 | throw "Wrong number of parameters provided. Please refer to the usage of this script for proper use."; |
| 18 | } |
| 19 | |
| 20 | const fullpath = process.argv[2]; |
| 21 | const dirname = path.normalize(path.dirname(fullpath)); |
| 22 | |
| 23 | // In Windows this should make sure c:\ is always lowercase and in |
| 24 | // Unix '/'.toLowerCase() = '/' |
| 25 | const normalizedDirname = dirname.toLowerCase(); |
| 26 | const filenameAndNumber = path.basename(fullpath); |
| 27 | const fileInfo = filenameAndNumber.split(":"); |
| 28 | const filename = path.join(normalizedDirname, fileInfo[0]); |
| 29 | let lineNumber: number = 1; |
| 30 | |
| 31 | if (fileInfo.length >= 2) { |
| 32 | lineNumber = parseInt(fileInfo[1], 10); |
| 33 | } |
| 34 | |
| 35 | getReactNativeWorkspaceForFile(filename).then(projectRootPath => { |
| 36 | const remoteExtension = RemoteExtension.atProjectRootPath(projectRootPath); |
| 37 | return remoteExtension.openFileAtLocation(filename, lineNumber); |
| 38 | }).done(() => { }, (reason) => { |
| 39 | throw ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, |
| 40 | "Unable to communicate with VSCode. Please make sure it is open in the appropriate workspace."); |
| 41 | }); |
| 42 | |
| 43 | function getReactNativeWorkspaceForFile(file: string): Q.Promise<string> { |
| 44 | return getPathForRNParentWorkspace(path.dirname(file)) |
| 45 | .catch((reason) => { |
| 46 | return Q.reject<string>(ErrorHelper.getNestedError(reason, InternalErrorCode.WorkspaceNotFound, `Error while looking at workspace for file: ${file}.`)); |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | function getPathForRNParentWorkspace(dir: string): Q.Promise<string> { |
| 51 | const reactNativeProjectHelper = new ReactNativeProjectHelper(dir); |
| 52 | return reactNativeProjectHelper.isReactNativeProject().then(isRNProject => { |
| 53 | if (isRNProject) { |
| 54 | return dir; |
| 55 | } |
| 56 | if (dir === "" || dir === "." || dir === "/" || dir === path.dirname(dir)) { |
| 57 | return Q.reject<string>(ErrorHelper.getInternalError(InternalErrorCode.WorkspaceNotFound, "React Native project workspace not found.")); |
| 58 | } |
| 59 | return getPathForRNParentWorkspace(path.dirname(dir)); |
| 60 | }); |
| 61 | } |