#!/usr/bin/env node
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const fs_1 = require("fs");
const path = __importStar(require("path"));
const bundle_1 = require("../build/bundle");
const path_1 = require("../path");
const serve_1 = require("./serve");
/* eslint-disable no-console */
process.on('uncaughtException', function (err) {
    console.error(new Date().toUTCString() + ' uncaughtException:', err.message);
    console.error(err.stack);
    process.exit(1);
});
const packageJson = JSON.parse((0, fs_1.readFileSync)(path.join(path_1.PATH_UNIT, 'package.json')).toString());
const { version } = packageJson;
const program = new commander_1.Command();
program
    .version(version)
    .option('-p, --port <number>', 'Port to run unit server on');
// "serve" as default/root command
program.action(() => {
    const { port } = program.opts();
    (0, serve_1.serve)({ port });
});
// "build" subcommand
program
    .command('build [input]')
    .description('Build from a bundle or system file. Shorthand: "unit build bundle.json"')
    .option('-b, --bundle <path>', 'Path to bundle file (e.g., bundle.json)')
    .option('-s, --system <path>', 'Path to system file (e.g., system.json)')
    .option('-o, --output <path>', 'Output directory (defaults to same dir as input)')
    .action(async (input, cmdOptions) => {
    const bundle = cmdOptions.bundle
        ? path.resolve(cmdOptions.bundle)
        : input
            ? path.resolve(input)
            : undefined;
    const system = cmdOptions.system
        ? path.resolve(cmdOptions.system)
        : undefined;
    const basisForOutput = cmdOptions.output
        ? path.resolve(cmdOptions.output)
        : bundle ?? system ?? (input ? path.resolve(input) : undefined);
    if (!basisForOutput) {
        console.error('Error: provide at least a positional [input] or --bundle or --system');
        process.exit(1);
    }
    const output = cmdOptions.output
        ? path.resolve(cmdOptions.output)
        : path.extname(basisForOutput)
            ? path.dirname(basisForOutput)
            : basisForOutput;
    await (0, bundle_1.defaultBundle)(bundle, system, output, false, {});
});
program.parse(process.argv);
//# sourceMappingURL=index.js.map