pr-evidence 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +0 -0
- package/dist/evidenceComment.js +11 -0
- package/package.json +1 -1
- package/dist/gifBuilder.js +0 -106
package/dist/cli.js
CHANGED
|
File without changes
|
package/dist/evidenceComment.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export const commentMarker = "<!-- pr-evidence -->";
|
|
2
|
+
const npmPackageUrl = "https://www.npmjs.com/package/pr-evidence";
|
|
2
3
|
/** Makes untrusted text safe for one line of Markdown, including inside a table cell. */
|
|
3
4
|
function escapeInline(text) {
|
|
4
5
|
return text
|
|
@@ -85,6 +86,15 @@ function renderOtherScreenshots({ outcome, screenshotUrls }) {
|
|
|
85
86
|
"</details>",
|
|
86
87
|
];
|
|
87
88
|
}
|
|
89
|
+
/** A small plug so reviewers who like the comment can find the tool. */
|
|
90
|
+
function renderFooter() {
|
|
91
|
+
return [
|
|
92
|
+
"",
|
|
93
|
+
"---",
|
|
94
|
+
"",
|
|
95
|
+
`<sub>Want to try it yourself? Check out [pr-evidence on npm](${npmPackageUrl}).</sub>`,
|
|
96
|
+
];
|
|
97
|
+
}
|
|
88
98
|
export function renderEvidenceComment(input) {
|
|
89
99
|
return [
|
|
90
100
|
commentMarker,
|
|
@@ -92,6 +102,7 @@ export function renderEvidenceComment(input) {
|
|
|
92
102
|
...renderTable(input.outcome),
|
|
93
103
|
...input.outcome.criteria.flatMap((criterion) => renderCriterionSection(criterion, input.screenshotUrls)),
|
|
94
104
|
...renderOtherScreenshots(input),
|
|
105
|
+
...renderFooter(),
|
|
95
106
|
"",
|
|
96
107
|
].join("\n");
|
|
97
108
|
}
|
package/package.json
CHANGED
package/dist/gifBuilder.js
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
|
-
import gifenc from "gifenc";
|
|
3
|
-
import { PNG } from "pngjs";
|
|
4
|
-
import { CliError } from "./errors.js";
|
|
5
|
-
import { imageLimitBytes } from "./uploader.js";
|
|
6
|
-
const stepDelayMilliseconds = 1500;
|
|
7
|
-
const maximumGifDimension = 65535;
|
|
8
|
-
const maximumColors = 256;
|
|
9
|
-
const candidateWidths = [1280, 960, 720, 480];
|
|
10
|
-
// About 50x faster than the default on busy images, and fine for UI screenshots.
|
|
11
|
-
const colorFormat = "rgb444";
|
|
12
|
-
function decodeScreenshot(path) {
|
|
13
|
-
try {
|
|
14
|
-
const image = PNG.sync.read(readFileSync(path));
|
|
15
|
-
return { width: image.width, height: image.height, rgba: image.data };
|
|
16
|
-
}
|
|
17
|
-
catch (cause) {
|
|
18
|
-
throw new CliError("screenshot_invalid", `${path} is not a readable PNG: ${String(cause)}`);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
/** Averages the source pixels behind one canvas pixel, which keeps small text legible. */
|
|
22
|
-
function averageSourceBlock(source, startX, endX, startY, endY, target, targetOffset) {
|
|
23
|
-
let red = 0;
|
|
24
|
-
let green = 0;
|
|
25
|
-
let blue = 0;
|
|
26
|
-
for (let y = startY; y < endY; y += 1) {
|
|
27
|
-
for (let x = startX; x < endX; x += 1) {
|
|
28
|
-
const offset = (y * source.width + x) * 4;
|
|
29
|
-
red += source.rgba[offset];
|
|
30
|
-
green += source.rgba[offset + 1];
|
|
31
|
-
blue += source.rgba[offset + 2];
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
const pixelCount = (endX - startX) * (endY - startY);
|
|
35
|
-
target[targetOffset] = red / pixelCount;
|
|
36
|
-
target[targetOffset + 1] = green / pixelCount;
|
|
37
|
-
target[targetOffset + 2] = blue / pixelCount;
|
|
38
|
-
target[targetOffset + 3] = 255;
|
|
39
|
-
}
|
|
40
|
-
/** Fits the image inside the canvas, centred on white, keeping its proportions. */
|
|
41
|
-
function fitToCanvas(source, canvasWidth, canvasHeight) {
|
|
42
|
-
const scale = Math.min(canvasWidth / source.width, canvasHeight / source.height);
|
|
43
|
-
const fittedWidth = Math.max(1, Math.round(source.width * scale));
|
|
44
|
-
const fittedHeight = Math.max(1, Math.round(source.height * scale));
|
|
45
|
-
const leftMargin = Math.floor((canvasWidth - fittedWidth) / 2);
|
|
46
|
-
const topMargin = Math.floor((canvasHeight - fittedHeight) / 2);
|
|
47
|
-
const rgba = new Uint8Array(canvasWidth * canvasHeight * 4).fill(255);
|
|
48
|
-
for (let y = 0; y < fittedHeight; y += 1) {
|
|
49
|
-
const startY = Math.floor((y * source.height) / fittedHeight);
|
|
50
|
-
const endY = Math.max(startY + 1, Math.floor(((y + 1) * source.height) / fittedHeight));
|
|
51
|
-
for (let x = 0; x < fittedWidth; x += 1) {
|
|
52
|
-
const startX = Math.floor((x * source.width) / fittedWidth);
|
|
53
|
-
const endX = Math.max(startX + 1, Math.floor(((x + 1) * source.width) / fittedWidth));
|
|
54
|
-
const targetOffset = ((y + topMargin) * canvasWidth + x + leftMargin) * 4;
|
|
55
|
-
averageSourceBlock(source, startX, endX, startY, endY, rgba, targetOffset);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return { width: canvasWidth, height: canvasHeight, rgba };
|
|
59
|
-
}
|
|
60
|
-
function encodeGif(frames, width, height) {
|
|
61
|
-
const scale = Math.min(1, maximumGifDimension / width, maximumGifDimension / height);
|
|
62
|
-
const canvasWidth = Math.max(1, Math.round(width * scale));
|
|
63
|
-
const canvasHeight = Math.max(1, Math.round(height * scale));
|
|
64
|
-
const encoder = gifenc.GIFEncoder();
|
|
65
|
-
for (const frame of frames) {
|
|
66
|
-
const { rgba } = fitToCanvas(frame, canvasWidth, canvasHeight);
|
|
67
|
-
const palette = gifenc.quantize(rgba, maximumColors, { format: colorFormat });
|
|
68
|
-
encoder.writeFrame(gifenc.applyPalette(rgba, palette, colorFormat), canvasWidth, canvasHeight, {
|
|
69
|
-
palette,
|
|
70
|
-
delay: stepDelayMilliseconds,
|
|
71
|
-
repeat: 0,
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
encoder.finish();
|
|
75
|
-
return encoder.bytes();
|
|
76
|
-
}
|
|
77
|
-
/** Every second frame, always keeping the last so the final state is shown. */
|
|
78
|
-
function dropEverySecondFrame(frames) {
|
|
79
|
-
const kept = frames.filter((_frame, index) => index % 2 === 0);
|
|
80
|
-
const lastFrame = frames.at(-1);
|
|
81
|
-
return kept.at(-1) === lastFrame ? kept : [...kept, lastFrame];
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Writes the GIF, shrinking it until it fits GitHub's image limit: first by
|
|
85
|
-
* width, then by dropping steps.
|
|
86
|
-
*/
|
|
87
|
-
export function buildGif(screenshotPaths, outputPath) {
|
|
88
|
-
let frames = screenshotPaths.map(decodeScreenshot);
|
|
89
|
-
const fullWidth = Math.max(...frames.map((frame) => frame.width));
|
|
90
|
-
const fullHeight = Math.max(...frames.map((frame) => frame.height));
|
|
91
|
-
const widths = [...new Set(candidateWidths.map((width) => Math.min(width, fullWidth)))];
|
|
92
|
-
for (;;) {
|
|
93
|
-
for (const width of widths) {
|
|
94
|
-
const height = Math.max(1, Math.round((fullHeight * width) / fullWidth));
|
|
95
|
-
const bytes = encodeGif(frames, width, height);
|
|
96
|
-
if (bytes.length <= imageLimitBytes) {
|
|
97
|
-
writeFileSync(outputPath, bytes);
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (frames.length <= 2) {
|
|
102
|
-
throw new CliError("gif_too_large", "The GIF could not be kept under GitHub's image size limit.");
|
|
103
|
-
}
|
|
104
|
-
frames = dropEverySecondFrame(frames);
|
|
105
|
-
}
|
|
106
|
-
}
|