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