mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-26 13:25:51 +08:00
Introduce react native CLI
Reviewed By: @frantic Differential Revision: D2430522
This commit is contained in:
committed by
facebook-github-bot-4
parent
71628638f6
commit
833ca598bc
62
private-cli/src/cli.js
Normal file
62
private-cli/src/cli.js
Normal file
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* 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';
|
||||
|
||||
const Config = require('./util/Config');
|
||||
const dependencies = require('./dependencies/dependencies');
|
||||
const Promise = require('promise');
|
||||
|
||||
const documentedCommands = {
|
||||
dependencies: dependencies,
|
||||
};
|
||||
|
||||
const hiddenCommands = {
|
||||
'-h': help,
|
||||
'--help': help,
|
||||
};
|
||||
|
||||
/**
|
||||
* Programmatic entry point for the cli. This function runs the given
|
||||
* command passing it the arguments array.
|
||||
*/
|
||||
function run(command, commandArgs) {
|
||||
if (!command) {
|
||||
throw new Error(helpMessage());
|
||||
}
|
||||
commandArgs = commandArgs || [];
|
||||
|
||||
const commandToExec = documentedCommands[command] || hiddenCommands[command];
|
||||
if (!commandToExec) {
|
||||
throw new Error(helpMessage(command));
|
||||
}
|
||||
|
||||
commandToExec(commandArgs, Config.get()).done();
|
||||
}
|
||||
|
||||
function helpMessage(command) {
|
||||
const validCommands = Object
|
||||
.keys(documentedCommands)
|
||||
.map(c => '"' + c + '"')
|
||||
.join(' | ');
|
||||
|
||||
if (command) {
|
||||
return 'Unknown command "' + command + '". ' +
|
||||
'Available commands: ' + validCommands;
|
||||
} else {
|
||||
return 'Must specify a command. Available commands: ' +
|
||||
validCommands;
|
||||
}
|
||||
}
|
||||
|
||||
function help() {
|
||||
console.log(helpMessage());
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
module.exports.run = run;
|
||||
Reference in New Issue
Block a user