// src/path/path.ts
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { existsSync } from "node:fs";
function getModuleDir(meta) {
  if (meta.dir) {
    return meta.dir;
  }
  return dirname(fileURLToPath(meta.url));
}
function getPackageRoot(meta) {
  const startDir = getModuleDir(meta);
  return findUp(startDir, "package.json");
}
function getMonorepoRoot(meta) {
  const startDir = getModuleDir(meta);
  const markers = [
    "pnpm-workspace.yaml",
    "pnpm-lock.yaml",
    "bun.lock",
    "bun.lockb",
    "package-lock.json",
    "yarn.lock"
  ];
  for (const marker of markers) {
    try {
      return findUp(startDir, marker);
    } catch {}
  }
  return findUp(startDir, "package.json");
}
function resolveFromRoot(meta, ...paths) {
  const root = getMonorepoRoot(meta);
  return resolve(root, ...paths);
}
function resolveFromPackage(meta, ...paths) {
  const pkgRoot = getPackageRoot(meta);
  return resolve(pkgRoot, ...paths);
}
function findUp(startDir, filename) {
  let current = startDir;
  const root = resolve("/");
  while (current !== root) {
    const candidate = resolve(current, filename);
    if (existsSync(candidate)) {
      return current;
    }
    const parent = dirname(current);
    if (parent === current)
      break;
    current = parent;
  }
  throw new Error(`Could not find ${filename} starting from ${startDir}`);
}
export {
  resolveFromRoot,
  resolveFromPackage,
  getPackageRoot,
  getMonorepoRoot,
  getModuleDir
};

//# debugId=6C5E3AD0868C9DBF64756E2164756E21
