mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
Merge rnpm cli into react-native
Summary: This is an initial step of rewriting the CLI interface to use `rnpm` one (commander, plugins etc.). It's scope is to move all existing commands to use rnpm CLI interface, so that we get plugins, flags and our existing ecosystem working out of the box. <s>This is still WIP and some of the commands are left commented out.</s> For the `config` of `rnpm` (functions get info about project and dependency), <s>I am thinking we can merge them with</s> we decided to merge it with [`default.config.js`](e57683e420/local-cli/default.config.js (L33)), so they are available on the `new Config()` [instance](e57683e420/local-cli/cliEntry.js (L59)) (which means we don't have to change anything and current plugins, like runIOS and runAndroid can just start using it [w/o depending on any extra argument](https://github.com/grabbou/react-native/blob/e57683e420210749a5a6b802b4e Closes https://github.com/facebook/react-native/pull/7899 Differential Revision: D3613193 Pulled By: bestander fbshipit-source-id: 09a072f3b21e5239dfcd8da88a205bd28dc5d037
This commit is contained in:
committed by
Facebook Github Bot
parent
a37d5a825e
commit
e8b508144f
@@ -11,46 +11,14 @@
|
||||
const child_process = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const parseCommandLine = require('../util/parseCommandLine');
|
||||
const findXcodeProject = require('./findXcodeProject');
|
||||
const parseIOSSimulatorsList = require('./parseIOSSimulatorsList');
|
||||
const Promise = require('promise');
|
||||
|
||||
/**
|
||||
* Starts the app on iOS simulator
|
||||
*/
|
||||
function runIOS(argv, config) {
|
||||
return new Promise((resolve, reject) => {
|
||||
_runIOS(argv, config, resolve, reject);
|
||||
});
|
||||
}
|
||||
|
||||
function _runIOS(argv, config, resolve, reject) {
|
||||
const args = parseCommandLine([
|
||||
{
|
||||
command: 'simulator',
|
||||
description: 'Explicitly set simulator to use',
|
||||
type: 'string',
|
||||
required: false,
|
||||
default: 'iPhone 6',
|
||||
}, {
|
||||
command: 'scheme',
|
||||
description: 'Explicitly set Xcode scheme to use',
|
||||
type: 'string',
|
||||
required: false,
|
||||
}, {
|
||||
command: 'project-path',
|
||||
description: 'Path relative to project root where the Xcode project (.xcodeproj) lives. The default is \'ios\'.',
|
||||
type: 'string',
|
||||
required: false,
|
||||
default: 'ios',
|
||||
}
|
||||
], argv);
|
||||
|
||||
process.chdir(args['project-path']);
|
||||
function runIOS(argv, config, args) {
|
||||
process.chdir(args.projectPath);
|
||||
const xcodeProject = findXcodeProject(fs.readdirSync('.'));
|
||||
if (!xcodeProject) {
|
||||
reject(new Error(`Could not find Xcode project files in ios folder`));
|
||||
throw new Error('Could not find Xcode project files in ios folder');
|
||||
}
|
||||
|
||||
const inferredSchemeName = path.basename(xcodeProject.name, path.extname(xcodeProject.name));
|
||||
@@ -62,14 +30,14 @@ function _runIOS(argv, config, resolve, reject) {
|
||||
);
|
||||
const selectedSimulator = matchingSimulator(simulators, args.simulator);
|
||||
if (!selectedSimulator) {
|
||||
reject(new Error(`Cound't find ${args.simulator} simulator`));
|
||||
throw new Error(`Cound't find ${args.simulator} simulator`);
|
||||
}
|
||||
|
||||
const simulatorFullName = formattedSimulatorName(selectedSimulator)
|
||||
const simulatorFullName = formattedSimulatorName(selectedSimulator);
|
||||
console.log(`Launching ${simulatorFullName}...`);
|
||||
try {
|
||||
child_process.spawnSync('xcrun', ['instruments', '-w', simulatorFullName]);
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
// instruments always fail with 255 because it expects more arguments,
|
||||
// but we want it to only launch the simulator
|
||||
}
|
||||
@@ -81,42 +49,20 @@ function _runIOS(argv, config, resolve, reject) {
|
||||
'-derivedDataPath', 'build',
|
||||
];
|
||||
console.log(`Building using "xcodebuild ${xcodebuildArgs.join(' ')}"`);
|
||||
child_process.spawnSync('xcodebuild', xcodebuildArgs, {stdio: 'inherit'});
|
||||
|
||||
let appPath = `build/Build/Products/Debug-iphonesimulator/${inferredSchemeName}.app`;
|
||||
const xcodeBuildProcess = child_process.spawn('xcodebuild', xcodebuildArgs, {
|
||||
stdio: [process.stdin, 'pipe', process.stderr]
|
||||
});
|
||||
const appPath = `build/Build/Products/Debug-iphonesimulator/${inferredSchemeName}.app`;
|
||||
console.log(`Installing ${appPath}`);
|
||||
child_process.spawnSync('xcrun', ['simctl', 'install', 'booted', appPath], {stdio: 'inherit'});
|
||||
|
||||
xcodeBuildProcess.stdout.on('data', (data) => {
|
||||
process.stdout.write(data);
|
||||
const bundleID = child_process.execFileSync(
|
||||
'/usr/libexec/PlistBuddy',
|
||||
['-c', 'Print:CFBundleIdentifier', path.join(appPath, 'Info.plist')],
|
||||
{encoding: 'utf8'}
|
||||
).trim();
|
||||
|
||||
// search this part of the process output for a path to the generated app and replace default
|
||||
const appPathFromLog = data.toString().match(/Touch (build\/Build\/Products\/.*\/.*\.app)/);
|
||||
if (appPathFromLog) {
|
||||
appPath = appPathFromLog[1];
|
||||
}
|
||||
});
|
||||
|
||||
xcodeBuildProcess.on('close', (code) => {
|
||||
if (code !== 0) {
|
||||
reject(new Error(`xcodebuild process exited with code ${code}`));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Installing ${appPath}`);
|
||||
child_process.spawnSync('xcrun', ['simctl', 'install', 'booted', appPath], {stdio: 'inherit'});
|
||||
|
||||
const bundleID = child_process.execFileSync(
|
||||
'/usr/libexec/PlistBuddy',
|
||||
['-c', 'Print:CFBundleIdentifier', path.join(appPath, 'Info.plist')],
|
||||
{encoding: 'utf8'}
|
||||
).trim();
|
||||
|
||||
console.log(`Launching ${bundleID}`);
|
||||
child_process.spawnSync('xcrun', ['simctl', 'launch', 'booted', bundleID], {stdio: 'inherit'});
|
||||
|
||||
resolve();
|
||||
});
|
||||
console.log(`Launching ${bundleID}`);
|
||||
child_process.spawnSync('xcrun', ['simctl', 'launch', 'booted', bundleID], {stdio: 'inherit'});
|
||||
}
|
||||
|
||||
function matchingSimulator(simulators, simulatorName) {
|
||||
@@ -131,4 +77,31 @@ function formattedSimulatorName(simulator) {
|
||||
return `${simulator.name} (${simulator.version})`;
|
||||
}
|
||||
|
||||
module.exports = runIOS;
|
||||
module.exports = {
|
||||
name: 'run-ios',
|
||||
description: 'builds your app and starts it on iOS simulator',
|
||||
func: runIOS,
|
||||
examples: [
|
||||
{
|
||||
desc: 'Run on a different simulator, e.g. iPhone 5',
|
||||
cmd: 'react-native run-ios --simulator "iPhone 5"',
|
||||
},
|
||||
{
|
||||
desc: 'Pass a non-standard location of iOS directory',
|
||||
cmd: 'react-native run-ios --project-path "./app/ios"',
|
||||
},
|
||||
],
|
||||
options: [{
|
||||
command: '--simulator [string]',
|
||||
description: 'Explicitly set simulator to use',
|
||||
default: 'iPhone 6',
|
||||
}, {
|
||||
command: '--scheme [string]',
|
||||
description: 'Explicitly set Xcode scheme to use',
|
||||
}, {
|
||||
command: '--project-path [string]',
|
||||
description: 'Path relative to project root where the Xcode project '
|
||||
+ '(.xcodeproj) lives. The default is \'ios\'.',
|
||||
default: 'ios',
|
||||
}]
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user