"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = run;
const util_1 = require("util");
const utils_1 = require("../utils");
const constants_1 = require("./sort-gff-utils/constants");
const process_utils_1 = require("./sort-gff-utils/process-utils");
const sort_utils_1 = require("./sort-gff-utils/sort-utils");
const validators_1 = require("./sort-gff-utils/validators");
async function run(args) {
    const options = {
        help: {
            type: 'boolean',
            short: 'h',
        },
    };
    const { values: flags, positionals } = (0, util_1.parseArgs)({
        args,
        options,
        allowPositionals: true,
    });
    const description = constants_1.SORT_GFF_DESCRIPTION;
    const examples = constants_1.SORT_GFF_EXAMPLES;
    if (flags.help) {
        (0, utils_1.printHelp)({
            description,
            examples,
            usage: 'jbrowse sort-gff [file] [options]',
            options,
        });
        return;
    }
    const file = positionals[0];
    if (file) {
        (0, validators_1.validateFileArgument)(file);
    }
    (0, validators_1.validateRequiredCommands)();
    try {
        const child = (0, sort_utils_1.spawnSortProcess)({ file: file || '-' });
        const exitCode = await (0, process_utils_1.waitForProcessClose)(child);
        if (exitCode !== 0) {
            console.error(`Sort process exited with code ${exitCode}`);
            process.exit(exitCode || 1);
        }
    }
    catch (error) {
        (0, process_utils_1.handleProcessError)(error);
    }
}
