#!/usr/bin/env node

// src/bin/package/lint.ts
import chalk2 from "chalk";

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

// src/actions/package/lint.ts
import { readdirSync } from "fs";
import path from "path";
import { cwd } from "process";
import { pathToFileURL } from "url";
import chalk from "chalk";
import { ESLint } from "eslint";
import { findUp } from "find-up";
import picomatch from "picomatch";
var dumpMessages = (lintResults) => {
  const colors = ["white", "yellow", "red"];
  const severity = ["none", "warning", "error"];
  for (const lintResult of lintResults) {
    if (lintResult.messages.length > 0) {
      console.log(chalk.gray(`
${lintResult.filePath}`));
      for (const message of lintResult.messages) {
        console.log(
          chalk.gray(`	${message.line}:${message.column}`),
          chalk[colors[message.severity]](`	${severity[message.severity]}`),
          chalk.white(`	${message.message}`),
          chalk.gray(`	${message.ruleId}`)
        );
      }
    }
  }
};
async function getRootESLintConfig() {
  const configPath = await findUp(["eslint.config.ts", "eslint.config.mjs"]);
  if (configPath === void 0) {
    throw new Error("eslint.config.ts not found in the monorepo");
  }
  return pathToFileURL(configPath);
}
function getFiles(dir, ignoreFolders) {
  const currentDirectory = cwd();
  const subDirectory = dir.split(currentDirectory)[1]?.split("/")[1];
  if (ignoreFolders.includes(subDirectory)) return [];
  return readdirSync(dir, { withFileTypes: true }).flatMap((dirent) => {
    const res = path.resolve(dir, dirent.name);
    const relativePath = subDirectory === void 0 ? dirent.name : `${subDirectory}/${dirent.name}`;
    const ignoreMatchers = ignoreFolders.map((pattern) => picomatch(pattern));
    if (ignoreMatchers.some((isMatch) => isMatch(relativePath))) return [];
    return dirent.isDirectory() ? getFiles(res, ignoreFolders) : [res];
  });
}
var packageLint = async (fix = false, verbose = false, cache = true) => {
  const pkg = INIT_CWD();
  const configPath = await getRootESLintConfig();
  const { default: eslintConfig } = await import(configPath.href);
  const start = Date.now();
  const ignoreFolders = ["node_modules", "dist", "packages", ".git", "build", ".yarn", ".vscode", ".github"];
  const engine = new ESLint({
    baseConfig: [...eslintConfig],
    fix,
    warnIgnored: false,
    cache
  });
  const files = getFiles(cwd(), ignoreFolders);
  console.log(chalk.green(`Linting ${pkg} [files = ${files.length}]`));
  if (verbose) {
    for (const file of files) {
      console.log(chalk.gray(`	${file}`));
    }
  }
  const lintResults = await engine.lintFiles(files);
  dumpMessages(lintResults);
  if (fix) {
    await ESLint.outputFixes(lintResults);
  }
  const filesCountColor = files.length < 100 ? "green" : files.length < 1e3 ? "yellow" : "red";
  const lintTime = Date.now() - start;
  const lintTimeColor = lintTime < 1e3 ? "green" : lintTime < 3e3 ? "yellow" : "red";
  console.log(chalk.white(`Linted ${chalk[filesCountColor](files.length)} files in ${chalk[lintTimeColor](lintTime)}ms`));
  return lintResults.reduce((prev, lintResult) => prev + lintResult.errorCount, 0);
};

// src/bin/package/lint.ts
packageLint().then((value) => {
  if (value) {
    process.exit(value);
  }
}).catch((reason) => {
  console.error(chalk2.red(reason));
  process.exit(-1);
});
//# sourceMappingURL=lint.mjs.map