From 132f8fa3ce2d45da887b04989001ceefd817e200 Mon Sep 17 00:00:00 2001 From: Joe Stanton Date: Sun, 29 Mar 2015 14:15:51 +0100 Subject: [PATCH] 1-1 port of init script to Nodejs This will remove the dependency on Ruby. --- cli.js | 37 +++++++++++++++++ init.sh | 86 ++++++++++++++++++++------------------- package.json | 8 ++++ react-native-cli/index.js | 24 ++++++++--- 4 files changed, 109 insertions(+), 46 deletions(-) diff --git a/cli.js b/cli.js index 2c527a5df..d72f5cfc3 100644 --- a/cli.js +++ b/cli.js @@ -5,3 +5,40 @@ 'use strict'; module.exports = require('./local-cli/cli.js'); +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); +} + +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 +} + +module.exports = { + run: run +}; diff --git a/init.sh b/init.sh index e7703f296..534fec959 100755 --- a/init.sh +++ b/init.sh @@ -1,46 +1,50 @@ -#!/usr/bin/env ruby +#!/usr/bin/env node +'use strict'; -def cp(src, dest, app_name) - if File.directory?(src) - Dir.mkdir(dest) unless Dir.exists?(dest) - else - content = File.read(src) - .gsub("SampleApp", app_name) - .gsub("Examples/#{app_name}/", "") - .gsub("../../Libraries/", "node_modules/react-native/Libraries/") - .gsub("../../React/", "node_modules/react-native/React/") - File.write(dest, content) - end -end +var path = require('path'); +var fs = require('fs'); +var file = require('file'); -def main(dest, app_name) - source = File.expand_path("../Examples/SampleApp", __FILE__) - files = Dir.chdir(source) { Dir["**/*"] } - .reject { |file| file["project.xcworkspace"] || file["xcuserdata"] } - .each { |file| - new_file = file - .gsub("SampleApp", app_name) - .gsub(/^_/, ".") - cp File.join(source, file), File.join(dest, new_file), app_name +if (process.argv.length === 0) { + console.log('Usage: ' + path.basename(__filename) + ' '); + console.log(''); + console.log('This script will bootstrap new React Native app in current folder'); + process.exit(1); +} + +var appName = process.argv[2]; +var dest = process.cwd(); +console.log('Setting up new React Native app in ' + dest); +console.log(''); + +main(dest, appName); + +function cp(src, dest, appName) { + if (fs.lstatSync(src).isDirectory()) { + if (!fs.existsSync(dest)) { + fs.mkdirSync(dest); } -end + } + else { + var content = fs.readFileSync(src, 'utf8') + .replace(new RegExp('SampleApp', 'g'), appName) + .replace(new RegExp('Examples/' + appName + '/', 'g'), '') + .replace(new RegExp('../../Libraries/', 'g'), 'node_modules/react-native/Libraries/') + .replace(new RegExp('../../React/', 'g'), 'node_modules/react-native/React/'); + fs.writeFileSync(dest, content); + } +} -if ARGV.count == 0 - puts "Usage: #{__FILE__} " - puts "" - puts "This script will bootstrap new React Native app in current folder" -else - app_name = ARGV.first - dest = Dir.pwd - puts "Setting up new React Native app in #{dest}" - puts "" - - main(dest, app_name) - - puts "Next steps:" - puts "" - puts " Open #{File.join(dest, app_name)}.xcodeproj in Xcode" - puts " Hit Run button" - puts "" -end +function main(dest, appName) { + var source = path.resolve(__dirname, 'Examples/SampleApp'); + file.walk(source, function(error, _, dirs, files) { + if (error) { throw error; } + dirs.concat(files).forEach(function(f) { + f = f.replace(source + '/', ''); // Strip off absolute path + if (f === 'project.xcworkspace' || f === 'xcuserdata') { return; } + var newFile = f.replace(new RegExp('SampleApp', 'g'), appName); + cp(path.resolve(source, f), path.resolve(dest, newFile), appName); + }); + }); +} diff --git a/package.json b/package.json index 0421c96a8..c59ccf41e 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,14 @@ "react-native-start": "packager/packager.sh" }, "dependencies": { + "connect": "2.8.3", + "file": "^0.2.2", + "jstransform": "10.0.1", + "react-timer-mixin": "^0.13.1", + "react-tools": "0.13.0-rc2", + "rebound": "^0.0.12", + "source-map": "0.1.31", + "stacktrace-parser": "0.1.1", "absolute-path": "0.0.0", "bluebird": "^2.9.21", "chalk": "^1.0.0", diff --git a/react-native-cli/index.js b/react-native-cli/index.js index 7bd237e02..8579933cb 100755 --- a/react-native-cli/index.js +++ b/react-native-cli/index.js @@ -85,17 +85,31 @@ function init(name) { start: 'node_modules/react-native/packager/packager.sh' } }; - fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson)); + fs.writeFileSync( + path.join(root, 'package.json'), + JSON.stringify(packageJson, null, 2) + ); process.chdir(root); - run('npm install --save react-native', function(e) { + var initCmd = path.resolve(__dirname, '..', 'init.sh') + ' ' + projectName; + run(initCmd, function(e) { if (e) { - console.error('`npm install --save react-native` failed'); + console.error('initialization failed'); process.exit(1); } - var cli = require(CLI_MODULE_PATH()); - cli.init(root, projectName); + run('npm install --save react-native', function(e) { + if (e) { + console.error('`npm install --save react-native` failed'); + process.exit(1); + } + + console.log('Next steps:'); + console.log(''); + console.log(' Open ' + path.join(root, projectName) + '.xcodeproj in Xcode'); + console.log(' Hit Run button'); + console.log(''); + }); }); }