"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("../common");
var init_1 = require("./init");
var stats_1 = require("./stats");
function bundle(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var target, _a, silent, prod, settings, summary;
        return tslib_1.__generator(this, function (_b) {
            target = args.target, _a = args.silent, silent = _a === void 0 ? false : _a, prod = args.prod, settings = args.settings, summary = args.summary;
            switch (target) {
                case 'electron':
                    return [2, bundleElectron({ settings: settings, prod: prod, silent: silent, summary: summary })];
                case 'web':
                    return [2, bundleWeb({ settings: settings, prod: prod, silent: silent, summary: summary })];
                default:
                    if (!silent) {
                        common_1.log.info();
                        common_1.log.warn("\uD83D\uDE29  The target \"" + common_1.log.yellow(target) + "\" is not supported.");
                        common_1.log.info();
                    }
                    return [2, { success: false }];
            }
            return [2];
        });
    });
}
exports.bundle = bundle;
function bundleElectron(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var settings, prod, _a, silent, summary, stats, env, main, renderer, pkg, electron, entry, bundlerArgs, out, all, tasks, cmd, error_1;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    settings = args.settings, prod = args.prod, _a = args.silent, silent = _a === void 0 ? false : _a;
                    summary = common_1.value.defaultValue(args.summary, true);
                    stats = common_1.value.defaultValue(args.stats, true);
                    env = toEnv(prod);
                    main = args.main, renderer = args.renderer;
                    pkg = settings.package;
                    electron = settings.electron;
                    entry = electron.entry;
                    bundlerArgs = electron.bundlerArgs;
                    out = electron.out(prod);
                    all = main === undefined && renderer === undefined;
                    main = all ? true : main;
                    renderer = all ? true : renderer;
                    return [4, init_1.init({ settings: settings, prod: prod })];
                case 1:
                    _b.sent();
                    tasks = new common_1.Listr([], {
                        concurrent: true,
                        renderer: silent ? 'silent' : undefined,
                    });
                    cmd = common_1.command()
                        .addLine("export NODE_ENV=\"" + env.value + "\"")
                        .addLine("cd " + common_1.fsPath.resolve('.'));
                    if (main) {
                        tasks.add({
                            title: "Bundling      " + common_1.log.cyan('main') + "     " + env.display,
                            task: function () {
                                return cmd
                                    .add("parcel")
                                    .add("build " + entry.main)
                                    .arg("--out-dir " + out.main.dir)
                                    .arg("--out-file " + out.main.file)
                                    .arg("--target electron")
                                    .add(bundlerArgs.cmd)
                                    .run({ silent: true });
                            },
                        });
                    }
                    if (renderer) {
                        tasks.add({
                            title: "Bundling      " + common_1.log.cyan('renderer') + " " + env.display,
                            task: function () {
                                return cmd
                                    .add("parcel")
                                    .add("build " + entry.renderer)
                                    .add("--public-url ./")
                                    .arg("--out-dir " + out.renderer.dir)
                                    .arg("--out-file " + out.renderer.file)
                                    .add(bundlerArgs.cmd)
                                    .run({ silent: true });
                            },
                        });
                    }
                    _b.label = 2;
                case 2:
                    _b.trys.push([2, 4, , 5]);
                    return [4, tasks.run()];
                case 3:
                    _b.sent();
                    return [3, 5];
                case 4:
                    error_1 = _b.sent();
                    if (!silent) {
                        common_1.log.info();
                        common_1.log.warn("\uD83D\uDE29  Failed while bundling for " + common_1.log.magenta('electron') + ".");
                        common_1.log.error(error_1.message);
                        common_1.log.info();
                    }
                    return [2, { success: false, error: error_1 }];
                case 5:
                    if (summary && !silent) {
                        common_1.log.info();
                        common_1.log.info("\uD83E\uDD1F  Javascript bundling for " + common_1.log.yellow('electron') + " complete.\n");
                        common_1.log.info.gray("   \u2022 package:     " + pkg.name);
                        common_1.log.info.gray("   \u2022 version:     " + pkg.version);
                        common_1.log.info.gray("   \u2022 env:         " + env.value);
                        common_1.log.info.gray("   \u2022 entry:       " + formatPath(entry.main));
                        common_1.log.info.gray("                  " + formatPath(entry.renderer));
                        common_1.log.info.gray("   \u2022 output:      " + formatPath(out.main.path));
                        common_1.log.info.gray("                  " + formatPath(out.renderer.path));
                        common_1.log.info();
                    }
                    if (!(stats && !silent)) return [3, 7];
                    return [4, stats_1.stats({ settings: settings, prod: prod, target: 'electron' })];
                case 6:
                    _b.sent();
                    _b.label = 7;
                case 7: return [2, { success: true }];
            }
        });
    });
}
exports.bundleElectron = bundleElectron;
function bundleWeb(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var settings, prod, _a, silent, summary, stats, env, pkg, web, entry, out, tasks, cmd, error_2;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    settings = args.settings, prod = args.prod, _a = args.silent, silent = _a === void 0 ? false : _a;
                    summary = common_1.value.defaultValue(args.summary, true);
                    stats = common_1.value.defaultValue(args.stats, true);
                    env = toEnv(prod);
                    pkg = settings.package;
                    web = settings.web;
                    entry = web.entry;
                    out = web.out(prod);
                    return [4, init_1.init({ settings: settings, prod: prod })];
                case 1:
                    _b.sent();
                    tasks = new common_1.Listr([], {
                        concurrent: true,
                        renderer: silent ? 'silent' : undefined,
                    });
                    cmd = common_1.command()
                        .addLine("export NODE_ENV=\"" + env.value + "\"")
                        .addLine("cd " + common_1.fsPath.resolve('.'))
                        .add("parcel")
                        .add("build " + entry)
                        .add("--public-url ./")
                        .arg("--out-dir " + out.dir)
                        .arg("--out-file " + out.file)
                        .add(web.bundlerArgs.cmd);
                    tasks.add({
                        title: "Bundling      " + common_1.log.cyan('web') + " " + env.display,
                        task: function () { return cmd.run({ silent: true }); },
                    });
                    _b.label = 2;
                case 2:
                    _b.trys.push([2, 4, , 5]);
                    return [4, tasks.run()];
                case 3:
                    _b.sent();
                    return [3, 5];
                case 4:
                    error_2 = _b.sent();
                    if (!silent) {
                        common_1.log.info();
                        common_1.log.warn("\uD83D\uDE29  Failed while bundling for " + common_1.log.magenta('web') + ".");
                        common_1.log.error(error_2.message);
                        common_1.log.info();
                    }
                    return [2, { success: false, error: error_2 }];
                case 5:
                    if (summary && !silent) {
                        common_1.log.info();
                        common_1.log.info("\uD83E\uDD1F  Javascript bundling for " + common_1.log.yellow('web') + " complete.\n");
                        common_1.log.info.gray("   \u2022 package:     " + pkg.name);
                        common_1.log.info.gray("   \u2022 version:     " + pkg.version);
                        common_1.log.info.gray("   \u2022 env:         " + env.value);
                        common_1.log.info.gray("   \u2022 entry:       " + formatPath(entry));
                        common_1.log.info.gray("   \u2022 output:      " + formatPath(out.path));
                        common_1.log.info();
                    }
                    if (!stats) return [3, 7];
                    return [4, stats_1.stats({ settings: settings, prod: prod, target: 'web' })];
                case 6:
                    _b.sent();
                    _b.label = 7;
                case 7: return [2, { success: true }];
            }
        });
    });
}
exports.bundleWeb = bundleWeb;
var formatPath = function (path) { return common_1.logging.formatPath(path, true); };
function toEnv(prod) {
    var value = prod === true || prod === undefined ? 'production' : 'development';
    var display = common_1.log.gray("(" + value + ")");
    return { value: value, display: display };
}
//# sourceMappingURL=bundle.js.map