"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cliWithConfig = void 0;
const commander_1 = require("commander");
const package_json_1 = __importDefault(require("../../package.json"));
const js_yaml_1 = __importDefault(require("js-yaml"));
const fs_1 = require("fs");
async function readYaml(filename) {
    const contents = await fs_1.promises.readFile(filename, { encoding: "utf-8" });
    const result = js_yaml_1.default.load(contents);
    return result;
}
async function cliWithConfig(doIt) {
    process.on("unhandledRejection", (reason, promise) => {
        console.error("Unhandled Rejection at:", promise, "reason:", reason);
        process.exit(1);
    });
    commander_1.program
        .version(package_json_1.default.version)
        .option("-c, --config <path>", "path to config file (json or yaml)");
    commander_1.program.parse(process.argv);
    const { config: configLocation } = commander_1.program.opts();
    const { teardown } = await doIt(!!configLocation ? await readYaml(configLocation) : process.env);
    async function _teardown(signal) {
        console.log("Received %s - terminating ...", signal);
        await teardown();
        process.exit(2);
    }
    process.on("SIGTERM", _teardown);
    process.on("SIGINT", _teardown);
}
exports.cliWithConfig = cliWithConfig;
//# sourceMappingURL=cliWithConfig.js.map