@cappa/cli 0.15.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -12,7 +12,7 @@ import { ProtocolMismatchError, createClient } from "@cappa/client";
12
12
  import { createServer, isLoopbackHost } from "@cappa/server";
13
13
  import { randomBytes } from "node:crypto";
14
14
  //#region package.json
15
- var version = "0.15.0";
15
+ var version = "0.15.1";
16
16
  //#endregion
17
17
  //#region ../../node_modules/.pnpm/tsdown@0.23.0_@arethetypeswrong+core@0.18.5_oxc-resolver@11.24.2_tsx@4.23.1_typescript@7.0.2/node_modules/tsdown/esm-shims.js
18
18
  const getFilename = () => fileURLToPath(import.meta.url);
@@ -93,6 +93,43 @@ const detectSkillStacks = (cwd) => {
93
93
  else if (has(".agents") || has("AGENTS.md")) stacks.push("agents");
94
94
  return stacks.length > 0 ? stacks : ["claude"];
95
95
  };
96
+ const LOCKFILES = {
97
+ "pnpm-lock.yaml": "pnpm",
98
+ "yarn.lock": "yarn",
99
+ "bun.lock": "bun",
100
+ "bun.lockb": "bun",
101
+ "package-lock.json": "npm"
102
+ };
103
+ const detectPackageManager = (dir) => {
104
+ for (const [lockfile, manager] of Object.entries(LOCKFILES)) if (fs.existsSync(path.join(dir, lockfile))) return manager;
105
+ };
106
+ /**
107
+ * The directory agents work from: the workspace root holding the lockfile.
108
+ *
109
+ * `cappa` is often installed in a single workspace package and run with
110
+ * `pnpm -F <pkg> exec cappa`, which puts `cwd` inside that package — but
111
+ * agents only read `.claude/` and `.agents/` from the root they were started
112
+ * in. Walk up to the nearest lockfile or `pnpm-workspace.yaml`, without
113
+ * leaving the git repository; `cwd` itself when there is neither.
114
+ */
115
+ const findProjectRoot = (cwd) => {
116
+ let current = path.resolve(cwd);
117
+ while (true) {
118
+ const has = (entry) => fs.existsSync(path.join(current, entry));
119
+ if (detectPackageManager(current) || has("pnpm-workspace.yaml")) return current;
120
+ const parent = path.dirname(current);
121
+ if (has(".git") || parent === current) return path.resolve(cwd);
122
+ current = parent;
123
+ }
124
+ };
125
+ const readPackageName = (dir) => {
126
+ try {
127
+ const { name } = JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8"));
128
+ return typeof name === "string" ? name : void 0;
129
+ } catch {
130
+ return;
131
+ }
132
+ };
96
133
  /**
97
134
  * How the project runs its own `cappa` binary, from its lockfile.
98
135
  *
@@ -100,18 +137,34 @@ const detectSkillStacks = (cwd) => {
100
137
  * commands *before* the model sees anything, so a bare `cappa` that is not on
101
138
  * `PATH` would inject an error instead of the review state; and agent
102
139
  * permission rules match the command text literally.
140
+ *
141
+ * When `cwd` is a package below `root`, the command switches into it: that is
142
+ * where `cappa` is installed and where `cappa.config.ts` lives, while agents
143
+ * run commands from the root.
103
144
  */
