// src/actions/deplint/findFiles.ts
import fs from "fs";
import path from "path";

// src/actions/deplint/findFilesByGlob.ts
import { globSync } from "glob";
function findFilesByGlob(cwd, pattern, ignore) {
  return globSync(pattern, {
    cwd,
    absolute: true,
    ignore,
    nodir: true
  });
}

// src/actions/deplint/findFiles.ts
var codeExtensions = "*.{ts,tsx,mts,cts,js,mjs,cjs}";
var codeExtensionRe = /\.(?:ts|tsx|mts|cts|js|mjs|cjs)$/i;
function readPackageJson(location) {
  try {
    return JSON.parse(fs.readFileSync(`${location}/package.json`, "utf8"));
  } catch {
    return void 0;
  }
}
function getWorkspaceIgnores(pkg) {
  const ws = pkg?.workspaces;
  if (Array.isArray(ws)) return ws;
  if (ws && Array.isArray(ws.packages)) return ws.packages;
  return [];
}
function getBinFiles(location, pkg) {
  const bin = pkg?.bin;
  if (!bin) return [];
  const entries = typeof bin === "string" ? [bin] : Object.values(bin);
  const results = [];
  for (const entry of entries) {
    if (typeof entry !== "string") continue;
    const abs = path.resolve(location, entry);
    if (!codeExtensionRe.test(abs)) continue;
    if (!fs.existsSync(abs)) continue;
    results.push(abs);
  }
  return results;
}
function uniqueFiles(...lists) {
  const seen = /* @__PURE__ */ new Set();
  const result = [];
  for (const list of lists) {
    for (const file of list) {
      if (seen.has(file)) continue;
      seen.add(file);
      result.push(file);
    }
  }
  return result;
}
function findFiles(location) {
  const pkg = readPackageJson(location);
  const workspaceIgnores = getWorkspaceIgnores(pkg).map((w) => `${w}/**`);
  const ignore = ["**/node_modules/**", "dist/**", ...workspaceIgnores];
  const allFiles = findFilesByGlob(location, `./**/${codeExtensions}`, ignore);
  const distGlobFiles = [
    ...findFilesByGlob(location, "./dist/**/*.d.ts"),
    ...findFilesByGlob(location, `./dist/**/${codeExtensions}`)
  ];
  const binFiles = getBinFiles(location, pkg);
  const distFiles = uniqueFiles(distGlobFiles, binFiles);
  return { allFiles, distFiles };
}
export {
  findFiles
};
//# sourceMappingURL=findFiles.mjs.map