Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

feat(cli): graft init — scaffold .claude/ integration into any repo

Shrish Dwivedi0596269697d1
diff --git a/src/claude/init.ts b/src/claude/init.ts
new file mode 100644
index 00000000..85d5aacf
--- /dev/null
+++ b/src/claude/init.ts
@@ -0,0 +1,38 @@
+import { mkdirSync, writeFileSync, readFileSync, existsSync, chmodSync } from 'node:fs';
+import { join } from 'node:path';
+import { execFileSync } from 'node:child_process';
+import { mergeGraftSettings } from './settings-merge.js';
+import { statuslineShim, hooksShim } from './shim-template.js';
+
+export interface InitResult {
+ settingsPath: string;
+ shims: string[];
+ warnings: string[];
+ built: boolean;
+}
+
+export function runInit(dir: string, opts: { build?: boolean; cliPath?: string } = {}): InitResult {
+ const helpersDir = join(dir, '.claude', 'helpers');
+ mkdirSync(helpersDir, { recursive: true });
+
+ const settingsPath = join(dir, '.claude', 'settings.json');
+ let existing: Record<string, any> = {};
+ try { existing = JSON.parse(readFileSync(settingsPath, 'utf8')); } catch { /* none/invalid → start fresh */ }
+ const { merged, warnings } = mergeGraftSettings(existing);
+ writeFileSync(settingsPath, `${JSON.stringify(merged, null, 2)}\n`);
+
+ const sl = join(helpersDir, 'graft-statusline.cjs');
+ const hk = join(helpersDir, 'graft-hooks.cjs');
+ writeFileSync(sl, statuslineShim()); chmodSync(sl, 0o755);
+ writeFileSync(hk, hooksShim()); chmodSync(hk, 0o755);
+
+ let built = false;
+ const wiring = join(dir, 'graft', '.graph', 'wiring.json');
+ if (opts.build !== false && opts.cliPath && !existsSync(wiring)) {
+ try {
+ execFileSync(process.execPath, [opts.cliPath, 'build', '.'], { cwd: dir, stdio: 'inherit', timeout: 300000 });
+ built = true;
+ } catch { /* build best-effort; user can run `graft build` manually */ }
+ }
+ return { settingsPath, shims: [sl, hk], warnings, built };
+}
diff --git a/src/cli.ts b/src/cli.ts
index 8c2b5a60..a6133987 100644
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -11,11 +11,13 @@
*/
import "dotenv/config";
import { Command } from "commander";
-import { relative } from "node:path";
+import { relative, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
import { Graft } from "./engine.js";
import { resolveConfig } from "./ai/providers.js";
import { formatCheckReport } from "./context/check.js";
import { formatGraphCheckReport } from "./graph/check.js";
+import { runInit } from "./claude/init.js";
const program = new Command();
@@ -180,6 +182,22 @@ program
}
});
+program
+ .command("init")
+ .description("Set up the Claude Code integration (.claude/ statusline + hooks) in this repo")
+ .argument("[dir]", "target repo directory", ".")
+ .option("--no-build", "skip building the graph (wire files only)")
+ .action((dir: string, opts: { build?: boolean }) => {
+ const cliPath = fileURLToPath(import.meta.url);
+ const res = runInit(resolve(dir), { build: opts.build, cliPath });
+ console.error(`✓ wrote ${res.settingsPath}`);
+ for (const s of res.shims) console.error(`✓ wrote ${s}`);
+ console.error(res.built ? "✓ built the graph (graft build)" : "· skipped graph build");
+ for (const w of res.warnings) console.error(`⚠ ${w}`);
+ console.error("\nDone. The statusline + hooks activate in Claude Code sessions in this repo.");
+ console.error("For LLM summaries: set OPENROUTER_API_KEY and run `graft build --deep`.");
+ });
+
program.parseAsync().catch((err) => {
console.error(err instanceof Error ? err.message : err);
process.exit(1);
diff --git a/test/claude-init.test.ts b/test/claude-init.test.ts
new file mode 100644
index 00000000..a63eafb2
--- /dev/null
+++ b/test/claude-init.test.ts
@@ -0,0 +1,39 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { mkdtempSync, readFileSync, existsSync, writeFileSync, mkdirSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { runInit } from '../src/claude/init.js';
+
+function fresh(): string { return mkdtempSync(join(tmpdir(), 'graft-init-')); }
+
+test('runInit scaffolds settings + both shims (build skipped)', () => {
+ const d = fresh();
+ const r = runInit(d, { build: false });
+ assert.ok(existsSync(join(d, '.claude', 'settings.json')));
+ assert.ok(existsSync(join(d, '.claude', 'helpers', 'graft-statusline.cjs')));
+ assert.ok(existsSync(join(d, '.claude', 'helpers', 'graft-hooks.cjs')));
+ assert.equal(r.built, false);
+ const s = JSON.parse(readFileSync(join(d, '.claude', 'settings.json'), 'utf8'));
+ assert.ok(s.statusLine.command.includes('graft-statusline.cjs'));
+ assert.ok(s.hooks.Stop[0].hooks[0].command.includes('graft-hooks.cjs'));
+});
+
+test('runInit preserves foreign settings and warns on foreign statusLine', () => {
+ const d = fresh();
+ mkdirSync(join(d, '.claude'), { recursive: true });
+ writeFileSync(join(d, '.claude', 'settings.json'), JSON.stringify({ model: 'x', statusLine: { command: 'mine' } }));
+ const r = runInit(d, { build: false });
+ const s = JSON.parse(readFileSync(join(d, '.claude', 'settings.json'), 'utf8'));
+ assert.equal(s.model, 'x');
+ assert.equal(s.statusLine.command, 'mine');
+ assert.equal(r.warnings.length, 1);
+});
+
+test('runInit is idempotent', () => {
+ const d = fresh();
+ runInit(d, { build: false });
+ runInit(d, { build: false });
+ const s = JSON.parse(readFileSync(join(d, '.claude', 'settings.json'), 'utf8'));
+ assert.equal(s.hooks.PostToolUse.length, 1);
+});