// src/lib/crossSpawn.ts
import crossSpawn from "cross-spawn";
function spawn(command, args, options) {
  return crossSpawn(command, args, options);
}

// 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/skills/passthrough.ts
var NPX_SKILLS_PATTERN = /(?<=\[[\d;]*m|\W|^)npx(?:\s+-y)?\s+skills(?=\W|$)/g;
var rewriteSkillsRefs = (text) => text.replaceAll(NPX_SKILLS_PATTERN, "xy skills");
var runSkills = (subcommand, args, options = {}) => {
  const stdio = options.interactive ? "inherit" : ["inherit", "pipe", "pipe"];
  return new Promise((resolve) => {
    const child = spawn("npx", ["-y", "skills", subcommand, ...args], {
      env: { FORCE_COLOR: "3", ...process.env },
      stdio
    });
    if (!options.interactive) {
      child.stdout?.setEncoding("utf8");
      child.stderr?.setEncoding("utf8");
      child.stdout?.on("data", (chunk) => {
        process.stdout.write(rewriteSkillsRefs(chunk));
      });
      child.stderr?.on("data", (chunk) => {
        process.stderr.write(rewriteSkillsRefs(chunk));
      });
    }
    child.on("error", () => resolve(1));
    child.on("close", (code) => resolve(code ?? 1));
  });
};
var argvAfterSkillsSubcommand = (canonicalName, aliases = []) => {
  const argv = process.argv;
  const skillsIdx = argv.indexOf("skills");
  if (skillsIdx === -1) return [];
  const names = /* @__PURE__ */ new Set([canonicalName, ...aliases]);
  for (let i = skillsIdx + 1; i < argv.length; i++) {
    const token = argv[i];
    if (token !== void 0 && names.has(token)) {
      return argv.slice(i + 1);
    }
  }
  return [];
};

// src/xy/common/skills/findCommand.ts
var findCommand = {
  builder: (yargs) => yargs.parserConfiguration({ "unknown-options-as-args": true }).positional("query", { describe: "Search keyword (omit for interactive search)", type: "string" }).example("$0 skills find", "Interactive search").example("$0 skills find typescript", "Search by keyword"),
  command: "find [query]",
  describe: "Search for skills interactively or by keyword (proxies to `npx skills find`)",
  handler: async () => {
    await withEnvelope("skills find", () => runSkills("find", argvAfterSkillsSubcommand("find")));
  }
};
export {
  findCommand
};
//# sourceMappingURL=findCommand.mjs.map
