import path from 'path';
import { exec } from 'child_process';
import webpack from 'webpack';
import express from 'express';
import WebpackDevServer from 'webpack-dev-server';
import historyApiFallback from 'connect-history-api-fallback';
import githubHookHandler from 'github-webhook-handler';
import chalk from 'chalk';
import cors from 'cors';
import webpackConfig from '../webpack.config';
import config from './config';

require('babel-polyfill');

const handler = githubHookHandler({ path: '/github', secret: 'test1234' });
if (config.env === 'development') {
  const gitCallbackServer = express();
  gitCallbackServer.use(cors());
  gitCallbackServer.use((req, _res, next) => {
    console.log('req.url', req.url);
    handler(req, _res, (err) => {
      const res = _res;
      res.statusCode = 404;
      res.end('no such location');
    });
    next();
  });
  handler.on('push', (event) => {
    exec(path.join(__dirname, '../git_update.sh'), (error, stdout, stderr) => {
    });
  });
  gitCallbackServer.listen(4000, () => console.log(chalk.green('gitCallbackServer is listening on port 4000')));

  const server = new WebpackDevServer(webpack(webpackConfig), {
    contentBase: '/build/',
    hot: true,
    historyApiFallback: true,
  });
  server.use(cors());
  // Serve static resources
  // server.use('/', express.static(path.join(__dirname, '../build')));
  server.use('/doc', express.static(path.join(__dirname, '../documentation/html')));
  server.listen(config.port, () => console.log(chalk.green(`Server is listening on port ${config.port}`)));
} else if (config.env === 'production') {
  // Launch Relay by creating a normal express server
  const server = express();
  server.use(cors());
  server.use(historyApiFallback());
  server.use('/', express.static(path.join(__dirname, '../build')));
  server.use('/doc', express.static(path.join(__dirname, '../documentation/html')));
  server.listen(config.port, () => console.log(chalk.green(`Server is listening on port ${config.port}`)));
}

@@ -450,665 +450,43 @@
 ');%0A
-%0Aconst handler = githubHookHandler(%7B path: '/github', secret: 'test1234' %7D);%0Aif (config.env === 'development') %7B%0A  const gitCallbackServer = express();%0A  gitCallbackServer.use(cors());%0A  gitCallbackServer.use((req, _res, next) =%3E %7B%0A    console.log('req.url', req.url);%0A    handler(req, _res, (err) =%3E %7B%0A      const res = _res;%0A      res.statusCode = 404;%0A      res.end('no such location');%0A    %7D);%0A    next();%0A  %7D);%0A  handler.on('push', (event) =%3E %7B%0A    exec(path.join(__dirname, '../git_update.sh'), (error, stdout, stderr) =%3E %7B%0A    %7D);%0A  %7D);%0A  gitCallbackServer.listen(4000, () =%3E console.log(chalk.green('gitCallbackServer is listening on port 4000')));%0A
+if (config.env === 'development') %7B
 %0A  c
@@ -651,108 +651,8 @@
 ));%0A
-  // Serve static resources%0A  // server.use('/', express.static(path.join(__dirname, '../build')));%0A
   se
@@ -1286,8 +1286,592 @@
 %60)));%0A%7D%0A
+%0A%0Aconst handler = githubHookHandler(%7B path: '/github', secret: 'test1234' %7D);%0Aconst gitCallbackServer = express();%0AgitCallbackServer.use(cors());%0AgitCallbackServer.use((req, _res, next) =%3E %7B%0A  console.log('req.url', req.url);%0A  handler(req, _res, (err) =%3E %7B%0A    const res = _res;%0A    res.statusCode = 404;%0A    res.end('no such location');%0A  %7D);%0A  next();%0A%7D);%0Ahandler.on('push', () =%3E %7B%0A  exec(path.join(__dirname, '../git_update.sh'), (error, stdout, stderr) =%3E %7B%0A  %7D);%0A%7D);%0AgitCallbackServer.listen(4000, () =%3E console.log(chalk.green('gitCallbackServer is listening on port 4000')));

update for production mode