microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.14

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/openFileAtLocation.ts

77lines · modeblame

514df4f4Patricio Beltran10 years ago1// 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
0fc1f1deJimmy Thomson9 years ago14...path\openFileAtLocation.js workspace filename:lineNumber
15...path\openFileAtLocation.js workspace filename
514df4f4Patricio Beltran10 years ago16*/
17
92ca3e65Jimmy Thomson9 years ago18{
19if (process.argv.length < 3) {
20throw "Wrong number of parameters provided. Please refer to the usage of this script for proper use.";
21}
514df4f4Patricio Beltran10 years ago22
92ca3e65Jimmy Thomson9 years ago23let fullpath: string;
24let workspace: string;
0fc1f1deJimmy Thomson9 years ago25
92ca3e65Jimmy Thomson9 years ago26if (process.argv.length === 3) {
27fullpath = process.argv[2];
5c8365a6Artem Egorov8 years ago28workspace = "";
92ca3e65Jimmy Thomson9 years ago29} else {
30fullpath = process.argv[3];
31workspace = process.argv[2];
32}
0fc1f1deJimmy Thomson9 years ago33
92ca3e65Jimmy Thomson9 years ago34const dirname = path.normalize(path.dirname(fullpath));
514df4f4Patricio Beltran10 years ago35
92ca3e65Jimmy Thomson9 years ago36// In Windows this should make sure c:\ is always lowercase and in
37// Unix '/'.toLowerCase() = '/'
38const normalizedDirname = dirname.toLowerCase();
39const filenameAndNumber = path.basename(fullpath);
40const fileInfo = filenameAndNumber.split(":");
41const filename = path.join(normalizedDirname, fileInfo[0]);
42let lineNumber: number = 1;
514df4f4Patricio Beltran10 years ago43
92ca3e65Jimmy Thomson9 years ago44if (fileInfo.length >= 2) {
45lineNumber = parseInt(fileInfo[1], 10);
46}
514df4f4Patricio Beltran10 years ago47
92ca3e65Jimmy Thomson9 years ago48getReactNativeWorkspaceForFile(filename, workspace).then(projectRootPath => {
49const remoteExtension = RemoteExtension.atProjectRootPath(projectRootPath);
50return remoteExtension.openFileAtLocation(filename, lineNumber);
51}).done(() => { }, (reason) => {
52throw ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed,
53"Unable to communicate with VSCode. Please make sure it is open in the appropriate workspace.");
54});
55}
514df4f4Patricio Beltran10 years ago56
92ca3e65Jimmy Thomson9 years ago57function getReactNativeWorkspaceForFile(file: string, workspace: string): Q.Promise<string> {
0fc1f1deJimmy Thomson9 years ago58if (workspace) {
59return Q(workspace);
60}
514df4f4Patricio Beltran10 years ago61return getPathForRNParentWorkspace(path.dirname(file))
62.catch((reason) => {
63return 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> {
2e432a9eArtem Egorov8 years ago68return ReactNativeProjectHelper.isReactNativeProject(dir).then(isRNProject => {
514df4f4Patricio Beltran10 years ago69if (isRNProject) {
70return dir;
71}
72if (dir === "" || dir === "." || dir === "/" || dir === path.dirname(dir)) {
73return Q.reject<string>(ErrorHelper.getInternalError(InternalErrorCode.WorkspaceNotFound, "React Native project workspace not found."));
74}
75return getPathForRNParentWorkspace(path.dirname(dir));
76});
77}