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,
    hostname: typeof flags.hostname === "string" ? flags.hostname : void 0,
    experimentalStaticBuild: typeof flags.experimentalStaticBuild === "boolean" ? flags.experimentalStaticBuild : false
  };
  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.log(`  ${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).
  --experimental-static-build A more performant build that expects assets to be define statically.
  --verbose             Enable verbose logging
  --silent              Disable logging
  --version             Show the version number and exit.
  --help                Show this help message.
`);
}
async function printVersion() {
  const pkgURL = new URL("../../package.json", import.meta.url);
  const pkg = JSON.parse(await fs.promises.readFile(pkgURL, "utf8"));
  const pkgVersion = pkg.version;
  console.log(pkgVersion);
}
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;
  if (typeof flags.experimentalStaticBuild === "boolean")
    astroConfig.buildOptions.experimentalStaticBuild = flags.experimentalStaticBuild;
}
async function cli(args) {
  const flags = yargs(args);
  const state = resolveArgs(flags);
  const options = __spreadValues({}, state.options);
  const projectRoot = options.projectRoot || flags._[3];
  switch (state.cmd) {
    case "help":
      printHelp();
      return process.exit(0);
    case "version":
      await printVersion();
      return process.exit(0);
  }
  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.error(formatConfigError(err));
    } else {
      console.error(colors.red(err.toString() || err));
    }
    process.exit(1);
  }
  switch (state.cmd) {
    case "dev": {
      try {
        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);
      return process.exit(ret);
    }
    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,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2NsaS9pbmRleC50cyJdLAogICJtYXBwaW5ncyI6ICI7Ozs7Ozs7Ozs7Ozs7Ozs7QUFLQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQWtCQSxxQkFBcUIsT0FBNEI7QUFDaEQsUUFBTSxVQUErQjtBQUFBLElBQ3BDLGFBQWEsT0FBTyxNQUFNLGdCQUFnQixXQUFXLE1BQU0sY0FBYztBQUFBLElBQ3pFLE1BQU0sT0FBTyxNQUFNLFNBQVMsV0FBVyxNQUFNLE9BQU87QUFBQSxJQUNwRCxTQUFTLE9BQU8sTUFBTSxZQUFZLFlBQVksTUFBTSxVQUFVO0FBQUEsSUFDOUQsTUFBTSxPQUFPLE1BQU0sU0FBUyxXQUFXLE1BQU0sT0FBTztBQUFBLElBQ3BELFFBQVEsT0FBTyxNQUFNLFdBQVcsV0FBVyxNQUFNLFNBQVM7QUFBQSxJQUMxRCxVQUFVLE9BQU8sTUFBTSxhQUFhLFdBQVcsTUFBTSxXQUFXO0FBQUEsSUFDaEUseUJBQXlCLE9BQU8sTUFBTSw0QkFBNEIsWUFBWSxNQUFNLDBCQUEwQjtBQUFBO0FBRy9HLE1BQUksTUFBTSxTQUFTO0FBQ2xCLFdBQU8sRUFBRSxLQUFLLFdBQVc7QUFBQSxhQUNmLE1BQU0sTUFBTTtBQUN0QixXQUFPLEVBQUUsS0FBSyxRQUFRO0FBQUE7QUFHdkIsUUFBTSxNQUFNLE1BQU0sRUFB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