microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/reactNativeProjectHelper.ts
45lines · 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 Q from "q"; |
| 5 | import {Package} from "./node/package"; |
| 6 | |
| 7 | export class ReactNativeProjectHelper { |
| 8 | private workspaceRoot: string; |
| 9 | private static REACT_NATIVE_NPM_LIB_NAME = "react-native"; |
| 10 | |
| 11 | constructor(workspaceRoot: string) { |
| 12 | this.workspaceRoot = workspaceRoot; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Ensures that we are in a React Native project and then executes the operation |
| 17 | * Otherwise, displays an error message banner |
| 18 | * {operation} - a function that performs the expected operation |
| 19 | */ |
| 20 | public isReactNativeProject(): Q.Promise<boolean> { |
| 21 | let currentPackage = new Package(this.workspaceRoot); |
| 22 | return currentPackage.dependencies().then(dependencies => { |
| 23 | return !!(dependencies && dependencies["react-native"]); |
| 24 | }).catch((err: Error) => { |
| 25 | return Q.resolve(false); |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | public validateReactNativeVersion(): Q.Promise<void> { |
| 30 | return new Package(this.workspaceRoot).dependencyPackage(ReactNativeProjectHelper.REACT_NATIVE_NPM_LIB_NAME).version().then(version => { |
| 31 | // TODO-V1: Use semver instead of all this logic |
| 32 | const components = version.split("."); |
| 33 | if (components.length >= 2) { // Even though react-native versions have 3 components, we only care about the first 2 |
| 34 | let minorVersion = parseInt(components[1], 10); |
| 35 | if (components[0] !== "0" || minorVersion >= 19) { |
| 36 | return Q.resolve<void>(void 0); |
| 37 | } else if (!(<any>Number).isNaN(minorVersion)) { |
| 38 | return Q.reject<void>(`Project version = ${version}`); |
| 39 | } // else minor version is nan. Continue to return parse error |
| 40 | } |
| 41 | |
| 42 | return Q.reject<void>(`Unable to read version = ${version}`); |
| 43 | }); |
| 44 | } |
| 45 | } |
| 46 | |