microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/reactNativeProjectHelper.ts
45lines · modeblame
3d97c2a3Meena Kunnathur Balakrishnan10 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 | | |
bd539474digeff10 years ago | 4 | import * as Q from "q"; |
3d97c2a3Meena Kunnathur Balakrishnan10 years ago | 5 | import {Package} from "./node/package"; |
| 6 | | |
| 7 | export class ReactNativeProjectHelper { | |
| 8 | private workspaceRoot: string; | |
da86e6ffdigeff10 years ago | 9 | private static REACT_NATIVE_NPM_LIB_NAME = "react-native"; |
3d97c2a3Meena Kunnathur Balakrishnan10 years ago | 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 => { | |
0633e07bMeena Kunnathur Balakrishnan10 years ago | 23 | return !!(dependencies && dependencies["react-native"]); |
fb8f49fbMeena Kunnathur Balakrishnan10 years ago | 24 | }).catch((err: Error) => { |
3d97c2a3Meena Kunnathur Balakrishnan10 years ago | 25 | return Q.resolve(false); |
| 26 | }); | |
| 27 | } | |
bd539474digeff10 years ago | 28 | |
| 29 | public validateReactNativeVersion(): Q.Promise<void> { | |
da86e6ffdigeff10 years ago | 30 | return new Package(this.workspaceRoot).dependencyPackage(ReactNativeProjectHelper.REACT_NATIVE_NPM_LIB_NAME).version().then(version => { |
bd539474digeff10 years ago | 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 | |
4a25dae0digeff10 years ago | 34 | let minorVersion = parseInt(components[1], 10); |
| 35 | if (components[0] !== "0" || minorVersion >= 19) { | |
bd539474digeff10 years ago | 36 | return Q.resolve<void>(void 0); |
4a25dae0digeff10 years ago | 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 | |
bd539474digeff10 years ago | 40 | } |
| 41 | | |
4a25dae0digeff10 years ago | 42 | return Q.reject<void>(`Unable to read version = ${version}`); |
bd539474digeff10 years ago | 43 | }); |
| 44 | } | |
3d97c2a3Meena Kunnathur Balakrishnan10 years ago | 45 | } |