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
JavaScript
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);
}
}