// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/

import gulp from 'gulp';
import debug from 'gulp-debug';
import changed from 'gulp-changed';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import colors from 'colors/safe';

import * as Paths from '../../src/shared/paths';
import * as Endpoints from '../../src/shared/endpoints';
import config from '../webpack/config.browser-frontend';
import { makeDevConfig, makeProdConfig } from '../webpack/config.base';

const WEBPACK_STATS_OPTIONS = {
  colors: true,
  warnings: false,
};

let gWebpackDevServer;

gulp.task('browser-frontend:copy-html', () =>
  gulp.src(`${Paths.BROWSER_FRONTEND_SRC}/**/*.html`)
    .pipe(changed(Paths.BROWSER_FRONTEND_DST))
    .pipe(debug({ title: `Running ${colors.cyan('cp')}` }))
    .pipe(gulp.dest(Paths.BROWSER_FRONTEND_DST)),
);

gulp.task('browser-frontend:webpack', (cb) => {
  const currentConfig = process.env.NODE_ENV === 'production'
    ? makeProdConfig(config)
    : makeDevConfig(config);

  const compiler = webpack(currentConfig);
  const server = new WebpackDevServer(compiler, {
    stats: WEBPACK_STATS_OPTIONS,
  });

  // We'll have to close the webpack dev server later, so store it as a global.
  // If we leave the server open, the build process will keep running forever.
  gWebpackDevServer = server;

  server.listen(
    Endpoints.WEBPACK_DEV_SERVER_PORT,
    Endpoints.WEBPACK_DEV_SERVER_HOST, cb);
});

gulp.task('browser-frontend:build', gulp.series(
  'browser-frontend:copy-html',
  'browser-frontend:webpack',
));

gulp.task('browser-frontend:build:cleanup', cb =>
  gWebpackDevServer.close(cb),
);

@@ -309,16 +309,49 @@
 safe';%0A%0A
+import logger from '../logger';%0A%0A
 import *
@@ -977,41 +977,54 @@
 ', (
-cb
 ) =%3E 
-%7B%0A  const currentConfig = 
+new Promise((resolve, reject) =%3E %7B%0A  if (
 proc
@@ -1044,30 +1044,116 @@
 ENV 
-=
+!
 == '
-production'%0A    ?
+development') %7B%0A    // In a production environment, webpack directly to disk.%0A    const currentConfig =
  mak
@@ -1175,14 +1175,335 @@
 fig)
+;
 %0A    
-:
+webpack(currentConfig, (err, stats) =%3E %7B%0A      if (err) %7B reject(err); return; %7D%0A      if (stats) %7B logger.log(stats.toString(WEBPACK_STATS_OPTIONS)); %7D%0A      resolve();%0A    %7D);%0A  %7D else %7B%0A    // In a development environment, webpack in memory and serve%0A    // the build with webpack-dev-server.%0A    const currentConfig =
  mak
@@ -1522,17 +1522,18 @@
 onfig);%0A
-%0A
+  
   const 
@@ -1567,16 +1567,18 @@
 onfig);%0A
+  
   const 
@@ -1623,16 +1623,18 @@
 , %7B%0A    
+  
 stats: W
@@ -1659,15 +1659,19 @@
 NS,%0A
+  
   %7D);%0A%0A
+  
   //
@@ -1748,16 +1748,18 @@
 obal.%0A  
+ 
+ 
 // If we
@@ -1829,16 +1829,18 @@
 ever.%0A  
+ 
+ 
 gWebpack
@@ -1860,16 +1860,18 @@
 erver;%0A%0A
+  
   server
@@ -1879,24 +1879,26 @@
 listen(%0A    
+ 
+ 
 Endpoints.WE
@@ -1920,16 +1920,18 @@
 R_PORT,%0A
+  
     Endp
@@ -1965,14 +1965,24 @@
 ST, 
-cb);%0A%7D
+resolve);%0A  %7D%0A%7D)
 );%0A%0A
@@ -2145,44 +2145,124 @@
 p', 
+(
 cb
+)
  =%3E
+ %7B
 %0A  
-gWebpackDevServer.close(cb),%0A
+if (process.env.NODE_ENV === 'development') %7B%0A    gWebpackDevServer.close(cb);%0A  %7D else %7B%0A    cb();%0A  %7D%0A%7D
 );%0A

Fix regression caused by 3155ca5 where `npm start` would be broken