#!/usr/bin/env node
import {
  MANIFEST_TYPECHECK_CONFIG_FILE
} from "./chunk-H76MT5UR.mjs";

// src/services/project/local-typecheck.ts
import { spawn } from "child_process";
import { lstat } from "fs/promises";
import { createRequire } from "module";
import path from "path";
var TYPESCRIPT_BIN_PATH_SEGMENTS = [
  "node_modules",
  "typescript",
  "bin",
  "tsc"
];
var CLI_PACKAGE_TYPESCRIPT_CLI = resolveCliPackageTypescriptCli();
function resolveCliPackageTypescriptCli() {
  try {
    const require2 = createRequire(import.meta.url);
    return require2.resolve("typescript/bin/tsc");
  } catch {
    return null;
  }
}
function getProjectNodeModules(projectRoot) {
  return path.join(projectRoot, "node_modules");
}
function getProjectTypescriptCli(projectRoot) {
  return path.join(projectRoot, ...TYPESCRIPT_BIN_PATH_SEGMENTS);
}
async function pathExists(targetPath) {
  try {
    await lstat(targetPath);
    return true;
  } catch {
    return false;
  }
}
async function ensureTypecheckDependencies(projectRoot) {
  if (await pathExists(getProjectNodeModules(projectRoot))) {
    return null;
  }
  return `Skipping local typecheck: workspace dependencies are not installed at ${getProjectNodeModules(projectRoot)}. Run \`dreamboard sync\` to reconcile workspace dependencies first.`;
}
async function resolveTypecheckRunner(projectRoot) {
  const localTypescriptCli = getProjectTypescriptCli(projectRoot);
  if (await pathExists(localTypescriptCli)) {
    return {
      command: process.execPath,
      argsPrefix: [localTypescriptCli]
    };
  }
  if (CLI_PACKAGE_TYPESCRIPT_CLI && await pathExists(CLI_PACKAGE_TYPESCRIPT_CLI)) {
    return {
      command: process.execPath,
      argsPrefix: [CLI_PACKAGE_TYPESCRIPT_CLI]
    };
  }
  const globalTscAvailable = await new Promise((resolve) => {
    const child = spawn("tsc", ["--version"], {
      env: process.env,
      stdio: "ignore"
    });
    child.on("error", () => {
      resolve(false);
    });
    child.on("close", (code) => {
      resolve(code === 0);
    });
  });
  if (!globalTscAvailable) {
    return null;
  }
  return {
    command: "tsc",
    argsPrefix: []
  };
}
async function runTypecheckProject(runner, projectRoot, projectPath) {
  return new Promise((resolve, reject) => {
    const child = spawn(
      runner.command,
      [...runner.argsPrefix, "--noEmit", "-p", projectPath],
      {
        cwd: projectRoot,
        env: process.env,
        stdio: ["ignore", "pipe", "pipe"]
      }
    );
    let stdout = "";
    let stderr = "";
    child.stdout.on("data", (chunk) => {
      stdout += chunk.toString();
    });
    child.stderr.on("data", (chunk) => {
      stderr += chunk.toString();
    });
    child.on("error", (error) => {
      reject(new Error(`Failed to start local typecheck. ${error.message}`));
    });
    child.on("close", (code) => {
      const output = [stdout, stderr].filter(Boolean).join("\n").trim();
      resolve({
        success: code === 0,
        output
      });
    });
  });
}
async function runLocalTypecheck(projectRoot) {
  const dependencySkipReason = await ensureTypecheckDependencies(projectRoot);
  if (dependencySkipReason) {
    return {
      success: true,
      skipped: true,
      output: dependencySkipReason
    };
  }
  const runner = await resolveTypecheckRunner(projectRoot);
  if (!runner) {
    return {
      success: true,
      skipped: true,
      output: "Skipping local typecheck: TypeScript CLI was not found in workspace dependencies or on PATH."
    };
  }
  const results = await Promise.all(
    [
      MANIFEST_TYPECHECK_CONFIG_FILE,
      "app/tsconfig.json",
      "ui/tsconfig.json"
    ].map(
      (projectPath) => runTypecheckProject(runner, projectRoot, projectPath)
    )
  );
  const firstFailure = results.find((result) => !result.success);
  if (firstFailure) {
    return firstFailure;
  }
  return {
    success: true,
    output: ""
  };
}

export {
  runLocalTypecheck
};
//# sourceMappingURL=chunk-UIOLGH4A.mjs.map