"use strict";

const gulp = require("gulp");
const run = require("gulp-run");
const runSequence = require("run-sequence");
const del = require("del");
const path = require("path");
const fs = require("fs");

const outputDir = "./wwwroot/dist";
global.aot = true;

function getEnvOptions() {
    const options = [];
    if (global.prod) {
        options.push("--env.prod");
    }
    if (global.analyse) {
        options.push("--env.analyse");
    }
    if (global.aot) {
        options.push("--env.aot");
    }
    if (options.length > 0) {
        return " " + options.join(" ");
    } else {
        return "";
    }
}

function webpack(type) {
    return run(`webpack --config webpack.config${type ? `.${type}` : ""}.ts${getEnvOptions()}`).exec();
}

gulp.task("vendor", () => {
    let build = false;
    const vendorPath = path.join(outputDir, "vendor.js");
    const vendorExists = fs.existsSync(vendorPath);
    if (vendorExists) {
        const vendorStat = fs.statSync(vendorPath);
        const packageStat = fs.statSync("package.json");
        const vendorConfigStat = fs.statSync("webpack.config.vendor.ts");
        const commonConfigStat = fs.statSync("webpack.config.common.ts");
        if (packageStat.mtime > vendorStat.mtime) {
            build = true;
        }
        if (vendorConfigStat.mtime > vendorStat.mtime) {
            build = true;
        }
        if (commonConfigStat.mtime > vendorStat.mtime) {
            build = true;
        }
    } else {
        build = true;
    }
    if (build) {
        return webpack("vendor");
    }
});

gulp.task("vendor_force", () => {
    return webpack("vendor");
})

gulp.task("main", () => {
    return webpack()
});

gulp.task("prod_var", () => {
    global.prod = true;
})

gulp.task("analyse_var", () => {
    global.analyse = true;
})

gulp.task("clean", () => {
    del.sync(outputDir, { force: true });
});

gulp.task("test_compile", function () {
    return webpack("test");
});

gulp.task("test_run", function () {
    return run("karma start ClientApp/test/karma.conf.js").exec();
});

gulp.task("test", callback => runSequence("test_compile", "test_run", callback));
gulp.task("lint", () => run("npm run lint").exec());
gulp.task("lint_fix", () => run("npm run lint -- --fix").exec());
gulp.task("build", callback => runSequence("vendor", "main", callback));
gulp.task("analyse", callback => runSequence("analyse_var", "clean", "build", callback));
gulp.task("full", callback => runSequence("clean", "build", callback));
gulp.task("publish", callback => runSequence("prod_var", "full", callback));
@@ -199,16 +199,45 @@
 e(%22fs%22);
+%0Arequire(%22ts-node/register%22);
 %0A%0Aconst 
@@ -321,21 +321,15 @@
     
-const options
+var env
  = %5B
@@ -362,36 +362,26 @@
         
-options
+env
 .push(%22
---env.
 prod%22);%0A
@@ -420,36 +420,26 @@
         
-options
+env
 .push(%22
---env.
 analyse%22
@@ -465,24 +465,199 @@
 obal.aot) %7B%0A
+        env.push(%22aot%22);%0A    %7D%0A    return env;%0A%7D%0A%0Afunction getOptions() %7B%0A    var envOptions = getEnvOptions();%0A    const options = %5B%5D;%0A    for (const option of envOptions) %7B%0A
         opti
@@ -669,19 +669,25 @@
 ush(
-%22
+%60
 --env.
-aot%22
+$%7Boption%7D%60
 );%0A 
@@ -907,19 +907,16 @@
 .ts$%7Bget
-Env
 Options(
@@ -1701,24 +1701,336 @@
 f (build) %7B%0A
+        var envOptions = getEnvOptions();%0A        var env = %7B%7D;%0A        for (const option of envOptions) %7B%0A            env%5Boption%5D = true;%0A        %7D%0A        var config = require(%22./webpack.config.vendor.ts%22)(env);%0A        if (config.entry.vendor.length) %7B // webpack will crash if given an empty entry list %0A    
         retu
@@ -2047,24 +2047,34 @@
 (%22vendor%22);%0A
+        %7D%0A
     %7D%0A%7D);%0A%0Ag

Fix webpack crashing when given an empty list in the vendor file (e.g. in prod with no css)