Skip to content

armgit / clipy

Public Imported from GitHub
0

The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.

3.7 KB
Raw Blame
#!/usr/bin/env node
/**
 * Generate the Gemini CLI `/watch` command from the Claude watch skill, so the
 * two never drift. `plugins/gemini-watch/commands/watch.toml` is derived from
 * `plugins/watch/skills/watch/SKILL.md`: the same instructions, with the
 * skill's `$ARGUMENTS` placeholder written as Gemini's `{{args}}`.
 *
 *   node scripts/gen-gemini-watch.mjs           # write the command file
 *   node scripts/gen-gemini-watch.mjs --check    # fail if it is out of date
 *
 * The --check form runs in `pnpm test:plugins`, so a change to the watch skill
 * that is not regenerated here fails CI.
 */
import assert from "node:assert/strict";
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const root = join(dirname(fileURLToPath(import.meta.url)), "..");
const skillPath = join(root, "plugins/watch/skills/watch/SKILL.md");
const tomlPath = join(root, "plugins/gemini-watch/commands/watch.toml");

/**
 * Git on Windows (core.autocrlf) checks text files out with CRLF, so both the
 * skill and the generated command are compared with LF endings.
 */
export function normalizeNewlines(text) {
  return text.replaceAll("\r\n", "\n");
}

/** Split a skill's YAML frontmatter from its Markdown body. */
export function parseSkill(text) {
  const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n([\s\S]*)$/.exec(text);
  assert.ok(match, "SKILL.md has no frontmatter");
  const frontmatter = {};
  for (const line of match[1].split(/\r?\n/)) {
    const i = line.indexOf(":");
    if (i !== -1) frontmatter[line.slice(0, i).trim()] = line.slice(i + 1).trim();
  }
  return { frontmatter, body: match[2].trim() };
}

/** A TOML multi-line literal string: no escaping, so `{{args}}` and backslashes survive verbatim. */
function tomlLiteral(value) {
  assert.ok(!value.includes("'''"), "the prompt cannot contain a TOML triple-quote");
  return `'''\n${value}\n'''`;
}

/** The generated watch.toml for the given SKILL.md text (CRLF or LF). */
export function render(skillText) {
  const { frontmatter, body } = parseSkill(normalizeNewlines(skillText));
  assert.ok(frontmatter.description, "the watch skill has no description");
  // Gemini shows a one-line description in /help; take the skill's first sentence.
  const description = frontmatter.description.split(/(?<=\.)\s/)[0].trim();
  // Gemini substitutes the user's arguments where {{args}} appears; the skill calls them $ARGUMENTS.
  const prompt = body.replaceAll("$ARGUMENTS", "{{args}}");
  return [
    "# Generated from plugins/watch/skills/watch/SKILL.md by scripts/gen-gemini-watch.mjs.",
    "# Do not edit by hand: run `node scripts/gen-gemini-watch.mjs`; `pnpm test:plugins` checks it is in sync.",
    "",
    `description = ${JSON.stringify(description)}`,
    `prompt = ${tomlLiteral(prompt)}`,
    "",
  ].join("\n");
}

function main() {
  const generated = render(readFileSync(skillPath, "utf8"));
  if (process.argv.includes("--check")) {
    let current = null;
    try {
      current = normalizeNewlines(readFileSync(tomlPath, "utf8"));
    } catch {
      current = null;
    }
    assert.equal(current, generated, `${tomlPath} is out of date with the watch skill. Run: node scripts/gen-gemini-watch.mjs`);
    console.log("gemini-watch: commands/watch.toml is in sync with the watch skill");
  } else {
    mkdirSync(dirname(tomlPath), { recursive: true });
    writeFileSync(tomlPath, generated);
    console.log(`gemini-watch: wrote ${tomlPath.slice(root.length + 1)}`);
  }
}

// Run only as a script, so the test can import render() without side effects.
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) main();