diff --git a/cli.js b/cli.js index 8a1a306b6db..99a3b24e0e2 100644 --- a/cli.js +++ b/cli.js @@ -38,8 +38,8 @@ function run() { // Here goes any cli commands we need to } -function init() { - spawn('sh', [path.resolve(__dirname, 'init.sh')], {stdio:'inherit'}); +function init(root, projectName) { + spawn(path.resolve(__dirname, 'init.sh'), [projectName], {stdio:'inherit'}); } module.exports = { diff --git a/packager/init.sh b/init.sh similarity index 94% rename from packager/init.sh rename to init.sh index 6a42b9aeb6e..f74850d5724 100755 --- a/packager/init.sh +++ b/init.sh @@ -14,7 +14,7 @@ def cp(src, dest, app_name) end def main(dest, app_name) - source = File.expand_path("../../Examples/SampleApp", __FILE__) + source = File.expand_path("../Examples/SampleApp", __FILE__) files = Dir.chdir(source) { Dir["**/*"] } .reject { |file| file["project.xcworkspace"] || file["xcuserdata"] } .each { |file| diff --git a/react-native-cli/index.js b/react-native-cli/index.js index cc69da9d5b2..49ee5001396 100755 --- a/react-native-cli/index.js +++ b/react-native-cli/index.js @@ -4,19 +4,22 @@ * Copyright 2004-present Facebook. All Rights Reserved. */ -var spawn = require('child_process').spawn; +var fs = require('fs'); var path = require('path'); +var spawn = require('child_process').spawn; -var CLI_MODULE_PATH = path.resolve( - process.cwd(), - 'node_modules', - 'react-native', - 'cli' -); +var CLI_MODULE_PATH = function() { + return path.resolve( + process.cwd(), + 'node_modules', + 'react-native', + 'cli' + ); +}; var cli; try { - cli = require(CLI_MODULE_PATH); + cli = require(CLI_MODULE_PATH()); } catch(e) {} if (cli) { @@ -25,13 +28,20 @@ if (cli) { var args = process.argv.slice(2); if (args.length === 0) { console.error( - 'You did not pass any commands, did you mean to run init?' + 'You did not pass any commands, did you mean to run `react-native init`?' ); process.exit(1); } if (args[0] === 'init') { - init(); + if (args[1]) { + init(args[1]); + } else { + console.error( + 'Usage: react-native init ' + ); + process.exit(1); + } } else { console.error( 'Command `%s` unrecognized.' + @@ -42,28 +52,38 @@ if (cli) { } } -function init() { +function init(name) { + var root = path.resolve(name); + var projectName = path.basename(root); + console.log( - 'This will walk you through creating a new react-native project', - 'in the current directory' + 'This will walk you through creating a new React Native project in', + root ); - console.log('Running npm init'); - run('npm init', function(e) { + if (!fs.existsSync(root)) { + fs.mkdirSync(root); + } + + var packageJson = { + name: projectName, + version: '0.0.1', + private: true, + scripts: { + start: "react-native start" + } + }; + fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson)); + process.chdir(root); + + run('npm install --save react-native', function(e) { if (e) { - console.error('npm init failed'); + console.error('`npm install --save react-native` failed'); 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(); - }); + var cli = require(CLI_MODULE_PATH()); + cli.init(root, projectName); }); }