`;
+ const idx = replaced.indexOf(marker);
+ if (idx === -1) return;
+ const nStart = replaced.indexOf('
', idx);
+ if (nStart === -1) return;
+ const nEnd = replaced.indexOf("
", nStart);
+ if (nEnd === -1) return;
+ replaced = replaced.slice(0, nStart + '
'.length) + String(val) + replaced.slice(nEnd);
+ }
+ replaceStat("stat open", counts.open);
+ replaceStat("stat merged", counts.merged);
+ replaceStat("stat closed", counts.closed);
+ replaceStat("stat draft", counts.draft);
+ } catch (e) {}
+
+ try {
+ const safe = String(label).replace(/[^a-z0-9]/gi, "_");
+ replaced = replaced.replace(/const filename = '[^']*';/, `const filename = 'pr-dashboard-${safe}.md';`);
+ } catch (e) {}
+
+ const outPath = path.join(os.tmpdir(), "pr-dashboard.html");
+ fs.writeFileSync(outPath, replaced, "utf8");
+ return outPath;
+}
+
+function openInBrowser(filePath) {
+ try {
+ const platform = process.platform;
+ const opener = platform === "win32" ? null : platform === "darwin" ? "open" : "xdg-open";
+ const child = opener
+ ? spawn(opener, [filePath], { detached: true, stdio: "ignore" })
+ : spawn("cmd", ["/c", "start", '""', filePath], { detached: true, stdio: "ignore" });
+ child.unref();
+ } catch (e) { /* ignore */ }
+}
+
+// ── Main ──────────────────────────────────────────────────────────────────────
+(async () => {
+ try {
+ const { start, end, label } = parseDateRange(query);
+ const labelWithRange = `${label} (${start} → ${end})`;
+
+ console.log(`[pr-dashboard] Fetching PRs for: ${labelWithRange} · role: ${role}`);
+
+ const username = await getGhUsername();
+
+ const roleMap = {
+ "Authored by me": `author:${username}`,
+ "Requested reviews": `review-requested:${username}`,
+ "Assigned to me": `assignee:${username}`,
+ "All": `involves:${username}`,
+ };
+ const roleQualifier = roleMap[role] || `author:${username}`;
+ const qstr = `is:pr ${roleQualifier} created:${start}..${end}`;
+
+ console.log(`[pr-dashboard] Search: ${qstr}`);
+ const items = await searchIssues(qstr);
+ console.log(`[pr-dashboard] Found ${items.length} PR(s)`);
+
+ if (!items.length) {
+ const extDir = path.dirname(fileURLToPath(import.meta.url));
+ const noResultsPath = path.join(os.tmpdir(), "pr-dashboard-no-results.html");
+ fs.writeFileSync(noResultsPath,
+ `
PR Dashboard — ${labelWithRange}No PRs found
No pull requests matched your query for ${labelWithRange}.
`,
+ "utf8"
+ );
+ openInBrowser(noResultsPath);
+ console.log("[pr-dashboard] No results — opened placeholder page.");
+ return;
+ }
+
+ const prs = await pMap(items, getPrDetails, 5);
+ console.log(`[pr-dashboard] Fetched details for ${prs.length} PR(s)`);
+
+ const md = buildMarkdown(prs, labelWithRange);
+ const out = await renderHtml(md, labelWithRange, prs);
+ openInBrowser(out);
+ console.log(`[pr-dashboard] Dashboard opened: ${out}`);
+ } catch (e) {
+ console.error("[pr-dashboard] Error:", e?.message || e);
+ process.exit(1);
+ }
+})();