UNPKG

scai

Version:

> **AI-powered CLI for local code analysis, commit message suggestions, and natural-language queries.** 100% local, private, GDPR-friendly, made in Denmark/EU with ā¤ļø.

50 lines (49 loc) • 1.99 kB
import fs from "fs"; import os from "os"; import path from "path"; import { spawn } from "child_process"; /** * Opens a visual diff in VS Code between the original and modified versions. * * @param filePath - The path or name of the file being diffed. * @param diffContent - The unified diff text for that file. */ export async function openDiffInVSCode(filePath, diffContent) { try { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "scai-diff-")); // Temporary file paths const baseName = path.basename(filePath); const originalPath = path.join(tmpDir, `${baseName}_OLD.tmp`); const modifiedPath = path.join(tmpDir, `${baseName}_NEW.tmp`); // Extract file versions from unified diff const oldLines = []; const newLines = []; for (const line of diffContent.split("\n")) { if (line.startsWith("+") && !line.startsWith("+++")) { newLines.push(line.slice(1)); } else if (line.startsWith("-") && !line.startsWith("---")) { oldLines.push(line.slice(1)); } else if (!line.startsWith("@@")) { // context lines appear in both versions oldLines.push(line); newLines.push(line); } } fs.writeFileSync(originalPath, oldLines.join("\n"), "utf-8"); fs.writeFileSync(modifiedPath, newLines.join("\n"), "utf-8"); console.log(`\nšŸ” Opening VS Code diff:`); console.log(` ${originalPath}`); console.log(`⇄ ${modifiedPath}\n`); const child = spawn("code", ["--diff", originalPath, modifiedPath], { stdio: "inherit", }); child.on("error", () => { console.error("āŒ Failed to open diff in VS Code. Ensure the 'code' CLI is installed and in your PATH."); }); } catch (err) { console.error("āŒ Error while preparing diff for VS Code:", err); } }