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

// 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/skills/lint.ts
import {
  existsSync,
  readdirSync,
  statSync
} from "node:fs";
import OS from "node:os";
import PATH from "node:path";
import chalk from "chalk";
var listSkillDirs = (root) => {
  if (!existsSync(root)) return [];
  return readdirSync(root).filter(
    (f) => statSync(PATH.resolve(root, f)).isDirectory()
  );
};
var skillsLint = (options = {}) => {
  const projectDir = options.projectDir ?? PATH.resolve(INIT_CWD(), ".claude", "skills");
  const globalDir = options.globalDir ?? PATH.resolve(OS.homedir(), ".claude", "skills");
  const projectSkills = listSkillDirs(projectDir);
  const globalSkills = new Set(listSkillDirs(globalDir));
  const duplicates = projectSkills.filter((name) => globalSkills.has(name)).toSorted();
  if (duplicates.length === 0) {
    console.log(chalk.gray("skills: no project/global duplicates"));
    return 0;
  }
  for (const name of duplicates) {
    console.log(chalk.yellow(`skills: '${name}' is installed in both project and global scope`));
  }
  console.log(chalk.yellow(`skills: ${duplicates.length} duplicate(s) found`));
  return 1;
};

// 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/actions/skills/remove.ts
var INTERACTIVE_FLAG_PATTERN = /^--(?:no-)?interactive(?:=.+)?$/;
var shouldUseInteractiveRemove = (argv) => {
  if (argv.interactive === true) return true;
  if (Array.isArray(argv.skills) && argv.skills.length > 0) return false;
  if (argv.all === true) return false;
  if (Array.isArray(argv.skill) && argv.skill.length > 0) return false;
  return true;
};

// src/actions/skills/updo.ts
var SCOPE_FLAGS = /* @__PURE__ */ new Set(["-g", "--global", "-p", "--project"]);
var hasSkillsScopeFlag = (args) => args.some((a) => SCOPE_FLAGS.has(a));
var skillsUpdo = (args) => {
  const finalArgs = hasSkillsScopeFlag(args) ? args : ["-p", ...args];
  return runSkills("update", finalArgs);
};

// src/xy/common/skills/addCommand.ts
var addCommand = {
  aliases: ["a"],
  builder: (yargs) => yargs.parserConfiguration({ "unknown-options-as-args": true }).positional("package", {
    describe: "Skill package to add (e.g. vercel-labs/agent-skills or a git URL)",
    type: "string"
  }).option("global", {
    alias: "g",
    describe: "Install skill globally (user-level) instead of project-level",
    type: "boolean"
  }).option("agent", {
    alias: "a",
    describe: "Specify agents to install to (use '*' for all agents)",
    type: "array"
  }).option("skill", {
    alias: "s",
    describe: "Specify skill names to install (use '*' for all skills)",
    type: "array"
  }).option("list", {
    alias: "l",
    describe: "List available skills in the repository without installing",
    type: "boolean"
  }).option("yes", {
    alias: "y",
    describe: "Skip confirmation prompts",
    type: "boolean"
  }).option("copy", { describe: "Copy files instead of symlinking to agent directories", type: "boolean" }).option("all", { describe: "Shorthand for --skill '*' --agent '*' -y", type: "boolean" }).option("full-depth", { describe: "Search all subdirectories even when a root SKILL.md exists", type: "boolean" }).example("$0 skills add vercel-labs/agent-skills", "Install a package").example("$0 skills add vercel-labs/agent-skills -g", "Install globally").example("$0 skills add vercel-labs/agent-skills --agent claude-code cursor", "Restrict to specific agents").example("$0 skills add vercel-labs/agent-skills --skill pr-review commit", "Restrict to specific skills"),
  command: "add [package]",
  describe: "Add a skill package (proxies to `npx skills add`)",
  handler: async () => {
    await withEnvelope("skills add", () => runSkills("add", argvAfterSkillsSubcommand("add", ["a"])));
  }
};

