microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.4.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/extensionHelper.ts

52lines · 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 * as path from "path";
5import * as fs from "fs";
6
7export function getExtensionVersion(): string | null {
8 const packageJsonPath = findFileInFolderHierarchy(__dirname, "package.json");
9 if (packageJsonPath) {
10 return JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")).version;
11 } else {
12 return null;
13 }
14}
15
16export function getExtensionName(): string | null {
17 const packageJsonPath = findFileInFolderHierarchy(__dirname, "package.json");
18 if (packageJsonPath) {
19 return JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")).name;
20 } else {
21 return null;
22 }
23}
24
25export function findFileInFolderHierarchy(dir: string, filename: string): string | null {
26 let parentPath: string;
27 let projectRoot: string = dir;
28 let atFsRoot: boolean = false;
29
30 while (!fs.existsSync(path.join(projectRoot, filename))) {
31 // Navigate up one level until either config.xml is found
32 parentPath = path.resolve(projectRoot, "..");
33 if (parentPath !== projectRoot) {
34 projectRoot = parentPath;
35 } else {
36 // we have reached the filesystem root
37 atFsRoot = true;
38 break;
39 }
40 }
41
42 if (atFsRoot) {
43 // We reached the fs root
44 return null;
45 }
46
47 return path.join(projectRoot, filename);
48}
49
50export function generateRandomPortNumber(): number {
51 return Math.round(Math.random() * 40000 + 3000);
52}
53