UNPKG

scai

Version:

> AI-powered CLI tools for smart commit messages, auto generated comments, and readme files — all powered by local models.

62 lines (57 loc) • 1.95 kB
import { execSync } from 'child_process'; import fs from 'fs/promises'; import path from 'path'; export async function updateReadmeIfNeeded() { try { let diff = execSync("git diff", { encoding: "utf-8" }).trim(); if (!diff) { diff = execSync("git diff --cached", { encoding: "utf-8" }).trim(); } if (!diff) { console.log('āš ļø No staged changes to suggest a message for.'); return; } const readmePath = path.resolve("README.md"); let readme = ""; try { readme = await fs.readFile(readmePath, "utf-8"); } catch { console.log("šŸ“„ No existing README.md found, skipping update."); return; } const prompt = ` You're an experienced documentation writer. Here's the current README: --- README START --- ${readme} --- README END --- Here is a Git diff of recent code changes: --- DIFF START --- ${diff} --- DIFF END --- āœ… If the changes are significant and relevant to the public-facing documentation, return an updated README. āŒ If they are not, return ONLY: "NO UPDATE". `; const res = await fetch("http://localhost:11434/api/generate", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ model: "llama3", prompt, stream: false, }), }); const { response } = await res.json(); const result = response.trim(); if (result === "NO UPDATE") { console.log("āœ… No significant changes for README."); } else { await fs.writeFile(readmePath, result, "utf-8"); console.log("šŸ“ README.md updated based on significant changes."); } } catch (err) { console.error("āŒ Failed to update README:", err.message); } }