|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Screenshot generator for readme |
| 4 | + |
| 5 | +const { firefox } = require("playwright"); |
| 6 | +const { execSync } = require("child_process"); |
| 7 | +const { readFileSync } = require("fs"); |
| 8 | +const { imageSize } = require("image-size"); |
| 9 | + |
| 10 | +async function main() { |
| 11 | + const version = execSync( |
| 12 | + 'git describe --tags --match "v*.*.*" --abbrev=0', |
| 13 | + { encoding: "utf-8" } |
| 14 | + ).trim(); |
| 15 | + const browser = await firefox.launch({ headless: true }); |
| 16 | + const page = await browser.newPage({ |
| 17 | + viewport: { width: 1280, height: 1024 }, |
| 18 | + deviceScaleFactor: 2, |
| 19 | + }); |
| 20 | + |
| 21 | + console.log("Loading PR page..."); |
| 22 | + await page.goto("https://github.com/rossjrw/pr-preview-action/pull/1", { |
| 23 | + waitUntil: "domcontentloaded", |
| 24 | + }); |
| 25 | + await page.waitForTimeout(2000); |
| 26 | + console.log("Page loaded"); |
| 27 | + |
| 28 | + console.log("Cleaning up comment..."); |
| 29 | + await page.evaluate((newVersion) => { |
| 30 | + const style = document.createElement("style"); |
| 31 | + style.textContent = ` |
| 32 | + .TimelineItem::before, |
| 33 | + .timeline-comment-actions, |
| 34 | + .details-overlay{ |
| 35 | + display: none !important; |
| 36 | + } |
| 37 | + .timeline-comment .comment-body table { |
| 38 | + margin-bottom: 0; |
| 39 | + } |
| 40 | + .timeline-comment { |
| 41 | + max-width: fit-content; |
| 42 | + } |
| 43 | + .timeline-comment-header > * { |
| 44 | + font-size: 0 !important; |
| 45 | + } |
| 46 | + .timeline-comment-header strong { |
| 47 | + font-size: 0.875rem !important; |
| 48 | + } |
| 49 | + `; |
| 50 | + document.head.appendChild(style); |
| 51 | + |
| 52 | + const links = document.querySelectorAll( |
| 53 | + 'a[href="/apps/github-actions"]' |
| 54 | + ); |
| 55 | + let commentGroup = null; |
| 56 | + for (const link of links) { |
| 57 | + commentGroup = link.closest(".timeline-comment-group"); |
| 58 | + if (commentGroup) break; |
| 59 | + } |
| 60 | + |
| 61 | + if (!commentGroup) return { error: "Comment group not found" }; |
| 62 | + |
| 63 | + // Replace version number |
| 64 | + let versionReplaced = 0; |
| 65 | + const walker = document.createTreeWalker( |
| 66 | + commentGroup, |
| 67 | + NodeFilter.SHOW_TEXT, |
| 68 | + null |
| 69 | + ); |
| 70 | + const nodes = []; |
| 71 | + while (walker.nextNode()) { |
| 72 | + nodes.push(walker.currentNode); |
| 73 | + } |
| 74 | + nodes.forEach((node) => { |
| 75 | + if (node.textContent && node.textContent.match(/v\d+\.\d+/)) { |
| 76 | + node.textContent = node.textContent.replace( |
| 77 | + /v[\d\.\-a-z]+/g, |
| 78 | + newVersion |
| 79 | + ); |
| 80 | + versionReplaced++; |
| 81 | + } |
| 82 | + }); |
| 83 | + return { versionReplaced }; |
| 84 | + }, version); |
| 85 | + |
| 86 | + await page.waitForTimeout(500); |
| 87 | + |
| 88 | + // Calculate bounding box that includes avatar and comment |
| 89 | + const bounds = await page.evaluate(() => { |
| 90 | + const links = document.querySelectorAll( |
| 91 | + 'a[href="/apps/github-actions"]' |
| 92 | + ); |
| 93 | + let timelineItem = null; |
| 94 | + for (const link of links) { |
| 95 | + timelineItem = link.closest(".TimelineItem"); |
| 96 | + if (timelineItem) break; |
| 97 | + } |
| 98 | + |
| 99 | + if (!timelineItem) return null; |
| 100 | + |
| 101 | + const avatar = timelineItem.querySelector(".TimelineItem-avatar"); |
| 102 | + const comment = timelineItem.querySelector(".timeline-comment"); |
| 103 | + |
| 104 | + if (!avatar || !comment) return null; |
| 105 | + |
| 106 | + const avatarRect = avatar.getBoundingClientRect(); |
| 107 | + const commentRect = comment.getBoundingClientRect(); |
| 108 | + |
| 109 | + return { |
| 110 | + x: Math.min(avatarRect.left, commentRect.left), |
| 111 | + y: Math.min(avatarRect.top, commentRect.top), |
| 112 | + right: Math.max(avatarRect.right, commentRect.right), |
| 113 | + bottom: Math.max(avatarRect.bottom, commentRect.bottom), |
| 114 | + }; |
| 115 | + }); |
| 116 | + |
| 117 | + if (!bounds) { |
| 118 | + throw new Error("Could not calculate bounds"); |
| 119 | + } |
| 120 | + |
| 121 | + const width = bounds.right - bounds.x; |
| 122 | + const height = bounds.bottom - bounds.y; |
| 123 | + |
| 124 | + console.log("Bounds:", { x: bounds.x, y: bounds.y, width, height }); |
| 125 | + |
| 126 | + const clipWidth = Math.round(width + 20); |
| 127 | + const clipHeight = Math.round(height + 20); |
| 128 | + |
| 129 | + console.log("Taking screenshot..."); |
| 130 | + await page.screenshot({ |
| 131 | + path: "sample-preview-link.png", |
| 132 | + clip: { |
| 133 | + x: Math.max(0, bounds.x - 10), |
| 134 | + y: Math.max(0, bounds.y - 10), |
| 135 | + width: clipWidth, |
| 136 | + height: clipHeight, |
| 137 | + }, |
| 138 | + }); |
| 139 | + |
| 140 | + const imageBuffer = readFileSync("sample-preview-link.png"); |
| 141 | + const dimensions = imageSize(imageBuffer); |
| 142 | + console.log( |
| 143 | + `Screenshot saved to sample-preview-link.png (${dimensions.width}x${dimensions.height}px)` |
| 144 | + ); |
| 145 | + |
| 146 | + await browser.close(); |
| 147 | +} |
| 148 | + |
| 149 | +main().catch((error) => { |
| 150 | + console.error(error); |
| 151 | + process.exit(1); |
| 152 | +}); |
0 commit comments