#!/usr/bin/env node
import process from "node:process";
import { importConfiguration } from "./import.js";
import { join } from "node:path";
import { ok } from "../../../is.js";
function isURL(value) {
    try {
        new URL(value);
        return true;
    }
    catch {
        return false;
    }
}
async function read(stream) {
    const chunks = [];
    for await (const chunk of stream)
        chunks.push(chunk);
    return Buffer.concat(chunks).toString("utf8");
}
process.on("unhandledRejection", console.error);
const argv = [...process.argv];
let configUrl;
if (argv.includes("--stdin")) {
    configUrl = (await read(process.stdin)).trim();
}
else {
    configUrl = argv.pop().trim();
}
if ((configUrl.startsWith("'") && configUrl.endsWith("'")) || (configUrl.startsWith(`"`) && configUrl.endsWith(`"`))) {
    configUrl = configUrl.slice(0, configUrl.length - 2);
}
if (configUrl.startsWith("{") && configUrl.endsWith("}")) {
    const config = JSON.parse(configUrl);
    ok(config.url, "No url given in config");
    if (config.url.startsWith("./")) {
        config.url = join(process.cwd(), config.url);
    }
    if (!config.url.startsWith("file://")) {
        config.url = new URL(config.url, `file://${process.cwd()}`).toString();
    }
    configUrl = config;
}
else if (!configUrl.startsWith("/") && !isURL(configUrl)) {
    configUrl = join(process.cwd(), configUrl);
}
try {
    await importConfiguration(configUrl);
}
catch (error) {
    console.error(error);
    process.exit(1);
}
//# sourceMappingURL=main.js.map