Skip to content

Commit 0f076ba

Browse files
fsonfacebook-github-bot
authored andcommitted
Make the CLI recognize CRNA projects
Summary: 1. 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. **Before**: <img width="762" alt="screen shot 2017-07-21 at 16 55 32" src="https://user-images.githubusercontent.com/497214/28467425-86b309c8-6e38-11e7-8946-139bda927d93.png"><img width="762" alt="screen shot 2017-07-21 at 16 55 52" src="https://user-images.githubusercontent.com/497214/28467436-8df02482-6e38-11e7-8a03-3fa664944cac.png"> **After**: <img width="762" alt="screen shot 2017-07-21 at 16 52 15" src="https://user-images.githubusercontent.com/497214/28467522-e4bb6cae-6e38-11e7-97bb-9cfa9cb4dc67.png"> 2. Make running `react-native link` inside a CRNA project display a helpful error message. **Before**: <img width="762" alt="screen shot 2017-07-21 at 16 55 10" src="https://user-images.githubusercontent.com/497214/28467608-1d1781fa-6e39-11e7-9620-cc16c8b1b40f.png"> **After**: <img width="762" alt="screen shot 2017-07-21 at 16 53 10" src="https://user-images.githubusercontent.com/497214/28467637-2cd6ed1a-6e39-11e7-8947-6df69b3f321e.png"> Fixes #14828. * Run `react-native run-ios`, `react-native run-android` and `react-native link` in: * A CRNA project (screenshot above) * A traditional RN project (existing behaviour) * A folder that contains neither (existing behaviour) Closes facebook/react-native#15139 Differential Revision: D5498914 Pulled By: hramos fbshipit-source-id: 94b6196e3451857bbaa45335a01643c89bed19a0
1 parent 829701a commit 0f076ba

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

link/link.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const getDependencyConfig = require('./getDependencyConfig');
3030
const pollParams = require('./pollParams');
3131
const commandStub = require('./commandStub');
3232
const promisify = require('./promisify');
33+
const findReactNativeScripts = require('../util/findReactNativeScripts');
3334

3435
import type {RNConfig} from '../core';
3536

@@ -147,6 +148,16 @@ function link(args: Array<string>, config: RNConfig) {
147148
return Promise.reject(err);
148149
}
149150

151+
if (!project.android && !project.ios && !project.windows && findReactNativeScripts()) {
152+
throw new Error(
153+
'`react-native link` can not be used in Create React Native App projects. ' +
154+
'If you need to include a library that relies on custom native code, ' +
155+
'you might have to eject first. ' +
156+
'See https://github.com/react-community/create-react-native-app/blob/master/EJECTING.md ' +
157+
'for more information.'
158+
);
159+
}
160+
150161
let packageName = args[0];
151162
// Check if install package by specific version (eg. package@latest)
152163
if (packageName !== undefined) {

runAndroid/runAndroid.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const chalk = require('chalk');
1313
const child_process = require('child_process');
1414
const fs = require('fs');
1515
const isPackagerRunning = require('../util/isPackagerRunning');
16+
const findReactNativeScripts = require('../util/findReactNativeScripts');
1617
const isString = require('lodash/isString');
1718
const path = require('path');
1819
const Promise = require('promise');
@@ -27,7 +28,16 @@ function checkAndroid(root) {
2728
*/
2829
function runAndroid(argv, config, args) {
2930
if (!checkAndroid(args.root)) {
30-
console.log(chalk.red('Android project not found. Maybe run react-native android first?'));
31+
const reactNativeScriptsPath = findReactNativeScripts();
32+
if (reactNativeScriptsPath) {
33+
child_process.spawnSync(
34+
reactNativeScriptsPath,
35+
['android'].concat(process.argv.slice(1)),
36+
{stdio: 'inherit'}
37+
);
38+
} else {
39+
console.log(chalk.red('Android project not found. Maybe run react-native android first?'));
40+
}
3141
return;
3242
}
3343

runIOS/runIOS.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@ const child_process = require('child_process');
1212
const fs = require('fs');
1313
const path = require('path');
1414
const findXcodeProject = require('./findXcodeProject');
15+
const findReactNativeScripts = require('../util/findReactNativeScripts');
1516
const parseIOSDevicesList = require('./parseIOSDevicesList');
1617
const findMatchingSimulator = require('./findMatchingSimulator');
1718
const getBuildPath = function(configuration = 'Debug', appName, isDevice) {
1819
return `build/Build/Products/${configuration}-${isDevice ? 'iphoneos' : 'iphonesimulator'}/${appName}.app`;
1920
};
2021

2122
function runIOS(argv, config, args) {
23+
if (!fs.existsSync(args.projectPath)) {
24+
const reactNativeScriptsPath = findReactNativeScripts();
25+
if (reactNativeScriptsPath) {
26+
child_process.spawnSync(
27+
reactNativeScriptsPath,
28+
['ios'].concat(process.argv.slice(1)),
29+
{stdio: 'inherit'}
30+
);
31+
return;
32+
} else {
33+
throw new Error('iOS project folder not found. Are you sure this is a React Native project?');
34+
}
35+
}
2236
process.chdir(args.projectPath);
2337
const xcodeProject = findXcodeProject(fs.readdirSync('.'));
2438
if (!xcodeProject) {

util/findReactNativeScripts.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @flow
10+
*/
11+
'use strict';
12+
13+
const path = require('path');
14+
const fs = require('fs');
15+
16+
function findReactNativeScripts(): ?string {
17+
const executablePath = path.resolve(
18+
'node_modules',
19+
'.bin',
20+
'react-native-scripts'
21+
);
22+
if (fs.existsSync(executablePath)) {
23+
return executablePath;
24+
}
25+
return null;
26+
}
27+
28+
module.exports = findReactNativeScripts;

0 commit comments

Comments
 (0)