2015-04-04 01:00:23 +00:00
|
|
|
/**
|
2015-10-20 16:53:54 +00:00
|
|
|
* 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.
|
2015-04-04 01:00:23 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-10-22 17:11:28 +00:00
|
|
|
require('../packager/babelRegisterOnly')([
|
|
|
|
/private-cli\/src/,
|
|
|
|
/local-cli/
|
|
|
|
]);
|
|
|
|
|
2015-10-26 14:55:29 +00:00
|
|
|
var bundle = require('./bundle/bundle');
|
2015-10-20 23:58:11 +00:00
|
|
|
var childProcess = require('child_process');
|
2015-10-26 14:55:29 +00:00
|
|
|
var Config = require('./util/Config');
|
2015-10-20 23:58:13 +00:00
|
|
|
var defaultConfig = require('./default.config');
|
2015-10-26 14:55:29 +00:00
|
|
|
var dependencies = require('./dependencies/dependencies');
|
|
|
|
var generate = require('./generate/generate');
|
|
|
|
var library = require('./library/library');
|
2015-10-27 18:19:38 +00:00
|
|
|
var link = require('./library/link');
|
2015-10-20 23:58:10 +00:00
|
|
|
var path = require('path');
|
2015-10-20 23:58:11 +00:00
|
|
|
var Promise = require('promise');
|
2015-10-26 14:55:29 +00:00
|
|
|
var runAndroid = require('./runAndroid/runAndroid');
|
2016-01-06 01:11:53 +00:00
|
|
|
var runIOS = require('./runIOS/runIOS');
|
2015-10-26 14:55:29 +00:00
|
|
|
var server = require('./server/server');
|
2015-10-20 23:58:10 +00:00
|
|
|
var TerminalAdapter = require('yeoman-environment/lib/adapter.js');
|
|
|
|
var yeoman = require('yeoman-environment');
|
2015-12-01 15:42:44 +00:00
|
|
|
var unbundle = require('./bundle/unbundle');
|
2015-10-26 14:55:29 +00:00
|
|
|
var upgrade = require('./upgrade/upgrade');
|
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
var gracefulFs = require('graceful-fs');
|
|
|
|
|
|
|
|
// graceful-fs helps on getting an error when we run out of file
|
|
|
|
// descriptors. When that happens it will enqueue the operation and retry it.
|
|
|
|
gracefulFs.gracefulify(fs);
|
2015-04-04 01:00:23 +00:00
|
|
|
|
2015-10-20 23:58:11 +00:00
|
|
|
var documentedCommands = {
|
2015-10-20 23:58:10 +00:00
|
|
|
'start': [server, 'starts the webserver'],
|
|
|
|
'bundle': [bundle, 'builds the javascript bundle for offline use'],
|
2015-12-01 15:42:44 +00:00
|
|
|
'unbundle': [unbundle, 'builds javascript as "unbundle" for offline use'],
|
2015-10-20 23:58:10 +00:00
|
|
|
'new-library': [library, 'generates a native library bridge'],
|
2015-10-27 18:19:38 +00:00
|
|
|
'link': [link, 'Adds a third-party library to your project. Example: react-native link awesome-camera'],
|
2015-10-20 23:58:11 +00:00
|
|
|
'android': [generateWrapper, 'generates an Android project for your app'],
|
2015-10-23 10:35:59 +00:00
|
|
|
'run-android': [runAndroid, 'builds your app and starts it on a connected Android emulator or device'],
|
2016-01-06 01:11:53 +00:00
|
|
|
'run-ios': [runIOS, 'builds your app and starts it on iOS simulator'],
|
2015-10-23 10:35:59 +00:00
|
|
|
'upgrade': [upgrade, 'upgrade your app\'s template files to the latest version; run this after ' +
|
|
|
|
'updating the react-native version in your package.json and running npm install']
|
2015-10-20 23:58:10 +00:00
|
|
|
};
|
|
|
|
|
2015-10-26 14:55:29 +00:00
|
|
|
var exportedCommands = {dependencies: dependencies};
|
|
|
|
Object.keys(documentedCommands).forEach(function(command) {
|
|
|
|
exportedCommands[command] = documentedCommands[command][0];
|
|
|
|
});
|
|
|
|
|
2015-10-20 23:58:11 +00:00
|
|
|
var undocumentedCommands = {
|
|
|
|
'init': [printInitWarning, ''],
|
|
|
|
};
|
|
|
|
|
|
|
|
var commands = Object.assign({}, documentedCommands, undocumentedCommands);
|
|
|
|
|
2015-10-20 23:58:10 +00:00
|
|
|
/**
|
|
|
|
* Parses the command line and runs a command of the CLI.
|
|
|
|
*/
|
|
|
|
function run() {
|
|
|
|
var args = process.argv.slice(2);
|
|
|
|
if (args.length === 0) {
|
|
|
|
printUsage();
|
|
|
|
}
|
|
|
|
|
2016-03-07 02:55:44 +00:00
|
|
|
var setupEnvScript = /^win/.test(process.platform)
|
2015-10-20 23:58:11 +00:00
|
|
|
? 'setup_env.bat'
|
|
|
|
: 'setup_env.sh';
|
|
|
|
childProcess.execFileSync(path.join(__dirname, setupEnvScript));
|
|
|
|
|
|
|
|
var command = commands[args[0]];
|
|
|
|
if (!command) {
|
|
|
|
console.error('Command `%s` unrecognized', args[0]);
|
|
|
|
printUsage();
|
|
|
|
return;
|
2015-10-20 23:58:10 +00:00
|
|
|
}
|
2015-10-20 23:58:11 +00:00
|
|
|
|
2015-10-20 23:58:13 +00:00
|
|
|
command[0](args, Config.get(__dirname, defaultConfig)).done();
|
2015-10-20 23:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function generateWrapper(args, config) {
|
|
|
|
return generate([
|
|
|
|
'--platform', 'android',
|
|
|
|
'--project-path', process.cwd(),
|
|
|
|
'--project-name', JSON.parse(
|
|
|
|
fs.readFileSync('package.json', 'utf8')
|
|
|
|
).name
|
|
|
|
], config);
|
2015-10-20 23:58:10 +00:00
|
|
|
}
|
2015-10-15 17:19:21 +00:00
|
|
|
|
2015-04-04 01:00:23 +00:00
|
|
|
function printUsage() {
|
|
|
|
console.log([
|
|
|
|
'Usage: react-native <command>',
|
|
|
|
'',
|
2015-10-20 23:58:10 +00:00
|
|
|
'Commands:'
|
2015-10-20 23:58:11 +00:00
|
|
|
].concat(Object.keys(documentedCommands).map(function(name) {
|
|
|
|
return ' - ' + name + ': ' + documentedCommands[name][1];
|
|
|
|
})).join('\n'));
|
2015-04-04 01:00:23 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2015-10-20 23:58:10 +00:00
|
|
|
// The user should never get here because projects are inited by
|
|
|
|
// using `react-native-cli` from outside a project directory.
|
2015-05-04 19:17:47 +00:00
|
|
|
function printInitWarning() {
|
2015-10-20 23:58:11 +00:00
|
|
|
return Promise.resolve().then(function() {
|
|
|
|
console.log([
|
|
|
|
'Looks like React Native project already exists in the current',
|
|
|
|
'folder. Run this command from a different folder or remove node_modules/react-native'
|
|
|
|
].join('\n'));
|
|
|
|
process.exit(1);
|
|
|
|
});
|
2015-05-04 19:17:47 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 23:58:10 +00:00
|
|
|
class CreateSuppressingTerminalAdapter extends TerminalAdapter {
|
|
|
|
constructor() {
|
|
|
|
super();
|
2015-11-17 17:47:55 +00:00
|
|
|
// suppress 'create' output generated by yeoman
|
2015-10-20 23:58:10 +00:00
|
|
|
this.log.create = function() {};
|
2015-04-04 01:00:23 +00:00
|
|
|
}
|
2015-10-20 23:58:10 +00:00
|
|
|
}
|
2015-04-04 01:00:23 +00:00
|
|
|
|
2015-10-20 23:58:10 +00:00
|
|
|
/**
|
|
|
|
* Creates the template for a React Native project given the provided
|
|
|
|
* parameters:
|
|
|
|
* - projectDir: templates will be copied here.
|
|
|
|
* - argsOrName: project name or full list of custom arguments to pass to the
|
|
|
|
* generator.
|
|
|
|
*/
|
|
|
|
function init(projectDir, argsOrName) {
|
|
|
|
console.log('Setting up new React Native app in ' + projectDir);
|
|
|
|
var env = yeoman.createEnv(
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
new CreateSuppressingTerminalAdapter()
|
|
|
|
);
|
2015-10-20 18:46:37 +00:00
|
|
|
|
2015-10-20 23:58:10 +00:00
|
|
|
env.register(
|
|
|
|
require.resolve(path.join(__dirname, 'generator')),
|
|
|
|
'react:app'
|
|
|
|
);
|
|
|
|
|
|
|
|
// argv is for instance
|
|
|
|
// ['node', 'react-native', 'init', 'AwesomeApp', '--verbose']
|
|
|
|
// args should be ['AwesomeApp', '--verbose']
|
|
|
|
var args = Array.isArray(argsOrName)
|
|
|
|
? argsOrName
|
|
|
|
: [argsOrName].concat(process.argv.slice(4));
|
|
|
|
|
|
|
|
var generator = env.create('react:app', {args: args});
|
|
|
|
generator.destinationRoot(projectDir);
|
|
|
|
generator.run();
|
2015-04-04 01:00:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 19:33:41 +00:00
|
|
|
if (require.main === module) {
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
|
2015-04-04 01:00:23 +00:00
|
|
|
module.exports = {
|
|
|
|
run: run,
|
|
|
|
init: init,
|
2015-10-26 14:55:29 +00:00
|
|
|
commands: exportedCommands
|
2015-04-04 01:00:23 +00:00
|
|
|
};
|