// src/xy/common/skills/experimentalInstallCommand.ts
var experimentalInstallCommand = {
  builder: (yargs) => yargs.parserConfiguration({ "unknown-options-as-args": true }).example("$0 skills experimental_install", "Restore skills from skills-lock.json"),
  command: "experimental_install",
  describe: "Restore skills from skills-lock.json (proxies to `npx skills experimental_install`)",
  handler: async () => {
    await withEnvelope(
      "skills experimental_install",
      () => runSkills("experimental_install", argvAfterSkillsSubcommand("experimental_install"))
    );
  }
};

// src/xy/common/skills/experimentalSyncCommand.ts
var experimentalSyncCommand = {
  builder: (yargs) => yargs.parserConfiguration({ "unknown-options-as-args": true }).option("agent", {
    alias: "a",
    describe: "Specify agents to install to (use '*' for all agents)",
    type: "array"
  }).option("yes", {
    alias: "y",
    describe: "Skip confirmation prompts",
    type: "boolean"
  }).example("$0 skills experimental_sync", "Sync from node_modules").example("$0 skills experimental_sync -y", "Sync without prompts"),
  command: "experimental_sync",
  describe: "Sync skills from node_modules into agent directories (proxies to `npx skills experimental_sync`)",
  handler: async () => {
    await withEnvelope(
      "skills experimental_sync",
      () => runSkills("experimental_sync", argvAfterSkillsSubcommand("experimental_sync"))
    );
  }
};

// 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")));
  }
};

// src/xy/common/skills/initCommand.ts
var initCommand = {
  builder: (yargs) => yargs.parserConfiguration({ "unknown-options-as-args": true }).positional("name", {
    describe: "Skill name (creates <name>/SKILL.md; omit to create ./SKILL.md)",
    type: "string"
  }).example("$0 skills init my-skill", "Create my-skill/SKILL.md").example("$0 skills init", "Create ./SKILL.md in the current directory"),
  command: "init [name]",
  describe: "Initialize a new skill template (proxies to `npx skills init`)",
  handler: async () => {
    await withEnvelope("skills init", () => runSkills("init", argvAfterSkillsSubcommand("init")));
  }
};

// src/xy/common/skills/lintCommand.ts
var lintCommand = {
  builder: (yargs) => yargs,
  command: "lint",
  describe: "Check for skills installed in both project and global scopes",
  handler: async (argv) => {
    if (argv.verbose) console.log("Skills Lint");
    await withEnvelope("skills lint", () => skillsLint());
  }
};

// src/xy/common/skills/listCommand.ts
var listCommand = {
  aliases: ["ls"],
  builder: (yargs) => yargs.parserConfiguration({ "unknown-options-as-args": true }).option("global", {
    alias: "g",
    describe: "List global skills (default: project)",
    type: "boolean"
  }).option("agent", {
    alias: "a",
    describe: "Filter by specific agents",
    type: "array"
  }).option("json", { describe: "Output as JSON (machine-readable, no ANSI codes)", type: "boolean" }).example("$0 skills list", "List project skills").example("$0 skills ls -g", "List global skills").example("$0 skills ls -a claude-code", "Filter by agent").example("$0 skills ls --json", "JSON output"),
  command: "list",
  describe: "List installed skills (proxies to `npx skills list`)",
  handler: async () => {
    await withEnvelope("skills list", () => runSkills("list", argvAfterSkillsSubcommand("list", ["ls"])));
  }
};

