// src/actions/clean-eslint.ts
import path3 from "node:path";
import chalk2 from "chalk";

// src/lib/deleteGlob.ts
import fs from "node:fs";
import { glob } from "glob";
var deleteGlob = (globPath) => {
  const files = glob.sync(globPath);
  for (const file of files) {
    fs.rmSync(file, { recursive: true, force: true });
  }
};

// src/pm/resolveWorkspace.ts
import chalk from "chalk";
function shortName(name) {
  const slash = name.lastIndexOf("/");
  return slash === -1 ? name : name.slice(slash + 1);
}
function resolveWorkspace(workspaces, query) {
  const exact = workspaces.find((ws) => ws.name === query);
  if (exact) return exact;
  const byLocation = workspaces.find((ws) => ws.location === query);
  if (byLocation) return byLocation;
  const byShort = workspaces.filter((ws) => shortName(ws.name) === query);
  if (byShort.length === 1) return byShort[0];
  if (byShort.length > 1) {
    console.error(chalk.red(`Ambiguous workspace "${query}"; matches: ${byShort.map((ws) => ws.name).join(", ")}`));
    return void 0;
  }
  return void 0;
}

// src/pm/workspaces.ts
import { existsSync, readFileSync } from "node:fs";
import PATH from "node:path";
import { globSync } from "glob";
function readJsonFile(path4) {
  if (!existsSync(path4)) return void 0;
  try {
    return JSON.parse(readFileSync(path4, "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
import { existsSync as existsSync2 } from "node:fs";
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/lib/eslintCache.ts
import path from "node:path";
function workspaceEslintCachePath(rootDir, workspaceLocation) {
  const segment = workspaceLocation === "." ? "root" : workspaceLocation;
  return path.resolve(rootDir, ".xy/cache/eslint", segment, ".eslintcache");
}
function legacyWorkspaceEslintCachePath(rootDir, workspaceLocation) {
  return path.resolve(rootDir, workspaceLocation, ".eslintcache");
}

// src/lib/initCwd.ts
function INIT_CWD() {
  return process.env.INIT_CWD ?? process.cwd();
}

// src/lib/packageName.ts
import { readFileSync as readFileSync2 } from "node:fs";
import path2 from "node:path";
function packageName() {
  if (process.env.npm_package_name != null && process.env.npm_package_name.length > 0) {
    return process.env.npm_package_name;
  }
  try {
    const pkgPath = path2.join(INIT_CWD(), "package.json");
    const pkg = JSON.parse(readFileSync2(pkgPath, "utf8"));
    return pkg.name;
  } catch {
    return void 0;
  }
}

// src/actions/clean-eslint.ts
var cleanESLint = () => {
  const pkgDir = INIT_CWD();
  const pkgName = packageName();
  console.log(chalk2.green(`Cleaning ESLint [${pkgName ?? pkgDir}]`));
  deleteGlob(path3.join(pkgDir, ".eslintcache"));
  return 0;
};
function cleanWorkspaceLintCaches(rootDir, workspaceLocation) {
  deleteGlob(workspaceEslintCachePath(rootDir, workspaceLocation));
  deleteGlob(legacyWorkspaceEslintCachePath(rootDir, workspaceLocation));
}
function cleanLintCaches(pkg) {
  const rootDir = INIT_CWD();
  const pm = getPackageManager();
  if (pkg !== void 0) {
    const workspace = resolveWorkspace(pm.listWorkspaces(), pkg);
    if (workspace === void 0) {
      console.log(chalk2.yellow(`Package "${pkg}" not found; cleaning root ESLint cache only`));
      deleteGlob(path3.join(rootDir, ".xy/cache/eslint/**"));
      deleteGlob(path3.join(rootDir, ".eslintcache"));
      return;
    }
    console.log(chalk2.green(`Cleaning ESLint cache [${workspace.name}]`));
    cleanWorkspaceLintCaches(rootDir, workspace.location);
    return;
  }
  console.log(chalk2.green("Cleaning ESLint caches"));
  deleteGlob(path3.join(rootDir, ".xy/cache/eslint/**"));
  deleteGlob(path3.join(rootDir, ".eslintcache"));
  for (const workspace of pm.listWorkspaces()) {
    cleanWorkspaceLintCaches(rootDir, workspace.location);
  }
}
export {
  cleanESLint,
  cleanLintCaches
};
//# sourceMappingURL=clean-eslint.mjs.map
