repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/diff-review/scripts/dev-server.ts

Raw
import { execFile } from "node:child_process";
import { mkdtemp, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { promisify } from "node:util";
import { collectDiff } from "../git.js";
import { startReviewServer } from "../server.js";

const execFileAsync = promisify(execFile);

async function git(cwd: string, args: string[]) {
	await execFileAsync("git", args, { cwd, maxBuffer: 20 * 1024 * 1024 });
}

async function main() {
	const repo = await mkdtemp(join(tmpdir(), "diff-review-dev-"));
	console.log("temp repo:", repo);

	await git(repo, ["init", "-q"]);
	await git(repo, ["config", "user.email", "dev@example.invalid"]);
	await git(repo, ["config", "user.name", "Dev"]);

	await writeFile(
		join(repo, "hello.ts"),
		"export function hello(name: string): string {\n  return `Hello, ${name}!`;\n}\n",
	);
	await git(repo, ["add", "hello.ts"]);
	await git(repo, ["commit", "-qm", "initial"]);

	await writeFile(
		join(repo, "hello.ts"),
		"export function hello(name: string): string {\n  if (!name) throw new Error('name required');\n  return `Hello, ${name}!`;\n}\n",
	);

	const diff = await collectDiff(repo, { staged: false });
	const server = await startReviewServer({
		diff,
		state: { round: 1, findings: [], rounds: [] },
		round: 1,
		onSubmit: (result) => {
			console.log("\n=== SUBMITTED ===");
			console.log("notes:", result.notes);
			console.log("findings:", result.findings.length);
			for (const f of result.findings) {
				console.log(
					`  [${f.severity}][${f.category}] ${f.file}:${f.startLine}-${f.endLine} ${f.comment}`,
				);
			}
		},
	});

	console.log("\n============================================");
	console.log("Open this URL in Chrome or Firefox:");
	console.log(server.url);
	console.log("============================================\n");
	console.log("Press Ctrl+C to stop");

	await server.result;
	console.log("server closed");
}

main().catch((err) => {
	console.error(err);
	process.exit(1);
});