diff --git a/local-cli/link/link.js b/local-cli/link/link.js index 2f990f6c2..124bd8e66 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 d98533317..14ea61cae 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 9a70bb405..a56723166 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 000000000..a66bb110d --- /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;