react-native-macos/local-cli/cli.js

135 строки
3.9 KiB
JavaScript
Исходник Обычный вид История

/**
* 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.
*/
'use strict';
var bundle = require('../private-cli/src/bundle/bundle');
var Config = require('../private-cli/src/util/Config');
var fs = require('fs');
var generate = require('../private-cli/src/generate/generate');
var library = require('../private-cli/src/library/library');
var path = require('path');
var runAndroid = require('../private-cli/src/runAndroid/runAndroid');
var server = require('../private-cli/src/server/server');
var TerminalAdapter = require('yeoman-environment/lib/adapter.js');
var yeoman = require('yeoman-environment');
var availableCommands = {
'start': [server, 'starts the webserver'],
'bundle': [bundle, 'builds the javascript bundle for offline use'],
'new-library': [library, 'generates a native library bridge'],
'android': [generate, 'generates an Android project for your app'],
'run-android': [runAndroid, 'builds your app and starts it on a connected Android emulator or device']
};
/**
* Parses the command line and runs a command of the CLI.
*/
function run() {
var args = process.argv.slice(2);
if (args.length === 0) {
printUsage();
}
var config = Config.get(__dirname);
switch (args[0]) {
case 'start': return server(args, config).done();
case 'bundle': return bundle(args, config).done();
case 'new-library': return library(args, config).done();
case 'init': return printInitWarning();
case 'run-android': return runAndroid(args, config).done();
case 'android':
generate(
[
'--platform', 'android',
'--project-path', process.cwd(),
'--project-name', JSON.parse(
fs.readFileSync('package.json', 'utf8')
).name
],
config
).done();
break;
default:
console.error('Command `%s` unrecognized', args[0]);
printUsage();
}
}
function printUsage() {
console.log([
'Usage: react-native <command>',
'',
'Commands:'
].concat(
Object.keys(availableCommands).map(function(name){
return name + ':\t' + availableCommands[name][1];
}).join('\n')
));
process.exit(1);
}
// The user should never get here because projects are inited by
// using `react-native-cli` from outside a project directory.
function printInitWarning() {
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);
}
class CreateSuppressingTerminalAdapter extends TerminalAdapter {
constructor() {
super();
// suppres 'create' output generated by yeoman
this.log.create = function() {};
}
}
/**
* 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()
);
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-07 22:33:41 +03:00
if (require.main === module) {
run();
}
module.exports = {
run: run,
init: init,
};