"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("../common");
var bundle_1 = require("./bundle");
var init_1 = require("./init");
var PATH = common_1.constants.PATH;
var ELECTRON = PATH.ELECTRON;
function dist(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var target, _a, silent, settings;
        return tslib_1.__generator(this, function (_b) {
            target = args.target, _a = args.silent, silent = _a === void 0 ? false : _a, settings = args.settings;
            switch (target) {
                case 'electron':
                    return [2, distElectron({ settings: settings, silent: silent })];
                case 'web':
                    return [2, distWeb({ settings: settings, silent: silent })];
                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.dist = dist;
function distElectron(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var settings, _a, silent, prod, electron, out, handleError, error_1, cmd, tasks, error_2, config, path;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    settings = args.settings, _a = args.silent, silent = _a === void 0 ? false : _a;
                    prod = true;
                    electron = settings.electron;
                    out = electron.out(prod);
                    process.env.NODE_ENV = 'production';
                    handleError = function (error, step) {
                        if (silent) {
                            throw error;
                        }
                        else {
                            common_1.log.info();
                            common_1.log.warn("\uD83D\uDE29  Failed while " + step + ".");
                            common_1.log.error(error.message);
                            common_1.log.info();
                            return;
                        }
                    };
                    return [4, init_1.init({ settings: settings, prod: prod })];
                case 1:
                    _b.sent();
                    if (!silent) {
                        common_1.log.info();
                        common_1.logElectronInfo({ settings: settings, port: false });
                    }
                    _b.label = 2;
                case 2:
                    _b.trys.push([2, 4, , 5]);
                    return [4, bundle_1.bundleElectron({
                            settings: settings,
                            silent: silent,
                            prod: true,
                            summary: false,
                            stats: false,
                        })];
                case 3:
                    _b.sent();
                    return [3, 5];
                case 4:
                    error_1 = _b.sent();
                    handleError(error_1, 'building javascript for electron distribution');
                    return [2];
                case 5: return [4, updateBuilderYaml({ settings: settings })];
                case 6:
                    _b.sent();
                    cmd = common_1.command()
                        .addLine("cd " + common_1.fsPath.resolve('.'))
                        .add("build")
                        .arg("--x64")
                        .arg("--publish=never")
                        .alias("-c.extraMetadata.main=\"" + out.main.path + "\"")
                        .arg("--config=\"" + ELECTRON.BUILDER.CONFIG.FILE_NAME + "\"");
                    tasks = new common_1.Listr([
                        {
                            title: "Building      " + common_1.log.yellow('electron app') + " \uD83C\uDF3C",
                            task: function () { return cmd.run({ silent: true }); },
                        },
                    ]);
                    _b.label = 7;
                case 7:
                    _b.trys.push([7, 9, , 10]);
                    return [4, tasks.run()];
                case 8:
                    _b.sent();
                    return [3, 10];
                case 9:
                    error_2 = _b.sent();
                    handleError(error_2, 'building electron distribution');
                    return [2];
                case 10:
                    config = settings.electron.builderArgs;
                    path = config.outputDir
                        ? common_1.logging.formatPath(config.outputDir, true)
                        : 'UNKNOWN';
                    if (!silent) {
                        common_1.log.info();
                        common_1.log.info("\uD83E\uDD1F  Application packaging complete.\n");
                        common_1.log.info.gray("   \u2022 productName: " + common_1.log.yellow(config.productName));
                        common_1.log.info.gray("   \u2022 version:     " + settings.package.version);
                        common_1.log.info.gray("   \u2022 appId:       " + config.appId);
                        common_1.log.info.gray("   \u2022 folder:      " + path);
                        common_1.log.info();
                        common_1.log.info("\uD83D\uDC49  Run " + common_1.log.cyan('yarn ui open') + " to run it.");
                        common_1.log.info();
                    }
                    return [2];
            }
        });
    });
}
exports.distElectron = distElectron;
function updateBuilderYaml(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var settings, electron, BUILDER, filename, path, data;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    settings = args.settings;
                    electron = settings.electron;
                    BUILDER = ELECTRON.BUILDER;
                    filename = BUILDER.CONFIG.FILE_NAME;
                    path = common_1.fsPath.resolve(common_1.fsPath.join('.', filename));
                    return [4, common_1.file.loadAndParse(path)];
                case 1:
                    data = _a.sent();
                    data.productName = electron.name;
                    data.files = BUILDER.FILES;
                    data.directories = tslib_1.__assign({}, (data.directories || {}), { output: BUILDER.OUTPUT });
                    return [4, common_1.file.stringifyAndSave(path, data)];
                case 2:
                    _a.sent();
                    return [2];
            }
        });
    });
}
function distWeb(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var settings, silent, prod;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    settings = args.settings, silent = args.silent;
                    prod = true;
                    return [4, bundle_1.bundleWeb({ settings: settings, prod: prod, silent: silent })];
                case 1:
                    _a.sent();
                    common_1.log.info("\uD83D\uDC49  Run " + common_1.log.cyan('yarn ui serve') + " to view it in the browser.");
                    common_1.log.info();
                    return [2];
            }
        });
    });
}
exports.distWeb = distWeb;
//# sourceMappingURL=dist.js.map