// src/pm/detectReact.ts
import { readFileSync as readFileSync2 } from "node:fs";
import PATH2 from "node:path";
import chalk from "chalk";
import { globSync as globSync2 } from "glob";

// src/pm/detectPackageManager.ts
import { existsSync as existsSync2 } from "node:fs";

// src/pm/workspaces.ts
import { existsSync, readFileSync } from "node:fs";
import PATH from "node:path";
import { globSync } from "glob";
function readJsonFile(path) {
  if (!existsSync(path)) return void 0;
  try {
    return JSON.parse(readFileSync(path, "utf8"));
  } catch {
    return void 0;
  }
}
function readRootPackageJson(cwd = process.cwd()) {
  return readJsonFile(PATH.resolve(cwd, "package.json"));
}
function detectPackageManagerFromPackageJson(cwd = process.cwd()) {
  const packageManager = readRootPackageJson(cwd)?.packageManager;
  if (!(packageManager != null && packageManager.length > 0)) return void 0;
  return packageManager.split("@", 1)[0];
}

// src/pm/detectPackageManager.ts
var knownPackageManagers = /* @__PURE__ */ new Set(["bun", "npm", "pnpm", "yarn"]);
function detectPackageManager() {
  const packageManager = detectPackageManagerFromPackageJson();
  if (packageManager != null && packageManager.length > 0 && knownPackageManagers.has(packageManager)) {
    return packageManager;
  }
  if (existsSync2("pnpm-lock.yaml") || existsSync2("pnpm-workspace.yaml")) return "pnpm";
  if (existsSync2("bun.lock") || existsSync2("bun.lockb")) return "bun";
  if (existsSync2("yarn.lock") || existsSync2(".yarnrc.yml")) return "yarn";
  if (existsSync2("package-lock.json") || existsSync2("npm-shrinkwrap.json")) return "npm";
  return "yarn";
}

// src/pm/registry.ts
var implementations = /* @__PURE__ */ new Map();
function getPackageManager(name) {
  const pmName = name ?? detectPackageManager();
  const pm = implementations.get(pmName);
  if (!pm) {
    throw new Error(
      `No package manager implementation registered for "${pmName}". Ensure registerPackageManager() has been called before getPackageManager().`
    );
  }
  return pm;
}

// src/pm/detectReact.ts
var cached;
function hasReactDependency(packageJsonPath) {
  try {
    const content = readFileSync2(packageJsonPath, "utf8");
    const pkg = JSON.parse(content);
    const allDeps = {
      ...pkg.dependencies,
      ...pkg.devDependencies,
      ...pkg.peerDependencies
    };
    return Object.hasOwn(allDeps, "react") || Object.hasOwn(allDeps, "react-dom");
  } catch {
    return false;
  }
}
function workspaceHasReact(workspace) {
  return hasReactDependency(PATH2.join(workspace.location, "package.json"));
}
function detectReactInMonorepo(cwd, verbose) {
  const rootPkgPath = PATH2.resolve(cwd, "package.json");
  if (hasReactDependency(rootPkgPath)) {
    if (verbose === true) console.log(chalk.gray("  React detected in package.json"));
    return true;
  }
  const packageJsonPaths = globSync2("packages/**/package.json", {
    cwd,
    ignore: ["**/node_modules/**"]
  });
  for (const relPath of packageJsonPaths) {
    const fullPath = PATH2.resolve(cwd, relPath);
    if (hasReactDependency(fullPath)) {
      if (verbose === true) console.log(chalk.gray(`  React detected in ${relPath}`));
      return true;
    }
  }
  return false;
}
function detectReact() {
  if (cached !== void 0) return cached;
  try {
    const pm = getPackageManager();
    const workspaces = pm.listWorkspaces();
    cached = workspaces.some(workspaceHasReact);
  } catch {
    cached = false;
  }
  return cached;
}
export {
  detectReact,
  detectReactInMonorepo
};
//# sourceMappingURL=detectReact.mjs.map
