Skip to content

Commit

Permalink
react-native-cli initial commit
Browse files Browse the repository at this point in the history
A cli package that is responsible for:
* starting a new react-native project
* forwarding all other commands to a cli module in react-native
* currently cli.js knows how to start the webserver, in the future it may do things like build the app etc.
  • Loading branch information
amasad committed Mar 20, 2015
1 parent e2e123c commit 98097ca
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*/

'use strict';

var spawn = require('child_process').spawn;
var path = require('path');

function printUsage() {
console.log([
'Usage: react-native <command>',
'',
'Commands:',
' start: starts the webserver',
].join('\n'));
process.exit(1);
}

function run() {
var args = process.argv.slice(2);
if (args.length === 0) {
printUsage();
}

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();
}
// Here goes any cli commands we need to
}

function init() {
spawn('sh', [path.resolve(__dirname, 'init.sh')], {stdio:'inherit'});
}

module.exports = {
run: run,
init: init,
};
82 changes: 82 additions & 0 deletions react-native-cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env node

/**
* Copyright 2004-present Facebook. All Rights Reserved.
*/

var spawn = require('child_process').spawn;
var path = require('path');

var CLI_MODULE_PATH = path.resolve(
process.cwd(),
'node_modules',
'react-native',
'cli'
);

var cli;
try {
cli = require(CLI_MODULE_PATH);
} catch(e) {}

if (cli) {
cli.run();
} else {
var args = process.argv.slice(2);
if (args.length === 0) {
console.error(
'You did not pass any commands, did you mean to run init?'
);
process.exit(1);
}

if (args[0] === 'init') {
init();
} else {
console.error(
'Command `%s` unrecognized.' +
'Did you mean to run this inside a react-native project?',

This comment has been minimized.

Copy link
@sahrens

sahrens Mar 20, 2015

Contributor

Maybe suggest running npm update?

This comment has been minimized.

Copy link
@amasad

amasad Mar 20, 2015

Author Contributor

(y)

args[0]
);
process.exit(1);
}
}

function init() {
console.log(
'This will walk you through creating a new react-native project',
'in the current directory'
);

console.log('Running npm init');
run('npm init', function(e) {
if (e) {
console.error('npm init failed');

This comment has been minimized.

Copy link
@sahrens

sahrens Mar 20, 2015

Contributor

print the error? Or will it already by printed to stderr anyway?

This comment has been minimized.

Copy link
@amasad

amasad Mar 20, 2015

Author Contributor

Yes it will be printed, since the child process is inherited stdio

process.exit(1);
}

run('npm install --save react-native', function(e) {
if (e) {
console.error('`npm install --save react-native` failed');
process.exit(1);
}

var cli = require(CLI_MODULE_PATH);
cli.init();
});
});
}

function run(command, cb) {
var parts = command.split(/\s+/);
var cmd = parts[0];
var args = parts.slice(1);
var proc = spawn(cmd, args, {stdio: 'inherit'});
proc.on('close', function(code) {
if (code !== 0) {
cb(new Error('Command exited with a non-zero status'));
} else {
cb(null);
}
});
}

This comment has been minimized.

Copy link
@vjeux

vjeux Mar 20, 2015

Contributor

Really good! Thanks!

9 changes: 9 additions & 0 deletions react-native-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "react-native-cli",
"version": "0.0.0",
"description": "The ReactNative cli tools",
"main": "index.js",
"bin": {
"react-native": "index.js"
}
}

2 comments on commit 98097ca

@amasad
Copy link
Contributor Author

@amasad amasad commented on 98097ca Mar 20, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sahrens
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice :)

Please sign in to comment.