microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/openFileAtLocation.ts
79lines · modeblame
514df4f4Patricio Beltran10 years ago | 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"; | |
d7d405aeYuri Skorokhodov7 years ago | 10 | import * as nls from "vscode-nls"; |
| 11 | const localize = nls.loadMessageBundle(); | |
514df4f4Patricio Beltran10 years ago | 12 | |
| 13 | /* Usage: | |
| 14 | ...path\openFileAtLocation.js filename:lineNumber | |
| 15 | ...path\openFileAtLocation.js filename | |
0fc1f1deJimmy Thomson9 years ago | 16 | ...path\openFileAtLocation.js workspace filename:lineNumber |
| 17 | ...path\openFileAtLocation.js workspace filename | |
514df4f4Patricio Beltran10 years ago | 18 | */ |
| 19 | | |
92ca3e65Jimmy Thomson9 years ago | 20 | { |
| 21 | if (process.argv.length < 3) { | |
d7d405aeYuri Skorokhodov7 years ago | 22 | throw localize("WrongNumberOfParametersProvidedReferToTheUsageOfThisScript", "Wrong number of parameters provided. Please refer to the usage of this script for proper use."); |
92ca3e65Jimmy Thomson9 years ago | 23 | } |
514df4f4Patricio Beltran10 years ago | 24 | |
92ca3e65Jimmy Thomson9 years ago | 25 | let fullpath: string; |
| 26 | let workspace: string; | |
0fc1f1deJimmy Thomson9 years ago | 27 | |
92ca3e65Jimmy Thomson9 years ago | 28 | if (process.argv.length === 3) { |
| 29 | fullpath = process.argv[2]; | |
5c8365a6Artem Egorov8 years ago | 30 | workspace = ""; |
92ca3e65Jimmy Thomson9 years ago | 31 | } else { |
| 32 | fullpath = process.argv[3]; | |
| 33 | workspace = process.argv[2]; | |
| 34 | } | |
0fc1f1deJimmy Thomson9 years ago | 35 | |
92ca3e65Jimmy Thomson9 years ago | 36 | const dirname = path.normalize(path.dirname(fullpath)); |
514df4f4Patricio Beltran10 years ago | 37 | |
92ca3e65Jimmy Thomson9 years ago | 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; | |
514df4f4Patricio Beltran10 years ago | 45 | |
92ca3e65Jimmy Thomson9 years ago | 46 | if (fileInfo.length >= 2) { |
| 47 | lineNumber = parseInt(fileInfo[1], 10); | |
| 48 | } | |
514df4f4Patricio Beltran10 years ago | 49 | |
92ca3e65Jimmy Thomson9 years ago | 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 | } | |
514df4f4Patricio Beltran10 years ago | 58 | |
92ca3e65Jimmy Thomson9 years ago | 59 | function getReactNativeWorkspaceForFile(file: string, workspace: string): Q.Promise<string> { |
0fc1f1deJimmy Thomson9 years ago | 60 | if (workspace) { |
| 61 | return Q(workspace); | |
| 62 | } | |
514df4f4Patricio Beltran10 years ago | 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> { | |
2e432a9eArtem Egorov8 years ago | 70 | return ReactNativeProjectHelper.isReactNativeProject(dir).then(isRNProject => { |
514df4f4Patricio Beltran10 years ago | 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 | } |