microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/openFileAtLocation.ts

79lines · 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";
d7d405aeYuri Skorokhodov7 years ago10import * as nls from "vscode-nls";
11const localize = nls.loadMessageBundle();
514df4f4Patricio Beltran10 years ago12
13/* Usage:
14...path\openFileAtLocation.js filename:lineNumber
15...path\openFileAtLocation.js filename
0fc1f1deJimmy Thomson9 years ago16...path\openFileAtLocation.js workspace filename:lineNumber
17...path\openFileAtLocation.js workspace filename
514df4f4Patricio Beltran10 years ago18*/
19
92ca3e65Jimmy Thomson9 years ago20{
21if (process.argv.length < 3) {
d7d405aeYuri Skorokhodov7 years ago22throw localize("WrongNumberOfParametersProvidedReferToTheUsageOfThisScript", "Wrong number of parameters provided. Please refer to the usage of this script for proper use.");
92ca3e65Jimmy Thomson9 years ago23}
514df4f4Patricio Beltran10 years ago24
92ca3e65Jimmy Thomson9 years ago25let fullpath: string;
26let workspace: string;
0fc1f1deJimmy Thomson9 years ago27
92ca3e65Jimmy Thomson9 years ago28if (process.argv.length === 3) {
29fullpath = process.argv[2];
5c8365a6Artem Egorov8 years ago30workspace = "";
92ca3e65Jimmy Thomson9 years ago31} else {
32fullpath = process.argv[3];
33workspace = process.argv[2];
34}
0fc1f1deJimmy Thomson9 years ago35
92ca3e65Jimmy Thomson9 years ago36const dirname = path.normalize(path.dirname(fullpath));
514df4f4Patricio Beltran10 years ago37
92ca3e65Jimmy Thomson9 years ago38// In Windows this should make sure c:\ is always lowercase and in
39// Unix '/'.toLowerCase() = '/'
40const normalizedDirname = dirname.toLowerCase();
41const filenameAndNumber = path.basename(fullpath);
42const fileInfo = filenameAndNumber.split(":");
43const filename = path.join(normalizedDirname, fileInfo[0]);
44let lineNumber: number = 1;
514df4f4Patricio Beltran10 years ago45
92ca3e65Jimmy Thomson9 years ago46if (fileInfo.length >= 2) {
47lineNumber = parseInt(fileInfo[1], 10);
48}
514df4f4Patricio Beltran10 years ago49
92ca3e65Jimmy Thomson9 years ago50getReactNativeWorkspaceForFile(filename, workspace).then(projectRootPath => {
51const remoteExtension = RemoteExtension.atProjectRootPath(projectRootPath);
52return remoteExtension.openFileAtLocation(filename, lineNumber);
53}).done(() => { }, (reason) => {
54throw 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 ago58
92ca3e65Jimmy Thomson9 years ago59function getReactNativeWorkspaceForFile(file: string, workspace: string): Q.Promise<string> {
0fc1f1deJimmy Thomson9 years ago60if (workspace) {
61return Q(workspace);
62}
514df4f4Patricio Beltran10 years ago63return getPathForRNParentWorkspace(path.dirname(file))
64.catch((reason) => {
65return Q.reject<string>(ErrorHelper.getNestedError(reason, InternalErrorCode.WorkspaceNotFound, `Error while looking at workspace for file: ${file}.`));
66});
67}
68
69function getPathForRNParentWorkspace(dir: string): Q.Promise<string> {
2e432a9eArtem Egorov8 years ago70return ReactNativeProjectHelper.isReactNativeProject(dir).then(isRNProject => {
514df4f4Patricio Beltran10 years ago71if (isRNProject) {
72return dir;
73}
74if (dir === "" || dir === "." || dir === "/" || dir === path.dirname(dir)) {
75return Q.reject<string>(ErrorHelper.getInternalError(InternalErrorCode.WorkspaceNotFound, "React Native project workspace not found."));
76}
77return getPathForRNParentWorkspace(path.dirname(dir));
78});
79}