Skip to content

Commit

Permalink
Added bundle script
Browse files Browse the repository at this point in the history
  • Loading branch information
arthuralee committed Apr 16, 2015
1 parent 2cc3c50 commit a8e471d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
68 changes: 68 additions & 0 deletions local-cli/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var http = require('http');
var fs = require('fs');

var OUT_PATH = 'iOS/main.jsbundle';

function getBundle(flags, callback) {
var options = {
host: 'localhost',
port: 8081,
path: '/index.ios.bundle?dev=' + flags.dev + '&minify=' + flags.minify
};

console.log('Requesting bundle from local server');
var req = http.request(options, function(res) {
var bundle = '';

res.on('data', function (chunk) {
bundle += chunk;
});

res.on('end', function () {
console.log('Got bundle');
callback(bundle);
});
});

// set up error handler
req.on('error', function(err) {
console.error('An error occured while connecting to local server. Are you sure the server is running? Run npm start');
});

// end request
req.end();
}

function showHelp() {
console.log([
'Usage: react-native bundle [options]',
'',
'Options:',
' --dev\t\tsets DEV flag to true',
' --minify\tminify js bundle'
].join('\n'));
process.exit(1);
}

module.exports = {
init: function(args) {
var flags = {
help: args.indexOf('--help') !== -1,
dev: args.indexOf('--dev') !== -1,
minify: args.indexOf('--minify') !== -1
}

if (flags.help) {
showHelp();
} else {
getBundle(flags, function(bundle) {
fs.writeFile(OUT_PATH, bundle, function(err) {
if(err) {
return console.err('Error writing bundle to file', err);
}
console.log('Successfully saved bundle to ' + OUT_PATH);
});
});
}
}
}
7 changes: 6 additions & 1 deletion local-cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
var spawn = require('child_process').spawn;
var path = require('path');
var install = require('./install.js');
var bundle = require('./bundle.js');

function printUsage() {
console.log([
'Usage: react-native <command>',
'',
'Commands:',
' start: starts the webserver',
' install: installs npm react components'
' install: installs npm react components',
' build: builds the javascript bundle for offline use'
].join('\n'));
process.exit(1);
}
Expand All @@ -36,6 +38,9 @@ function run() {
case 'install':
install.init();
break;
case 'bundle':
bundle.init(args);
break;
default:
console.error('Command `%s` unrecognized', args[0]);
printUsage();
Expand Down

0 comments on commit a8e471d

Please sign in to comment.