From 6180ccd15dad82bd92b16ea4b0b99bd32aef9aed Mon Sep 17 00:00:00 2001 From: Mike Grabowski Date: Thu, 8 Jun 2017 10:02:50 +0200 Subject: [PATCH] Check in all places for react-native (#163) --- src/commands/init.js | 17 ++--------------- src/utils/isReactNativeProject.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 src/utils/isReactNativeProject.js diff --git a/src/commands/init.js b/src/commands/init.js index 62b9dcd4..852f256d 100644 --- a/src/commands/init.js +++ b/src/commands/init.js @@ -11,6 +11,7 @@ const dedent = require('dedent'); const ora = require('ora'); const chalk = require('chalk'); const inquirer = require('inquirer'); +const isReactNativeProject = require('../utils/isReactNativeProject'); const messages = require('../messages'); @@ -22,21 +23,7 @@ async function init() { const cwd = process.cwd(); // Are we inside a React Native project? - let valid = false; - - try { - const pak = JSON.parse( - fs.readFileSync(path.join(cwd, 'package.json')).toString(), - ); - - if (pak.dependencies['react-native']) { - valid = true; - } - } catch (e) { - // Ignore - } - - if (valid) { + if (isReactNativeProject(cwd)) { progress.succeed(messages.verifiedProject()); } else { progress.fail(messages.invalidProject()); diff --git a/src/utils/isReactNativeProject.js b/src/utils/isReactNativeProject.js new file mode 100644 index 00000000..3e7b2459 --- /dev/null +++ b/src/utils/isReactNativeProject.js @@ -0,0 +1,29 @@ +/** + * Copyright 2017-present, Callstack. + * All rights reserved. + * + * @flow + */ +const fs = require('fs'); +const path = require('path'); + +module.exports = function isReactNativeProject(cwd: string) { + try { + const pak = JSON.parse( + fs.readFileSync(path.join(cwd, 'package.json')).toString(), + ); + + const deps = { + ...(pak.dependencies || {}), + ...(pak.devDependencies || {}), + ...(pak.peerDependencies || {}), + }; + + if (deps['react-native']) { + return true; + } + } catch (e) { + // Ignore + } + return false; +};