104
- const detectCappaRunner = (cwd) => {
105
- const has = (entry) => fs.existsSync(path.join(cwd, entry));
106
- if (has("pnpm-lock.yaml")) return "pnpm exec cappa";
107
- if (has("yarn.lock")) return "yarn cappa";
108
- if (has("bun.lock") || has("bun.lockb")) return "bunx cappa";
109
- return "npx cappa";
145
+ const detectCappaRunner = (root, cwd = root) => {
146
+ const manager = detectPackageManager(root);
147
+ const dir = path.relative(root, cwd).split(path.sep).join("/");
148
+ if (!dir) return {
149
+ pnpm: "pnpm exec cappa",
150
+ yarn: "yarn cappa",
151
+ bun: "bunx cappa",
152
+ npm: "npx cappa"
153
+ }[manager ?? "npm"];
154
+ switch (manager) {
155
+ case "pnpm": return `pnpm -C ${dir} exec cappa`;
156
+ case "yarn": {
157
+ const name = readPackageName(cwd);
158
+ return name ? `yarn workspace ${name} cappa` : `yarn --cwd ${dir} cappa`;
159
+ }
160
+ case "bun": return `bun --cwd ${dir} x cappa`;
161
+ default: return `npm exec -w ${dir} -- cappa`;
162
+ }
110
163
  };
111
164
  /** Fill the `{{cappa}}` placeholder in a source file. */
112
165
  const renderIntegrationFile = (source, runner) => source.replaceAll("{{cappa}}", runner);
113
166
  /**
114
- * Copy each stack's files into the project.
167
+ * Copy each stack's files into the project root (see `findProjectRoot`).
115
168
  *
116
169
  * A file that exists with different content is left alone unless `force` is
117
170
  * set: users are expected to tailor these to their project, and an upgrade
@@ -120,11 +173,12 @@ const renderIntegrationFile = (source, runner) => source.replaceAll("{{cappa}}",
120
173
  */
121
174
  const installAgentSkills = async (options) => {
122
175
  const sourceDir = options.sourceDir ?? findIntegrationsDir();
123
- const runner = options.runner ?? detectCappaRunner(options.cwd);
176
+ const root = options.root ?? findProjectRoot(options.cwd);
177
+ const runner = options.runner ?? detectCappaRunner(root, options.cwd);
124
178
  const files = [];
125
179
  const seen = /* @__PURE__ */ new Set();
126
180
  for (const stack of options.stacks) for (const mapping of STACK_FILES[stack]) {
127
- const target = path.resolve(options.cwd, mapping.target);
181
+ const target = path.resolve(root, mapping.target);
128
182
  if (seen.has(target)) continue;
129
183
  seen.add(target);
130
184
  const source = renderIntegrationFile(await fsp.readFile(path.join(sourceDir, mapping.source), "utf8"), runner);
@@ -154,9 +208,10 @@ const installAgentSkills = async (options) => {
154
208
  });
155
209
  }
156
210
  const warnings = [];
157
- const claudeSkill = fs.existsSync(path.resolve(options.cwd, ".claude/skills/cappa/SKILL.md"));
211
+ const claudeSkill = fs.existsSync(path.resolve(root, ".claude/skills/cappa/SKILL.md"));
158
212
  if (options.stacks.includes("opencode") && claudeSkill) warnings.push("OpenCode reads .claude/skills too, and uses that copy of the cappa skill over the one in .agents/skills — keep the two in sync if you tailor them.");
159
213
  return {
214
+ root,
160
215
  runner,
161
216
  files,
162
217
  warnings
@@ -284,15 +339,18 @@ const agentApprove = (names, options) => run$1(options, async () => {
284
339
  });
285
340
  const agentInit = (options) => run$1(options, async () => {
286
341
  const cwd = process.cwd();
287
- const stacks = options.stack === "all" ? SKILL_STACK_NAMES : options.stack ? [options.stack] : detectSkillStacks(cwd);
342
+ const root = findProjectRoot(cwd);
343
+ const stacks = options.stack === "all" ? SKILL_STACK_NAMES : options.stack ? [options.stack] : detectSkillStacks(root);
288
344
  const { runner, files, warnings } = await installAgentSkills({
289
345
  cwd,
346
+ root,
290
347
  stacks,
291
348
  force: options.force,
292
349
  runner: options.runner
293
350
  });
294
351
  if (options.json) {
295
352
  printJson({
353
+ root,
296
354
  stacks,
297
355
  runner,
298
356
  files,