#!/usr/bin/env node
"use strict";
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var cmds = require("../cmds");
var common_1 = require("../common");
var settings_1 = require("../settings");
process.on('unhandledRejection', function (err) {
    throw err;
});
var CMD = {
    INIT: 'init',
    INIT_I: 'i',
    START: 'start [target]',
    START_ST: 'st',
    CLEAN: 'clean',
    CLEAN_C: 'c',
    BUNDLE: 'bundle [target]',
    BUNDLE_B: 'b',
    STATS: 'stats',
    DIST: 'dist [target]',
    DIST_D: 'd',
    OPEN: 'open',
    OPEN_O: 'o',
    SERVE: 'serve',
};
var CMDS = Object.keys(CMD)
    .map(function (key) { return CMD[key]; })
    .map(function (cmd) { return cmd.split(' ')[0]; });
var settings = settings_1.Settings.create('.');
var TARGETS = ['electron', 'web'];
var SCRIPT = common_1.log.magenta('uiharness');
var COMMAND = common_1.log.cyan('<command>');
var OPTIONS = common_1.log.gray('[options]');
var program = common_1.yargs
    .scriptName('')
    .usage('Usage:' + " " + SCRIPT + " " + COMMAND + " " + OPTIONS)
    .command([CMD.INIT, CMD.INIT_I], 'Initialize module with default files.', function (e) {
    return e
        .option('force', {
        alias: 'f',
        describe: 'Overwrite existing files.',
        boolean: true,
    })
        .option('reset', {
        alias: 'r',
        describe: 'Deletes all files created by a previous `init`.',
        boolean: true,
    });
}, function (e) {
    var force = e.force, reset = e.reset;
    cmds.init({ settings: settings, force: force, reset: reset });
})
    .command([CMD.START, CMD.START_ST], 'Start the development server.', function (e) {
    return e.positional('target', {
        type: 'string',
        default: 'electron',
        describe: 'Start "electron" (default) or "web" server.',
    });
}, function (e) {
    var target = formatTargetOption(e.target);
    if (!target) {
        return process.exit(1);
    }
    return cmds.start({ settings: settings, target: target });
})
    .command([CMD.CLEAN, CMD.CLEAN_C], 'Removes temporary generated files.', function (e) { return e; }, function (e) { return cmds.clean({}); })
    .command([CMD.BUNDLE, CMD.BUNDLE_B], 'Prepare the javascript bundle.', function (e) {
    return e
        .positional('target', {
        type: 'string',
        default: 'electron',
        describe: 'Build for "electron" (default) or "web" browser.',
    })
        .option('dev', {
        describe: 'Bundle for development (default: false, production).',
        alias: 'd',
        boolean: true,
    })
        .option('silent', {
        alias: 's',
        describe: 'No console output (default: false).',
        boolean: true,
    });
}, function (e) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
    var silent, _a, dev, prod, target;
    return tslib_1.__generator(this, function (_b) {
        switch (_b.label) {
            case 0:
                silent = e.silent, _a = e.dev, dev = _a === void 0 ? false : _a;
                prod = !dev;
                target = formatTargetOption(e.target);
                if (!target) {
                    return [2, process.exit(1)];
                }
                return [4, cmds.bundle({ settings: settings, prod: prod, silent: silent, target: target })];
            case 1:
                _b.sent();
                return [2, process.exit(0)];
        }
    });
}); })
    .command([CMD.DIST, CMD.DIST_D], 'Packages the application ready for distribution.', function (e) {
    return e
        .positional('target', {
        type: 'string',
        default: 'electron',
        describe: 'Build for "electron" (default) or "web" browser.',
    })
        .option('silent', {
        alias: 's',
        describe: 'No console output (default: false).',
        boolean: true,
    });
}, function (e) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
    var silent, target;
    return tslib_1.__generator(this, function (_a) {
        switch (_a.label) {
            case 0:
                silent = e.silent;
                target = formatTargetOption(e.target);
                if (!target) {
                    return [2, process.exit(1)];
                }
                return [4, cmds.dist({ settings: settings, silent: silent, target: target })];
            case 1:
                _a.sent();
                return [2, process.exit(0)];
        }
    });
}); })
    .command([CMD.STATS], 'Read size details about the distribution bundle.', function (e) {
    return e
        .option('prod', {
        alias: 'p',
        describe: 'Show for production.',
        boolean: true,
    })
        .option('target', {
        describe: 'Build for "electron" (default) or "web" browser.',
        alias: 't',
    })
        .option('dev', {
        alias: 'd',
        describe: 'Show for development.',
        boolean: true,
    });
}, function (e) {
    var dev = e.dev;
    var targets = (e.target || '')
        .split(',')
        .map(function (v) { return v; })
        .filter(function (t) { return TARGETS.includes(t); });
    var target = targets.length === 0 ? TARGETS : targets;
    var prod = e.prod && dev ? undefined : dev ? false : e.prod;
    cmds.stats({ settings: settings, prod: prod, target: target });
})
    .command([CMD.OPEN, CMD.OPEN_O], 'Opens a built application.', function (e) {
    return e.option('folder', {
        alias: 'f',
        describe: 'Open the dist folder instead of the app (default: false).',
        boolean: true,
    });
}, function (e) {
    var folder = e.folder;
    cmds.open({ settings: settings, folder: folder });
})
    .command([CMD.SERVE], 'Serve the web distribution.', function (e) { return e; }, function (e) { return cmds.serve({ settings: settings }); })
    .help('h')
    .alias('h', 'help')
    .alias('v', 'version')
    .epilog("See " + common_1.constants.URL.SITE);
if (!CMDS.includes(program.argv._[0])) {
    program.showHelp();
    common_1.log.info();
    process.exit(0);
}
function formatTargetOption(value) {
    var target = (typeof value === 'string'
        ? value.toLowerCase()
        : 'electron');
    if (!TARGETS.includes(target)) {
        var list = TARGETS.map(function (t) { return "\"" + common_1.log.cyan(t) + "\""; })
            .join(' ')
            .trim();
        var msg = '';
        msg += "\uD83D\uDE2B  The target \"" + common_1.log.yellow(target) + "\" is not supported. ";
        msg += "Must be one of " + list + ".";
        common_1.log.info(msg);
        common_1.log.info();
        return undefined;
    }
    return target;
}
//# sourceMappingURL=entry.js.map