|
| 1 | +var http = require('http'); |
| 2 | +var fs = require('fs'); |
| 3 | + |
| 4 | +var OUT_PATH = 'iOS/main.jsbundle'; |
| 5 | + |
| 6 | +function getBundle(flags, callback) { |
| 7 | + var options = { |
| 8 | + host: 'localhost', |
| 9 | + port: 8081, |
| 10 | + path: '/index.ios.bundle?dev=' + flags.dev + '&minify=' + flags.minify |
| 11 | + }; |
| 12 | + |
| 13 | + console.log('Requesting bundle from local server'); |
| 14 | + var req = http.request(options, function(res) { |
| 15 | + var bundle = ''; |
| 16 | + |
| 17 | + res.on('data', function (chunk) { |
| 18 | + bundle += chunk; |
| 19 | + }); |
| 20 | + |
| 21 | + res.on('end', function () { |
| 22 | + console.log('Got bundle'); |
| 23 | + callback(bundle); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + // set up error handler |
| 28 | + req.on('error', function(err) { |
| 29 | + console.error('An error occured while connecting to local server. Are you sure the server is running? Run npm start'); |
| 30 | + }); |
| 31 | + |
| 32 | + // end request |
| 33 | + req.end(); |
| 34 | +} |
| 35 | + |
| 36 | +function showHelp() { |
| 37 | + console.log([ |
| 38 | + 'Usage: react-native bundle [options]', |
| 39 | + '', |
| 40 | + 'Options:', |
| 41 | + ' --dev\t\tsets DEV flag to true', |
| 42 | + ' --minify\tminify js bundle' |
| 43 | + ].join('\n')); |
| 44 | + process.exit(1); |
| 45 | +} |
| 46 | + |
| 47 | +module.exports = { |
| 48 | + init: function(args) { |
| 49 | + var flags = { |
| 50 | + help: args.indexOf('--help') !== -1, |
| 51 | + dev: args.indexOf('--dev') !== -1, |
| 52 | + minify: args.indexOf('--minify') !== -1 |
| 53 | + } |
| 54 | + |
| 55 | + if (flags.help) { |
| 56 | + showHelp(); |
| 57 | + } else { |
| 58 | + getBundle(flags, function(bundle) { |
| 59 | + fs.writeFile(OUT_PATH, bundle, function(err) { |
| 60 | + if(err) { |
| 61 | + return console.err('Error writing bundle to file', err); |
| 62 | + } |
| 63 | + console.log('Successfully saved bundle to ' + OUT_PATH); |
| 64 | + }); |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments