"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const core_1 = require("@electron-forge/core");
const commander_1 = __importDefault(require("commander"));
const fs_extra_1 = __importDefault(require("fs-extra"));
require("./util/terminate");
const working_dir_1 = __importDefault(require("./util/working-dir"));
(async () => {
    let commandArgs = process.argv;
    let appArgs;
    const doubleDashIndex = process.argv.indexOf('--');
    if (doubleDashIndex !== -1) {
        commandArgs = process.argv.slice(0, doubleDashIndex);
        appArgs = process.argv.slice(doubleDashIndex + 1);
    }
    let dir = process.cwd();
    commander_1.default
        .version((await fs_extra_1.default.readJson(path_1.default.resolve(__dirname, '../package.json'))).version)
        .arguments('[cwd]')
        .option('-p, --app-path <path>', "Override the path to the Electron app to launch (defaults to '.')")
        .option('-l, --enable-logging', 'Enable advanced logging.  This will log internal Electron things')
        .option('-n, --run-as-node', 'Run the Electron app as a Node.JS script')
        .option('--vscode', 'Used to enable arg transformation for debugging Electron through VSCode.  Do not use yourself.')
        .option('-i, --inspect-electron', 'Triggers inspect mode on Electron to allow debugging the main process.  Electron >1.7 only')
        .option('--inspect-brk-electron', 'Triggers inspect-brk mode on Electron to allow debugging the main process.  Electron >1.7 only')
        .action((cwd) => {
        dir = (0, working_dir_1.default)(dir, cwd);
    })
        .parse(commandArgs);
    commander_1.default.on('--help', () => {
        console.log('  Any arguments found after "--" will be passed to the Electron app, e.g.');
        console.log('');
        console.log('    $ electron-forge /path/to/project -l -- -d -f foo.txt');
        console.log('');
        console.log('  will pass the arguments "-d -f foo.txt" to the Electron app');
    });
    const opts = {
        dir,
        interactive: true,
        enableLogging: !!commander_1.default.enableLogging,
        runAsNode: !!commander_1.default.runAsNode,
        inspect: !!commander_1.default.inspectElectron,
        inspectBrk: !!commander_1.default.inspectBrkElectron,
    };
    if (commander_1.default.vscode && appArgs) {
        // Args are in the format ~arg~ so we need to strip the "~"
        appArgs = appArgs.map((arg) => arg.substr(1, arg.length - 2)).filter((arg) => arg.length > 0);
    }
    if (commander_1.default.appPath)
        opts.appPath = commander_1.default.appPath;
    if (appArgs)
        opts.args = appArgs;
    const spawned = await core_1.api.start(opts);
    await new Promise((resolve) => {
        const listenForExit = (child) => {
            // Why: changing to const causes TypeScript compilation to fail.
            /* eslint-disable prefer-const */
            let onExit;
            let onRestart;
            /* eslint-enable prefer-const */
            const removeListeners = () => {
                child.removeListener('exit', onExit);
                child.removeListener('restarted', onRestart);
            };
            onExit = (code) => {
                removeListeners();
                if (spawned.restarted)
                    return;
                if (code !== 0) {
                    process.exit(code);
                }
                resolve();
            };
            onRestart = (newChild) => {
                removeListeners();
                listenForExit(newChild);
            };
            child.on('exit', onExit);
            child.on('restarted', onRestart);
        };
        listenForExit(spawned);
    });
})();
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxlY3Ryb24tZm9yZ2Utc3RhcnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvZWxlY3Ryb24tZm9yZ2Utc3RhcnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFBQSxnREFBd0I7QUFFeEIsK0NBQXlEO0FBRXpELDBEQUFnQztBQUNoQyx3REFBMEI7QUFFMUIsNEJBQTBCO0FBQzFCLHFFQUE0QztBQUU1QyxDQUFDLEtBQUssSUFBSSxFQUFFO0lBQ1YsSUFBSSxXQUFXLEdBQUcsT0FBTyxDQUFDLElBQUksQ0FBQztJQUMvQixJQUFJLE9BQU8sQ0FBQztJQUVaLE1BQU0sZUFBZSxHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQyxDQUFDO0lBQ25ELElBQUksZUFBZSxLQUFLLENBQUMsQ0FBQyxFQUFFO1FBQzFCLFdBQVcsR0FBRyxPQUFPLENBQUMsSUFBSSxDQUFDLEtBQUssQ0FBQy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