// src/actions/license.ts
import chalk from "chalk";
import { init } from "license-checker";

// 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) return void 0;
  return packageManager.split("@")[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 && 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/actions/license.ts
var license = async (pkg) => {
  const workspaces = getPackageManager().listWorkspaces();
  const workspaceList = workspaces.filter(({ name }) => {
    return pkg === void 0 || name === pkg;
  });
  const exclude = /* @__PURE__ */ new Set([
    "MIT",
    "MIT*",
    "ISC",
    "Apache-2.0",
    "BSD",
    "BSD*",
    "BSD-2-Clause",
    "BSD-3-Clause",
    "CC-BY-4.0",
    "Unlicense",
    "CC-BY-3.0",
    "CC0-1.0",
    "LGPL-3.0-only",
    "LGPL-3.0",
    "LGPL-3.0-or-later",
    "Python-2.0"
  ]);
  console.log(chalk.green("License Checker"));
  return (await Promise.all(
    workspaceList.map(({ location, name }) => {
      return new Promise((resolve) => {
        init({ production: true, start: location }, (error, packages) => {
          if (error) {
            console.error(chalk.red(`License Checker [${name}] Error`));
            console.error(chalk.gray(error));
            console.log("\n");
            resolve(1);
          } else {
            console.log(chalk.green(`License Checker [${name}]`));
            let count = 0;
            for (const [name2, info] of Object.entries(packages)) {
              const licenses = Array.isArray(info.licenses) ? info.licenses : [info.licenses];
              for (const license2 of licenses) {
                if (license2) {
                  const processedLicense = license2.startsWith("(") && license2.at(-1) === ")" ? license2.slice(1, -2) : license2;
                  const orLicenses = processedLicense.split(" OR ");
                  let orLicenseFound = false;
                  for (const orLicense of orLicenses) {
                    if (exclude.has(orLicense)) {
                      orLicenseFound = true;
                    }
                  }
                  if (!orLicenseFound) {
                    count++;
                    console.warn(chalk.yellow(`${name2}: Package License not allowed [${license2}]`));
                  }
                }
              }
            }
            console.log("\n");
            resolve(count);
          }
        });
      });
    })
  )).reduce((prev, value) => prev || value, 0);
};
export {
  license
};
//# sourceMappingURL=license.mjs.map
