diff --git a/cli.js b/cli.js index 99a3b24e0e2e8c..97116144bf4463 100644 --- a/cli.js +++ b/cli.js @@ -6,36 +6,37 @@ var spawn = require('child_process').spawn; var path = require('path'); - -function printUsage() { - console.log([ - 'Usage: react-native ', - '', - 'Commands:', - ' start: starts the webserver', - ].join('\n')); - process.exit(1); -} +var program = require('commander'); +var spec = require('./package.json'); function run() { - var args = process.argv.slice(2); - if (args.length === 0) { - printUsage(); - } + program.version(spec.version); + + program + .command('start') + .description('starts the webserver') + .action(startServer); - switch (args[0]) { - case 'start': - spawn('sh', [ - path.resolve(__dirname, 'packager', 'packager.sh'), - '--projectRoots', - process.cwd(), - ], {stdio: 'inherit'}); - break; - default: - console.error('Command `%s` unrecognized', args[0]); - printUsage(); + program.on('*', function(command) { + console.error('Command `%s` unrecognized', command); + program.outputHelp(); + process.exit(1); + }); + + program.parse(process.argv); + + if (!program.args.length) { + program.outputHelp(); + process.exit(1); } - // Here goes any cli commands we need to +} + +function startServer() { + spawn('sh', [ + path.resolve(__dirname, 'packager', 'packager.sh'), + '--projectRoots', + process.cwd(), + ], {stdio: 'inherit'}); } function init(root, projectName) { diff --git a/package.json b/package.json index 274a9ffdd7a7b9..f142c487a64be9 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "react-native-start": "packager/packager.sh" }, "dependencies": { + "commander": "^2.7.x", "connect": "2.8.3", "jstransform": "10.1.0", "react-timer-mixin": "^0.13.1", diff --git a/react-native-cli/index.js b/react-native-cli/index.js index 5742067f8ba80f..3c8999fb6b91c0 100755 --- a/react-native-cli/index.js +++ b/react-native-cli/index.js @@ -1,6 +1,9 @@ #!/usr/bin/env node /** + * This is the react-native binary installed globally. It shouldn't change + * much as it just delegates calls to cli.js, which can be updated idependently + * * Copyright 2004-present Facebook. All Rights Reserved. */