// src/actions/readme-lint.ts
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
import PATH3 from "path";
import chalk2 from "chalk";

// src/pm/workspaces.ts
import { existsSync, readFileSync } from "fs";
import PATH from "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 "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/lib/initCwd.ts
function INIT_CWD() {
  return process.env.INIT_CWD ?? process.cwd();
}

// src/lib/generateReadmeFiles.ts
import { execFile } from "child_process";
import FS, { readFileSync as readFileSync2 } from "fs";
import {
  mkdir,
  readFile,
  writeFile
} from "fs/promises";
import { createRequire } from "module";
import PATH2 from "path";
import { createInterface } from "readline";
import { promisify } from "util";
import chalk from "chalk";
var execFileAsync = promisify(execFile);
var require2 = createRequire(import.meta.url);
var packageRoot = PATH2.dirname(require2.resolve("@xylabs/toolchain/package.json"));
var readmeTemplatesDir = PATH2.resolve(packageRoot, "templates", "readme");
var DEFAULT_README_TEMPLATE = readFileSync2(PATH2.resolve(readmeTemplatesDir, "README.template.md"), "utf8");
var DEFAULT_README_BODY = readFileSync2(PATH2.resolve(readmeTemplatesDir, "README.body.md"), "utf8");
function resolveTemplatePath(templatePath) {
  const cwd = INIT_CWD();
  return templatePath ?? PATH2.join(cwd, ".xy", "README.template.md");
}

// src/actions/readme-lint.ts
function lintTemplate(cwd) {
  const result = { errors: [], warnings: [] };
  const templatePath = resolveTemplatePath();
  if (!existsSync3(templatePath)) {
    result.errors.push('Missing .xy/README.template.md (run "xy readme init" to create)');
    return result;
  }
  const template = readFileSync3(templatePath, "utf8");
  if (!template.includes("{{body}}")) {
    result.warnings.push(".xy/README.template.md does not contain a {{body}} placeholder");
  }
  if (!template.includes("{{description}}")) {
    result.warnings.push(".xy/README.template.md does not contain a {{description}} placeholder");
  }
  const bodyPath = PATH3.join(cwd, ".xy", "README.body.md");
  if (!existsSync3(bodyPath)) {
    result.errors.push('Missing .xy/README.body.md (run "xy readme init" to create)');
  }
  return result;
}
function lintLogoConfig(cwd, config) {
  const result = { errors: [], warnings: [] };
  const templatePath = resolveTemplatePath();
  if (existsSync3(templatePath)) {
    const template = readFileSync3(templatePath, "utf8");
    const logoRef = /\[logo]: (.+)/.exec(template);
    if (logoRef?.[1].includes("example.com")) {
      result.warnings.push(".xy/README.template.md still has the example.com logo placeholder \u2014 update it or set readme.logoUrl in xy.config.ts");
    } else if (!logoRef && !config.readme?.logoUrl) {
      result.warnings.push("No logo URL configured in xy.config.ts (readme.logoUrl) or template");
    }
  }
  if (!config.readme?.logoUrl && !config.readme?.logoLinkUrl) {
    result.warnings.push("No readme.logoUrl or readme.logoLinkUrl configured in xy.config.ts");
  }
  return result;
}
function lintPackages(cwd) {
  const result = { errors: [], warnings: [] };
  const pm = getPackageManager();
  const workspaces = pm.listWorkspaces();
  for (const { location, name } of workspaces) {
    if (location === ".") continue;
    const pkgPath = PATH3.join(cwd, location, "package.json");
    try {
      const pkg = JSON.parse(readFileSync3(pkgPath, "utf8"));
      if (pkg.private) continue;
      if (!pkg.description) {
        result.warnings.push(`${name} is missing a "description" in package.json`);
      }
      const readmePath = PATH3.join(cwd, location, "README.md");
      if (!existsSync3(readmePath)) {
        result.errors.push(`${name} is missing README.md`);
      }
    } catch {
    }
  }
  return result;
}
function readmeLint({ config, verbose }) {
  const cwd = INIT_CWD();
  console.log(chalk2.green("Readme Lint"));
  const checks = [
    lintTemplate(cwd),
    lintLogoConfig(cwd, config),
    lintPackages(cwd)
  ];
  let errorCount = 0;
  let warningCount = 0;
  for (const { errors, warnings } of checks) {
    for (const error of errors) {
      console.log(chalk2.red(`  \u2717 ${error}`));
      errorCount++;
    }
    for (const warning of warnings) {
      console.log(chalk2.yellow(`  \u26A0 ${warning}`));
      warningCount++;
    }
  }
  if (errorCount === 0 && warningCount === 0) {
    console.log(chalk2.green("  All checks passed"));
  } else {
    if (verbose) {
      console.log(chalk2.gray(`  ${errorCount} error(s), ${warningCount} warning(s)`));
    }
  }
  return errorCount > 0 ? 1 : 0;
}
export {
  readmeLint
};
//# sourceMappingURL=readme-lint.mjs.map