// src/xy/common/skills/removeCommand.ts
var removeCommand = {
  aliases: ["rm"],
  builder: (yargs) => yargs.parserConfiguration({ "unknown-options-as-args": true }).positional("skills", {
    array: true,
    describe: "Skill names to remove (omit for interactive mode)",
    type: "string"
  }).option("global", {
    alias: "g",
    describe: "Remove from global scope",
    type: "boolean"
  }).option("agent", {
    alias: "a",
    describe: "Remove from specific agents (use '*' for all agents)",
    type: "array"
  }).option("skill", {
    alias: "s",
    describe: "Specify skills to remove (use '*' for all skills)",
    type: "array"
  }).option("yes", {
    alias: "y",
    describe: "Skip confirmation prompts",
    type: "boolean"
  }).option("all", { describe: "Shorthand for --skill '*' --agent '*' -y", type: "boolean" }).option("interactive", {
    describe: "Pick skills to remove interactively (auto-detected when no skill names are given)",
    type: "boolean"
  }).example("$0 skills remove", "Interactive remove (auto-detected)").example("$0 skills remove --interactive", "Force interactive picker").example("$0 skills remove web-design", "Remove by name").example("$0 skills rm --global frontend-design", "Remove from global scope"),
  command: "remove [skills..]",
  describe: "Remove installed skills (proxies to `npx skills remove`)",
  handler: async (argv) => {
    const interactive = shouldUseInteractiveRemove(argv);
    const rawArgs = argvAfterSkillsSubcommand("remove", ["rm"]);
    const forwardArgs = rawArgs.filter((a) => !INTERACTIVE_FLAG_PATTERN.test(a));
    await withEnvelope("skills remove", () => runSkills("remove", forwardArgs, { interactive }));
  }
};

// src/xy/common/skills/updateCommand.ts
var updateCommand = {
  aliases: ["upgrade"],
  builder: (yargs) => yargs.parserConfiguration({ "unknown-options-as-args": true }).positional("skills", {
    array: true,
    describe: "Specific skills to update (omit to update all)",
    type: "string"
  }).option("global", {
    alias: "g",
    describe: "Update global skills only",
    type: "boolean"
  }).option("project", { describe: "Update project skills only (skills.sh `-p`)", type: "boolean" }).option("yes", {
    alias: "y",
    describe: "Skip scope prompt (auto-detect: project if in a project, else global)",
    type: "boolean"
  }).example("$0 skills update", "Update all installed skills").example("$0 skills update my-skill", "Update a single skill").example("$0 skills update -g", "Update global skills only"),
  command: "update [skills..]",
  describe: "Update skills to latest versions (proxies to `npx skills update`)",
  handler: async () => {
    await withEnvelope("skills update", () => runSkills("update", argvAfterSkillsSubcommand("update", ["upgrade"])));
  }
};

// src/xy/common/skills/updoCommand.ts
var updoCommand = {
  builder: (yargs) => yargs.parserConfiguration({ "unknown-options-as-args": true }).positional("skills", {
    array: true,
    describe: "Specific skills to check (omit to check all)",
    type: "string"
  }).option("global", {
    alias: "g",
    describe: "Check global skills instead of the project (default: project)",
    type: "boolean"
  }).option("project", { describe: "Check project skills only (default; equivalent to skills.sh `-p`)", type: "boolean" }).option("yes", {
    alias: "y",
    describe: "Skip confirmation prompts",
    type: "boolean"
  }).example("$0 skills updo", "Check project skills for newer versions (default)").example("$0 skills updo -g", "Check global skills instead").example("$0 skills updo my-skill", "Check a single skill"),
  command: "updo [skills..]",
  describe: "Check for newer versions of installed skills (defaults to project scope; pass --global to check global)",
  handler: async () => {
    await withEnvelope("skills updo", () => skillsUpdo(argvAfterSkillsSubcommand("updo")));
  }
};

// src/xy/common/skills/index.ts
var skillsCommand = {
  builder: (yargs) => {
    return yargs.command(addCommand).command(removeCommand).command(listCommand).command(findCommand).command(updateCommand).command(initCommand).command(experimentalInstallCommand).command(experimentalSyncCommand).command(lintCommand).command(updoCommand).demandCommand(1, "Please specify a skills subcommand").strictCommands();
  },
  command: "skills",
  describe: "Arie's Super Skills - manage agent skills via skills.sh (proxies to `npx skills`) with super-powers",
  handler: () => {
  }
};
export {
  skillsCommand
};
//# sourceMappingURL=index.mjs.map
