import process from 'node:process';
import { d as defineCommand, b as legacyRootDirArgs, e as extendsArgs, a as dotEnvArgs, l as logLevelArgs, c as cwdArgs } from '../shared/nuxi.C0YEEVs2.mjs';
import { r as resolveModulePath } from '../shared/nuxi.BLb4POFS.mjs';
import { a as readTSConfig } from '../shared/nuxi.8uLzFmy0.mjs';
import { d as c } from '../shared/nuxi.DQjfoGl0.mjs';
import { x as ve } from './main.mjs';
import { l as loadKit } from '../shared/nuxi.BiWTNJI4.mjs';
import { r as resolve } from '../shared/nuxi.Do0aYBCO.mjs';
import 'node:crypto';
import 'node:path';
import 'node:tty';
import 'node:url';
import 'node:fs';
import 'node:assert';
import 'node:v8';
import 'node:util';
import 'node:fs/promises';
import 'node:module';
import 'child_process';
import 'path';
import 'process';
import 'stream';
import 'readline';

const typecheck = defineCommand({
  meta: {
    name: "typecheck",
    description: "Runs `vue-tsc` to check types throughout your app."
  },
  args: {
    ...cwdArgs,
    ...logLevelArgs,
    ...dotEnvArgs,
    ...extendsArgs,
    ...legacyRootDirArgs
  },
  async run(ctx) {
    process.env.NODE_ENV = process.env.NODE_ENV || "production";
    const cwd = resolve(ctx.args.cwd || ctx.args.rootDir);
    const [supportsProjects, resolvedTypeScript, resolvedVueTsc] = await Promise.all([
      readTSConfig(cwd).then((r) => !!r.references?.length),
      // Prefer local install if possible
      resolveModulePath("typescript", { try: true }),
      resolveModulePath("vue-tsc/bin/vue-tsc.js", { try: true }),
      writeTypes(cwd, ctx.args.dotenv, ctx.args.logLevel, {
        ...ctx.data?.overrides,
        ...ctx.args.extends && { extends: ctx.args.extends }
      })
    ]);
    const typeCheckArgs = supportsProjects ? ["-b", "--noEmit"] : ["--noEmit"];
    if (resolvedTypeScript && resolvedVueTsc) {
      return await ve(resolvedVueTsc, typeCheckArgs, {
        throwOnError: true,
        nodeOptions: {
          stdio: "inherit",
          cwd
        }
      });
    }
    if (c) {
      await ve("bun", ["install", "typescript", "vue-tsc", "--global", "--silent"], {
        throwOnError: true,
        nodeOptions: { stdio: "inherit", cwd }
      });
      return await ve("bunx", ["vue-tsc", ...typeCheckArgs], {
        throwOnError: true,
        nodeOptions: {
          stdio: "inherit",
          cwd
        }
      });
    }
    await ve("npx", ["-p", "vue-tsc", "-p", "typescript", "vue-tsc", ...typeCheckArgs], {
      throwOnError: true,
      nodeOptions: { stdio: "inherit", cwd }
    });
  }
});
async function writeTypes(cwd, dotenv, logLevel, overrides) {
  const { loadNuxt, buildNuxt, writeTypes: writeTypes2 } = await loadKit(cwd);
  const nuxt = await loadNuxt({
    cwd,
    dotenv: { cwd, fileName: dotenv },
    overrides: {
      _prepare: true,
      logLevel,
      ...overrides
    }
  });
  await writeTypes2(nuxt);
  await buildNuxt(nuxt);
  await nuxt.close();
}

export { typecheck as default };
