#!/usr/bin/env node

const path = require('path');
const fs = require('fs');
const program = require('commander');
const package = require('../package.json');
const fetch = require('../lib/fetch');
const Server = require('../lib/Server');

/**
 * Create a server instance using the provided options.
 * @param  {Object} opts react-native-webpack-server options
 * @return {Server}      react-native-webpack-server server
 */
function createServer(opts) {
  opts.webpackConfigPath = path.resolve(process.cwd(), opts.webpackConfigPath);
  if (fs.existsSync(opts.webpackConfigPath)) {
    opts.webpackConfig = require(path.resolve(process.cwd(), opts.webpackConfigPath));
  } else {
    throw new Error('Must specify webpackConfigPath or create ./webpack.config.js');
  }
  delete opts.webpackConfigPath;

  const server = new Server(opts);
  return server;
}

function commonOptions(program) {
  return program
    .option(
      '-H, --hostname [hostname]',
      'Hostname on which the server will listen. [localhost]',
      'localhost'
    )
    .option(
      '-P, --port [port]', 
      'Port on which the server will listen. [8080]', 
      8080
    )
    .option(
      '-p, --packagerPort [port]',
      'Port on which the react-native packager will listen. [8081]',
      8081
    )
    .option(
      '-w, --webpackPort [port]', 
      'Port on which the webpack dev server will listen. [8082]', 
      8082
    )
    .option(
      '-c, --webpackConfigPath [path]', 
      'Path to the webpack configuration file. [webpack.config.js]', 
      'webpack.config.js'
    )
    .option(
      '-e, --entry [name]',
      'Webpack entry module. [index.ios]',
      'index.ios'
    );
}
  
program.version(package.version);

commonOptions(program.command('start'))
  .description('Start the webpack server.')
  .option('-r, --hot', 'Enable hot module replacement. [false]', false)
  .action(function(options) {
    const opts = options.opts();
    const server = createServer(opts);
    server.start();
  });

commonOptions(program.command('bundle'))
  .description('Bundle the app for distribution.')
  .option(
    '-b, --bundlePath [path]',
    'Path where the bundle should be written. [./iOS/main.jsbundle]',
    './iOS/main.jsbundle'
  )
  .action(function(options) {
    const opts = options.opts();
    const server = createServer(opts);
    const url = 'http://localhost:' + opts.port + '/index.ios.bundle';
    const targetPath = path.resolve(opts.bundlePath);

    server.start();

    fetch(url).then(function(content) {
      fs.writeFileSync(targetPath, content);
      server.stop();

      // XXX: Hack something is keeping the process alive but we can still
      // safely kill here without leaving processes hanging around...
      process.exit(0);
    }).catch(function(err) {
      console.log('Error creating bundle...', err.stack);
      server.stop();
    });
  });

program.parse(process.argv);

@@ -1068,33 +1068,32 @@
  --port %5Bport%5D',
- 
 %0A      'Port on 
@@ -1130,17 +1130,16 @@
 %5B8080%5D',
- 
 %0A      8
@@ -1328,17 +1328,16 @@
 %5Bport%5D',
- 
 %0A      '
@@ -1394,17 +1394,16 @@
 %5B8082%5D',
- 
 %0A      8
@@ -1464,17 +1464,16 @@
 %5Bpath%5D',
- 
 %0A      '
@@ -1533,17 +1533,16 @@
 ig.js%5D',
- 
 %0A      '
@@ -1677,18 +1677,16 @@
    );%0A%7D%0A
-  
 %0Aprogram
@@ -2232,16 +2232,122 @@
 le'%0A  )%0A
+  .option(%0A    '--no-optimize',%0A    'Whether the bundle should skip optimization. %5Bfalse%5D',%0A    false%0A  )%0A
   .actio
@@ -2436,24 +2436,91 @@
 rver(opts);%0A
+    const query = (opts.optimize) ? '?dev=false&minify=true' : '';%0A
     const ur
@@ -2576,16 +2576,24 @@
 .bundle'
+ + query
 ;%0A    co

Optimize bundles by default