import { runQuirrel } from "../../api";
import IORedis from "ioredis";
import { createRedisFactory } from "../../api/shared/create-redis";
import { CronDetector } from "../cron-detector";
import { getApplicationBaseUrl } from "../../client/config";
function requireFrameworkClientForDevelopmentDefaults(framework) {
    require(`../../${framework}`);
}
async function isRedisConnectionIntact(redisUrl) {
    try {
        const client = new IORedis(redisUrl);
        await client.ping();
        return true;
    }
    catch (error) {
        return false;
    }
}
function collect(value, previous = []) {
    return previous.concat([value]);
}
export 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 runQuirrel({
        ...config,
        redisFactory: createRedisFactory(redisUrl, {
            tlsCa: {
                base64: process.env.REDIS_TLS_CA_BASE64,
                path: process.env.REDIS_TLS_CA_FILE,
            },
        }),
    });
    let cronDetector;
    if (cronCwd) {
        cronDetector = new CronDetector(cronCwd, async (jobs) => {
            const firstJob = jobs[0];
            if (firstJob) {
                requireFrameworkClientForDevelopmentDefaults(firstJob.framework);
            }
            await quirrel.server.app.jobs.updateCron("anonymous", {
                baseUrl: getApplicationBaseUrl(config.host),
                crons: jobs,
            });
        });
        await cronDetector.awaitReady();
    }
    return async () => {
        await quirrel.close();
        await (cronDetector === null || cronDetector === void 0 ? void 0 : cronDetector.close());
    };
}
export default 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();
        });
    });
}
//# sourceMappingURL=index.js.map