Skip to content

A self-organizing Obsidian vault that gives AI coding agents persistent memory.

3.2 KB
Raw Blame
#!/usr/bin/env node
/**
 * Write a Gemini CLI wrapper for each om command (#356).
 *
 * Gemini reads custom commands only as TOML, from `.gemini/commands/`, so the
 * vault's `.claude/commands/om-*.md` don't exist there on their own. Each
 * wrapper's prompt injects its command file with `@{…}`, which Gemini resolves
 * from the workspace before it substitutes arguments, so the command text is
 * read live from the `.md` and there is no copy to drift. Wrappers for
 * commands that no longer exist are removed.
 *
 *   node --experimental-strip-types .scripts/gemini-commands.ts
 *
 * tests/gemini-commands.test.ts fails when a wrapper is missing or stale.
 */

import { existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { isMainModule } from "../.claude/scripts/lib/main-guard.ts";
import { parseFrontmatter } from "../.claude/scripts/lib/memory-recall.ts";

const REPO = join(dirname(fileURLToPath(import.meta.url)), "..");
const COMMANDS = join(REPO, ".claude", "commands");
/** Where the wrappers live. */
export const WRAPPERS = join(REPO, ".gemini", "commands");

/** The command's description: its frontmatter `description`, else its first line of prose. */
export function commandDescription(markdown: string): string {
	const described = parseFrontmatter(markdown)["description"];
	if (typeof described === "string" && described !== "") return described;
	const body = markdown.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, "");
	return body.split(/\r?\n/).map((l) => l.trim()).find((l) => l !== "" && !l.startsWith("#")) ?? "";
}

/** The Gemini TOML wrapper for the om command `name`, given its markdown. */
export function geminiCommand(name: string, markdown: string): string {
	return [
		`# Generated by .scripts/gemini-commands.ts from .claude/commands/${name}.md. Do not edit: run the script.`,
		`description = ${JSON.stringify(commandDescription(markdown))}`,
		'prompt = """',
		`Run the vault command below. It is written for Claude Code: where it says $ARGUMENTS, use these arguments: {{args}}`,
		"",
		`@{.claude/commands/${name}.md}`,
		'"""',
		"",
	].join("\n");
}

/** Every wrapper there should be: file name → contents, one per om command. */
export function expectedWrappers(): Map<string, string> {
	const wrappers = new Map<string, string>();
	for (const file of readdirSync(COMMANDS).filter((f) => /^om-.+\.md$/.test(f))) {
		const name = file.replace(/\.md$/, "");
		wrappers.set(`${name}.toml`, geminiCommand(name, readFileSync(join(COMMANDS, file), "utf8")));
	}
	return wrappers;
}

/** Wrapper files whose command is gone. */
export function orphanedWrappers(expected: Map<string, string>): string[] {
	return (existsSync(WRAPPERS) ? readdirSync(WRAPPERS) : []).filter((f) => /^om-.+\.toml$/.test(f) && !expected.has(f));
}

function main(): void {
	const expected = expectedWrappers();
	mkdirSync(WRAPPERS, { recursive: true });
	for (const [file, toml] of expected) writeFileSync(join(WRAPPERS, file), toml);
	for (const file of orphanedWrappers(expected)) unlinkSync(join(WRAPPERS, file));
	console.log(`${expected.size} Gemini command wrappers in .gemini/commands/`);
}

if (isMainModule(import.meta.url)) main();