microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/extensionHelper.ts
52lines · modeblame
549baae2RedMickey6 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
| 4 | import * as path from "path"; | |
| 5 | import * as fs from "fs"; | |
| 6 | | |
| 7 | export function getExtensionVersion() { | |
2d8af448Yuri Skorokhodov6 years ago | 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 | | |
9928222cYuri Skorokhodov5 years ago | 16 | export 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 | | |
2d8af448Yuri Skorokhodov6 years ago | 25 | export 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); | |
549baae2RedMickey6 years ago | 48 | } |
f872f4d5RedMickey6 years ago | 49 | |
| 50 | export function generateRandomPortNumber() { | |
| 51 | return Math.round(Math.random() * 40000 + 3000); | |
| 52 | } |