const chalk = require("chalk");
const webpack = require("webpack");
const env = require("./env");
const webpackConfig = require("./webpack.config.dev");
const DevServer = require("webpack-dev-server");

process.env.NODE_ENV = "development";
process.on("unhandledRejection", (error) => {
    throw error;
});

function server(compiler) {
    const rewrites = [];
    Object.keys(env.packageJSON.config.pages).forEach((name) => {
        rewrites.push({
            from: new RegExp(`/${name}`),
            to: `/${name}.html`
        });
    });
    return new DevServer(compiler, {
        contentBase: env.static,
        https: true,
        historyApiFallback: {rewrites: rewrites},
        hot: true,
        inline: true,
        compress: true,
        stats: "minimal",
        overlay: {
            warnings: true,
            errors: true
        },
        proxy: {
            "/ajax": {
                target: "https://localhost:8443",
                secure: false,
                changeOrigin: true
            }
        }
    });
}

function start() {
    const compiler = webpack(webpackConfig);
    const devServer = server(compiler);
    devServer.listen(7443, "localhost", (err) => {
        if (err) {
            return console.log(err);
        }
        console.log("starting the development server on", chalk.green("https://localhost:7443/\n"));
        return null;
    });

    ["SIGINT", "SIGTERM"].forEach((sig) => {
        process.on(sig, () => {
            devServer.close();
            process.exit();
        });
    });
}

start();

@@ -311,17 +311,20 @@
 unction 
-s
+devS
 erver(co
@@ -1126,20 +1126,17 @@
 nst 
-devS
+s
 erver = 
 serv
@@ -1131,17 +1131,20 @@
 erver = 
-s
+devS
 erver(co
@@ -1152,28 +1152,25 @@
 piler);%0A    
-devS
+s
 erver.listen
@@ -1193,16 +1193,18 @@
 t%22, (err
+or
 ) =%3E %7B%0A 
@@ -1217,16 +1217,18 @@
  if (err
+or
 ) %7B%0A    
@@ -1239,23 +1239,16 @@
     
-return 
 console.
 log(
@@ -1247,15 +1247,48 @@
 ole.
-log(err
+error(error);%0A            process.exit(1
 );%0A 
@@ -1330,23 +1330,11 @@
 ing 
-the development
+dev
  ser
@@ -1453,16 +1453,19 @@
 ach((sig
+nal
 ) =%3E %7B%0A 
@@ -1485,16 +1485,19 @@
 s.on(sig
+nal
 , () =%3E 
@@ -1510,20 +1510,17 @@
         
-devS
+s
 erver.cl

refactor start script