From 023ec39b3ffc2d7d529c35b81a514a84a16207b2 Mon Sep 17 00:00:00 2001 From: Ville Immonen Date: Fri, 21 Jul 2017 17:01:57 +0300 Subject: [PATCH] Make the CLI recognize CRNA projects Make running `react-native run-ios` or `react-native run-android` inside a project created with Create React Native App run the corresponding react-native-scripts command instead of failing. Make running `react-native link` inside a CRNA project display a helpful error message. --- local-cli/link/link.js | 11 ++++++++++ local-cli/runAndroid/runAndroid.js | 12 +++++++++- local-cli/runIOS/runIOS.js | 14 ++++++++++++ local-cli/util/findReactNativeScripts.js | 28 ++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 local-cli/util/findReactNativeScripts.js diff --git a/local-cli/link/link.js b/local-cli/link/link.js index 2f990f6c2d97cd..124bd8e668a61c 100644 --- a/local-cli/link/link.js +++ b/local-cli/link/link.js @@ -30,6 +30,7 @@ const getDependencyConfig = require('./getDependencyConfig'); const pollParams = require('./pollParams'); const commandStub = require('./commandStub'); const promisify = require('./promisify'); +const findReactNativeScripts = require('../util/findReactNativeScripts'); import type {RNConfig} from '../core'; @@ -147,6 +148,16 @@ function link(args: Array, config: RNConfig) { return Promise.reject(err); } + if (!project.android && !project.ios && !project.windows && findReactNativeScripts()) { + throw new Error( + '`react-native link` can not be used in Create React Native App projects. ' + + 'If you need to include a library that relies on custom native code, ' + + 'you might have to eject first. ' + + 'See https://github.com/react-community/create-react-native-app/blob/master/EJECTING.md ' + + 'for more information.' + ); + } + let packageName = args[0]; // Check if install package by specific version (eg. package@latest) if (packageName !== undefined) { diff --git a/local-cli/runAndroid/runAndroid.js b/local-cli/runAndroid/runAndroid.js index d985333178fa0b..14ea61caef9d78 100644 --- a/local-cli/runAndroid/runAndroid.js +++ b/local-cli/runAndroid/runAndroid.js @@ -13,6 +13,7 @@ const chalk = require('chalk'); const child_process = require('child_process'); const fs = require('fs'); const isPackagerRunning = require('../util/isPackagerRunning'); +const findReactNativeScripts = require('../util/findReactNativeScripts'); const isString = require('lodash/isString'); const path = require('path'); const Promise = require('promise'); @@ -27,7 +28,16 @@ function checkAndroid(root) { */ function runAndroid(argv, config, args) { if (!checkAndroid(args.root)) { - console.log(chalk.red('Android project not found. Maybe run react-native android first?')); + const reactNativeScriptsPath = findReactNativeScripts(); + if (reactNativeScriptsPath) { + child_process.spawnSync( + reactNativeScriptsPath, + ['android'].concat(process.argv.slice(1)), + {stdio: 'inherit'} + ); + } else { + console.log(chalk.red('Android project not found. Maybe run react-native android first?')); + } return; } diff --git a/local-cli/runIOS/runIOS.js b/local-cli/runIOS/runIOS.js index 9a70bb405853af..a5672316631f72 100644 --- a/local-cli/runIOS/runIOS.js +++ b/local-cli/runIOS/runIOS.js @@ -12,6 +12,7 @@ const child_process = require('child_process'); const fs = require('fs'); const path = require('path'); const findXcodeProject = require('./findXcodeProject'); +const findReactNativeScripts = require('../util/findReactNativeScripts'); const parseIOSDevicesList = require('./parseIOSDevicesList'); const findMatchingSimulator = require('./findMatchingSimulator'); const getBuildPath = function(configuration = 'Debug', appName, isDevice) { @@ -19,6 +20,19 @@ const getBuildPath = function(configuration = 'Debug', appName, isDevice) { }; function runIOS(argv, config, args) { + if (!fs.existsSync(args.projectPath)) { + const reactNativeScriptsPath = findReactNativeScripts(); + if (reactNativeScriptsPath) { + child_process.spawnSync( + reactNativeScriptsPath, + ['ios'].concat(process.argv.slice(1)), + {stdio: 'inherit'} + ); + return; + } else { + throw new Error('iOS project folder not found. Are you sure this is a React Native project?'); + } + } process.chdir(args.projectPath); const xcodeProject = findXcodeProject(fs.readdirSync('.')); if (!xcodeProject) { diff --git a/local-cli/util/findReactNativeScripts.js b/local-cli/util/findReactNativeScripts.js new file mode 100644 index 00000000000000..a66bb110d90e1a --- /dev/null +++ b/local-cli/util/findReactNativeScripts.js @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow + */ +'use strict'; + +const path = require('path'); +const fs = require('fs'); + +function findReactNativeScripts(): ?string { + const executablePath = path.resolve( + 'node_modules', + '.bin', + 'react-native-scripts' + ); + if (fs.existsSync(executablePath)) { + return executablePath; + } + return null; +} + +module.exports = findReactNativeScripts;