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

// src/lib/reporting/runWithEnvelope.ts
import { performance as performance2 } from "node:perf_hooks";

// src/lib/reporting/xyReporter.ts
import { performance } from "node:perf_hooks";
var jsonMode = false;
var originalStdoutWrite;
var envelopeEmitted = false;
var currentCommand = "unknown";
var startTime = 0;
function isJsonMode() {
  return jsonMode;
}
function setCurrentCommand(name) {
  currentCommand = name;
  startTime = performance.now();
  envelopeEmitted = false;
}
function emitJson(envelope) {
  if (envelopeEmitted) return;
  envelopeEmitted = true;
  const json = JSON.stringify(envelope) + "\n";
  if (originalStdoutWrite) {
    originalStdoutWrite(json);
  } else {
    process.stdout.write(json);
  }
}

// src/lib/reporting/runWithEnvelope.ts
async function runWithEnvelope(command, action) {
  setCurrentCommand(command);
  const start = performance2.now();
  let exitCode;
  let envelope;
  try {
    const ret = await action();
    if (typeof ret === "number") {
      exitCode = ret;
    } else {
      exitCode = ret.exitCode;
      envelope = ret.envelope;
    }
  } catch (ex) {
    exitCode = 1;
    const message = ex instanceof Error ? ex.message : String(ex);
    envelope = {
      errorCount: 1,
      errors: [{ message }]
    };
    if (!isJsonMode()) {
      console.error(message);
    }
  }
  if (isJsonMode()) {
    emitJson({
      command,
      data: envelope?.data,
      durationMs: Math.round(performance2.now() - start),
      errorCount: envelope?.errorCount ?? (exitCode === 0 ? 0 : 1),
      errors: envelope?.errors,
      exitCode,
      schemaVersion: 1,
      success: exitCode === 0,
      warningCount: envelope?.warningCount ?? 0,
      warnings: envelope?.warnings
    });
  }
  return exitCode;
}
async function withEnvelope(command, action) {
  process.exitCode = await runWithEnvelope(command, action);
}

// src/actions/claude-clean.ts
import {
  existsSync,
  readdirSync,
  rmSync,
  unlinkSync
} from "node:fs";
import PATH from "node:path";
import chalk from "chalk";
function removeFile(filePath, label) {
  if (existsSync(filePath)) {
    unlinkSync(filePath);
    console.log(chalk.yellow(`  Removed ${label}`));
    return true;
  }
  return false;
}
function removeDir(dirPath, label) {
  if (existsSync(dirPath)) {
    rmSync(dirPath, { recursive: true });
    console.log(chalk.yellow(`  Removed ${label}`));
    return true;
  }
  return false;
}
function claudeClean() {
  console.log(chalk.green("Clean Claude configuration"));
  const cwd = INIT_CWD();
  let removed = 0;
  const rootFiles = ["CLAUDE.md", "CLAUDE.local.md"];
  for (const file of rootFiles) {
    if (removeFile(PATH.resolve(cwd, file), file)) removed++;
  }
  if (removeDir(PATH.resolve(cwd, ".claude"), ".claude/")) removed++;
  const packagesDir = PATH.resolve(cwd, "packages");
  if (existsSync(packagesDir)) {
    const findClaudeFiles = (dir, prefix) => {
      const entries = readdirSync(dir, { withFileTypes: true });
      for (const entry of entries) {
        const fullPath = PATH.resolve(dir, entry.name);
        const label = `${prefix}${entry.name}`;
        if (entry.isFile() && (entry.name === "CLAUDE.md" || entry.name === "CLAUDE.local.md")) {
          if (removeFile(fullPath, label)) removed++;
        } else if (entry.isDirectory() && entry.name === ".claude") {
          if (removeDir(fullPath, `${label}/`)) removed++;
        } else if (entry.isDirectory() && entry.name !== "node_modules" && entry.name !== "dist") {
          findClaudeFiles(fullPath, `${label}/`);
        }
      }
    };
    findClaudeFiles(packagesDir, "packages/");
  }
  if (removed > 0) {
    console.log(chalk.green(`  Removed ${removed} item(s)`));
  } else {
    console.log(chalk.gray("  Nothing to clean"));
  }
  return 0;
}

// src/xy/common/claude/cleanCommand.ts
var cleanCommand = {
  command: "clean",
  describe: "Remove all Claude configuration files from the repo",
  handler: async (argv) => {
    if (Boolean(argv.verbose)) console.log("Claude Clean");
    await withEnvelope("claude clean", () => claudeClean());
  }
};
export {
  cleanCommand
};
//# sourceMappingURL=cleanCommand.mjs.map
