microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
81f88231b0c98e7265fe93cd7fb01b25ee66f67f

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

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
4import {RemoteExtension} from "../common/remoteExtension";
5import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper";
6import {InternalErrorCode} from "../common/error/internalErrorCode";
7import {ErrorHelper} from "../common/error/errorHelper";
8import * as path from "path";
9import * as Q from "q";
10
11/* Usage:
12...path\openFileAtLocation.js filename:lineNumber
13...path\openFileAtLocation.js filename
14*/
15
16if (process.argv.length < 3) {
17 throw "Wrong number of parameters provided. Please refer to the usage of this script for proper use.";
18}
19
20const fullpath = process.argv[2];
21const dirname = path.normalize(path.dirname(fullpath));
22
23// In Windows this should make sure c:\ is always lowercase and in
24// Unix '/'.toLowerCase() = '/'
25const normalizedDirname = dirname.toLowerCase();
26const filenameAndNumber = path.basename(fullpath);
27const fileInfo = filenameAndNumber.split(":");
28const filename = path.join(normalizedDirname, fileInfo[0]);
29let lineNumber: number = 1;
30
31if (fileInfo.length >= 2) {
32 lineNumber = parseInt(fileInfo[1], 10);
33}
34
35getReactNativeWorkspaceForFile(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
43function 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
50function 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}