var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
import * as colors from "kleur/colors";
import fs from "fs";
import yargs from "yargs-parser";
import { z } from "zod";
import { defaultLogDestination } from "../core/logger.js";
import build from "../core/build/index.js";
import devServer from "../core/dev/index.js";
import preview from "../core/preview/index.js";
import { check } from "./check.js";
import { formatConfigError, loadConfig } from "../core/config.js";
function resolveArgs(flags) {
  const options = {
    projectRoot: typeof flags.projectRoot === "string" ? flags.projectRoot : void 0,
    site: typeof flags.site === "string" ? flags.site : void 0,
    sitemap: typeof flags.sitemap === "boolean" ? flags.sitemap : void 0,
    port: typeof flags.port === "number" ? flags.port : void 0,
    config: typeof flags.config === "string" ? flags.config : void 0
  };
  if (flags.version) {
    return { cmd: "version", options };
  } else if (flags.help) {
    return { cmd: "help", options };
  }
  const cmd = flags._[2];
  switch (cmd) {
    case "dev":
      return { cmd: "dev", options };
    case "build":
      return { cmd: "build", options };
    case "preview":
      return { cmd: "preview", options };
    case "check":
      return { cmd: "check", options };
    default:
      return { cmd: "help", options };
  }
}
function printHelp() {
  console.error(`  ${colors.bold("astro")} - Futuristic web development tool.
  ${colors.bold("Commands:")}
  astro dev             Run Astro in development mode.
  astro build           Build a pre-compiled production version of your site.
  astro preview         Preview your build locally before deploying.
  astro check           Check your project for errors.

  ${colors.bold("Flags:")}
  --config <path>       Specify the path to the Astro config file.
  --project-root <path> Specify the path to the project root folder.
  --no-sitemap          Disable sitemap generation (build only).
  --verbose             Enable verbose logging
  --silent              Disable logging
  --version             Show the version number and exit.
  --help                Show this help message.
`);
}
async function printVersion() {
  const pkg = JSON.parse(await fs.promises.readFile(new URL("../package.json", import.meta.url), "utf8"));
  console.error(pkg.version);
}
function mergeCLIFlags(astroConfig, flags) {
  if (typeof flags.sitemap === "boolean")
    astroConfig.buildOptions.sitemap = flags.sitemap;
  if (typeof flags.site === "string")
    astroConfig.buildOptions.site = flags.site;
  if (typeof flags.port === "number")
    astroConfig.devOptions.port = flags.port;
  if (typeof flags.hostname === "string")
    astroConfig.devOptions.hostname = flags.hostname;
}
async function cli(args) {
  const flags = yargs(args);
  const state = resolveArgs(flags);
  const options = __spreadValues({}, state.options);
  const projectRoot = options.projectRoot || flags._[3];
  let logging = {
    dest: defaultLogDestination,
    level: "info"
  };
  if (flags.verbose)
    logging.level = "debug";
  if (flags.silent)
    logging.level = "silent";
  let config;
  try {
    config = await loadConfig({ cwd: projectRoot, filename: options.config });
    mergeCLIFlags(config, options);
  } catch (err) {
    if (err instanceof z.ZodError) {
      console.log(formatConfigError(err));
    } else {
      console.error(colors.red(err.toString() || err));
    }
    process.exit(1);
  }
  switch (state.cmd) {
    case "help": {
      printHelp();
      process.exit(1);
      return;
    }
    case "version": {
      await printVersion();
      process.exit(0);
      return;
    }
    case "dev": {
      try {
        const server = await devServer(config, { logging });
        await new Promise(() => {
        });
      } catch (err) {
        throwAndExit(err);
      }
      return;
    }
    case "build": {
      try {
        await build(config, { logging });
        process.exit(0);
      } catch (err) {
        throwAndExit(err);
      }
      return;
    }
    case "check": {
      const ret = await check(config);
      process.exit(ret);
      return;
    }
    case "preview": {
      try {
        await preview(config, { logging });
      } catch (err) {
        throwAndExit(err);
      }
      return;
    }
    default: {
      throw new Error(`Error running ${state.cmd}`);
    }
  }
}
function throwAndExit(err) {
  console.error(colors.red(err.toString() || err));
  process.exit(1);
}
export {
  cli
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJza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