"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runQuirrelDev = void 0;
const api_1 = require("../../api");
const ioredis_1 = __importDefault(require("ioredis"));
const create_redis_1 = require("../../api/shared/create-redis");
const cron_detector_1 = require("../cron-detector");
const config_1 = require("../../client/config");
function requireFrameworkClientForDevelopmentDefaults(framework) {
    require(`../../${framework}`);
}
async function isRedisConnectionIntact(redisUrl) {
    try {
        const client = new ioredis_1.default(redisUrl);
        await client.ping();
        return true;
    }
    catch (error) {
        return false;
    }
}
function collect(value, previous = []) {
    return previous.concat([value]);
}
async function runQuirrelDev(config) {
    const { redisUrl, cronCwd } = config;
    if (redisUrl) {
        if (!(await isRedisConnectionIntact(redisUrl))) {
            throw new Error("Couldn't connect to Redis.");
        }
    }
    const quirrel = await (0, api_1.runQuirrel)({
        ...config,
        redisFactory: (0, create_redis_1.createRedisFactory)(redisUrl, {
            tlsCa: {
                base64: process.env.REDIS_TLS_CA_BASE64,
                path: process.env.REDIS_TLS_CA_FILE,
            },
        }),
    });
    let cronDetector;
    if (cronCwd) {
        cronDetector = new cron_detector_1.CronDetector(cronCwd, async (jobs) => {
            const firstJob = jobs[0];
            if (firstJob) {
                requireFrameworkClientForDevelopmentDefaults(firstJob.framework);
            }
            await quirrel.server.app.jobs.updateCron("anonymous", {
                baseUrl: (0, config_1.getApplicationBaseUrl)(config.host),
                crons: jobs,
            });
        });
        await cronDetector.awaitReady();
    }
    return async () => {
        await quirrel.close();
        await (cronDetector === null || cronDetector === void 0 ? void 0 : cronDetector.close());
    };
}
exports.runQuirrelDev = runQuirrelDev;
function registerRun(program) {
    program
        .option("-h, --host <host>", "host to bind on", "localhost")
        .option("-p, --port <port>", "port to bind on", "9181")
        .option("-r, --redis-url <redis-url>", "enables the redis backend")
        .option("-q, --quiet", "silences welcome message, condenses output", false)
        .option("--no-cron", "Disable cron job detection")
        .option("--passphrase <passphrase>", "secure the server with a passphrase", collect)
        .action(async ({ redisUrl, passphrase, host, port, cron, quiet, }) => {
        const exit = await runQuirrelDev({
            redisUrl,
            cronCwd: cron ? process.cwd() : undefined,
            passphrases: passphrase,
            host,
            port: Number(port),
            disableTelemetry: Boolean(process.env.DISABLE_TELEMETRY),
            logger: quiet ? "quiet" : "dx",
        });
        process.on("SIGINT", async () => {
            await exit();
            process.exit();
        });
    });
}
exports.default = registerRun;
//# sourceMappingURL=index.js.map