microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/extensionHelper.ts
43lines · 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 | |
| 4 | import * as path from "path"; |
| 5 | import * as fs from "fs"; |
| 6 | |
| 7 | export function getExtensionVersion() { |
| 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 | |
| 16 | export function findFileInFolderHierarchy(dir: string, filename: string): string | null { |
| 17 | let parentPath: string; |
| 18 | let projectRoot: string = dir; |
| 19 | let atFsRoot: boolean = false; |
| 20 | |
| 21 | while (!fs.existsSync(path.join(projectRoot, filename))) { |
| 22 | // Navigate up one level until either config.xml is found |
| 23 | parentPath = path.resolve(projectRoot, ".."); |
| 24 | if (parentPath !== projectRoot) { |
| 25 | projectRoot = parentPath; |
| 26 | } else { |
| 27 | // we have reached the filesystem root |
| 28 | atFsRoot = true; |
| 29 | break; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | if (atFsRoot) { |
| 34 | // We reached the fs root |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | return path.join(projectRoot, filename); |
| 39 | } |
| 40 | |
| 41 | export function generateRandomPortNumber() { |
| 42 | return Math.round(Math.random() * 40000 + 3000); |
| 43 | } |
| 44 | |