microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
eba0de58017cd198f69bffca9be3d8e0e4df4d6a

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/openFileAtLocation.ts

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