#!/usr/bin/env node
"use strict";
/* eslint-disable no-console */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const openurl_1 = __importDefault(require("openurl"));
const yargs_1 = __importDefault(require("yargs"));
const helpers_1 = require("yargs/helpers");
const localtunnel_1 = __importDefault(require("../localtunnel"));
const { version } = require('../../package.json');
const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
    .usage('Usage: lt --port [num] <options>')
    .env(true)
    .option('p', {
    alias: 'port',
    describe: 'Internal HTTP server port',
    type: 'number',
    demandOption: true,
})
    .option('h', {
    alias: 'host',
    describe: 'Upstream server providing forwarding',
    default: 'https://localtunnel.me',
    type: 'string',
})
    .option('s', {
    alias: 'subdomain',
    describe: 'Request this subdomain',
    type: 'string',
})
    .option('l', {
    alias: 'local-host',
    describe: 'Tunnel traffic to this host instead of localhost, override Host header to this host',
    type: 'string',
})
    .option('local-https', {
    describe: 'Tunnel traffic to a local HTTPS server',
    type: 'boolean',
})
    .option('local-cert', {
    describe: 'Path to certificate PEM file for local HTTPS server',
    type: 'string',
})
    .option('local-key', {
    describe: 'Path to certificate key file for local HTTPS server',
    type: 'string',
})
    .option('local-ca', {
    describe: 'Path to certificate authority file for self-signed certificates',
    type: 'string',
})
    .option('allow-invalid-cert', {
    describe: 'Disable certificate checks for your local HTTPS server (ignore cert/key/ca options)',
    type: 'boolean',
})
    .options('o', {
    alias: 'open',
    describe: 'Opens the tunnel URL in your browser',
    type: 'boolean',
})
    .option('print-requests', {
    describe: 'Print basic request info',
    type: 'boolean',
})
    .boolean('local-https')
    .boolean('allow-invalid-cert')
    .boolean('print-requests')
    .help('help', 'Show this help and exit')
    .version(version)
    .parseSync();
if (typeof argv.port !== 'number') {
    yargs_1.default.showHelp();
    console.error('\nInvalid argument: `port` must be a number');
    process.exit(1);
}
(async () => {
    try {
        const tunnel = await (0, localtunnel_1.default)({
            port: argv.port,
            host: argv.host,
            subdomain: argv.subdomain,
            local_host: argv.localHost,
            local_https: argv.localHttps,
            local_cert: argv.localCert,
            local_key: argv.localKey,
            local_ca: argv.localCa,
            allow_invalid_cert: argv.allowInvalidCert,
        });
        tunnel.on('error', err => {
            throw err;
        });
        console.log('your url is: %s', tunnel.url);
        /**
         * `cachedUrl` is set when using a proxy server that support resource caching.
         * This URL generally remains available after the tunnel itself has closed.
         * @see https://github.com/localtunnel/localtunnel/pull/319#discussion_r319846289
         */
        if (tunnel.cachedUrl) {
            console.log('your cachedUrl is: %s', tunnel.cachedUrl);
        }
        if (argv.open) {
            openurl_1.default.open(tunnel.url);
        }
        if (argv['print-requests']) {
            tunnel.on('request', info => {
                console.log(new Date().toString(), info.method, info.path);
            });
        }
    }
    catch (err) {
        console.error('Error occurred:', err);
        process.exit(1);
    }
})();
