@allons-y/actions-file-diff 0.0.0-stage → 0.3.0

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.
Files changed (4) hide show
  1. package/LICENSE +385 -0
  2. package/README.md +316 -2
  3. package/dist/index.js +31047 -0
  4. package/package.json +76 -5
package/README.md CHANGED
@@ -1,3 +1,317 @@
1
- # Temporary Holding Version
1
+ <!-- weaver:header:START -->
2
+ <!-- auto-generated by Weaver — do not edit between these markers -->
2
3
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
4
+ <p align="center">
5
+ <a href="https://allons-y.studio">
6
+ <img id="user-content-logo" src="https://raw.githubusercontent.com/allonsy-studio/.github/main/assets/png/oss-banner.png" alt="Brought to you by Allons-y Studio" width="1500">
7
+ </a>
8
+ </p>
9
+
10
+ <h1 align="center" border="none">actions-file-diff</h1>
11
+
12
+ <p align="center">
13
+ <img src="https://github.com/allonsy-studio/.github/blob/main/assets/tag-rule.gif" alt="" height="30" />
14
+ </p>
15
+
16
+ <p align="center"><b>Compare file sizes between branches and report the diff back on the pull request.</b></p>
17
+
18
+ <p align="center">
19
+ <a href="https://www.npmjs.com/package/@allons-y/actions-file-diff">
20
+ <img src="https://img.shields.io/npm/v/@allons-y/actions-file-diff.svg" alt="NPM version" height="20" />
21
+ </a>
22
+ <a href="https://www.npmjs.com/package/@allons-y/actions-file-diff">
23
+ <img src="https://img.shields.io/npm/dw/@allons-y/actions-file-diff.svg" alt="NPM downloads" height="20" />
24
+ </a>
25
+ <a href="https://github.com/allonsy-studio/actions-file-diff/actions/workflows/ci.yml">
26
+ <img src="https://github.com/allonsy-studio/actions-file-diff/actions/workflows/ci.yml/badge.svg?branch=main" alt="CI" height="20" />
27
+ </a>
28
+ <a href="https://github.com/allonsy-studio/actions-file-diff/blob/main/LICENSE">
29
+ <img src="https://img.shields.io/github/license/allonsy-studio/actions-file-diff.svg" alt="License" height="20" />
30
+ </a>
31
+ <a href="https://allons-y.studio">
32
+ <img src="https://github.com/allonsy-studio/.github/blob/main/assets/svg/badge.svg" alt="Brought to you by Allons-y Studio" height="20" />
33
+ </a>
34
+ </p>
35
+
36
+ <!-- weaver:header:END -->
37
+
38
+ ## Usage
39
+
40
+ The action measures files that already exist on disk and compares the head tree against a base tree. It never checks out or builds anything itself, so the caller is responsible for producing both trees before running it. This keeps the action independent of your package manager and build tooling.
41
+
42
+ A complete pull request workflow that builds the head, builds the base into a second directory, and posts the comparison as a sticky comment:
43
+
44
+ ```yaml
45
+ name: File metrics
46
+
47
+ on:
48
+ pull_request:
49
+
50
+ permissions:
51
+ contents: read
52
+ pull-requests: write # required to post and update the PR comment
53
+
54
+ jobs:
55
+ file-diff:
56
+ runs-on: ubuntu-latest
57
+ steps:
58
+ # (a) Check out and build the pull request head.
59
+ - uses: actions/checkout@v6
60
+ with:
61
+ persist-credentials: false
62
+ - uses: actions/setup-node@v6
63
+ with:
64
+ node-version: 24
65
+ cache: npm
66
+ - run: npm ci
67
+ - run: npm run build
68
+
69
+ # (b) Check out and build the base ref into a second directory.
70
+ - uses: actions/checkout@v6
71
+ with:
72
+ ref: ${{ github.base_ref }}
73
+ path: base
74
+ persist-credentials: false
75
+ - run: npm ci
76
+ working-directory: base
77
+ - run: npm run build
78
+ working-directory: base
79
+
80
+ # (c) Compare the two trees and report on the pull request.
81
+ - uses: allonsy-studio/actions-file-diff@v0
82
+ with:
83
+ head-path: ${{ github.workspace }}
84
+ base-path: ${{ github.workspace }}/base
85
+ # The base checkout lives inside the workspace, so exclude it
86
+ # (and installed dependencies) from the head tree.
87
+ file-glob-pattern: |
88
+ **/dist/**
89
+ !base/**
90
+ !**/node_modules/**
91
+ ```
92
+
93
+ If `base-path` is omitted, or the directory does not exist, the action reports head totals only and `has-changed` is always `"false"`.
94
+
95
+ ## Requirements
96
+
97
+ - **`git` on the PATH.** Line diffs run `git diff --no-index`. It is preinstalled on GitHub-hosted runners. In a container or on a self-hosted runner, install it. Without it the action still reports sizes and images, skips the line diffs, and warns.
98
+ - **Built output, not source.** Line diffs are for compiled assets (your `dist/`), which are usually not committed or are minified, so the pull request's Files tab can't show them. Review source changes there as usual.
99
+ - **Regular files only.** The trees can come from an untrusted pull request, so symbolic links are never followed (a link could point outside the checkout). They are skipped with a warning.
100
+
101
+ ## Inputs
102
+
103
+ | Name | Required | Default | Description |
104
+ | ------------------- | -------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
105
+ | `head-path` | no | `${{ github.workspace }}` | Directory containing the head (pull request) tree to measure. |
106
+ | `base-path` | no | — | Directory containing the base tree to compare against. If omitted or missing, only head totals are reported. |
107
+ | `token` | no | `${{ github.token }}` | Token used to post the pull request comment. |
108
+ | `file-glob-pattern` | no | `**/dist/**` | Glob pattern(s) selecting the files to measure, resolved relative to `head-path` and `base-path` via [`@actions/glob`](https://github.com/actions/toolkit/tree/main/packages/glob). Newline-separated for multiple. Lines starting with `!` exclude, e.g. `!**/*.map`. |
109
+ | `build-dir` | no | `dist` | The path segment that separates a package from its built output. Files are grouped into a package named by the directory before this segment; the reported filename is the path after it. |
110
+ | `main-file` | no | `index.js` | Filename, relative to the build dir, treated as the package's main file. Used for the summary table. |
111
+ | `read-package-json` | no | `"true"` | When `"true"`, a package's `package.json` `main` field overrides `main-file` for that package. |
112
+ | `extensions` | no | — | Newline- or comma-separated list of base extensions to report, e.g. `css, js`. Empty reports all files. The `.min`, `.gz` and `.map` layers are stripped before matching, so `index.min.css.gz` counts as `css`. |
113
+ | `normalize-names` | no | — | Regular expressions, one per line, for the changing parts of output filenames, such as content hashes. Each match is replaced before base and head paths are compared, so `index-BxK3a9.js` and `index-Q7pL2m.js` pair as one modified file. See [Hashed and renamed files](#hashed-and-renamed-files). |
114
+ | `ignore-patterns` | no | — | Regular expressions, one per line, for generated text that changes on every build, such as random IDs. Matches are replaced on both sides before text files are compared and diffed. See [Random IDs and inline source maps](#random-ids-and-inline-source-maps). |
115
+ | `rename-threshold` | no | `50` | Minimum similarity, as a percentage, for pairing a file only on base with a file only on head that has the same extension, the way git detects renames. `0` turns it off. |
116
+ | `comment` | no | `"true"` | Whether to post or update the pull request comment. |
117
+ | `comment-header` | no | `## File metrics` | First line of the comment. Also used to derive the hidden marker that keeps the comment sticky across runs, so use distinct headers if you run the action more than once per pull request. |
118
+ | `visual-diff` | no | `summary` | Where the visual diff linked from the comment lives: `summary`, `branch` or `none`. See [Visual diff](#visual-diff). |
119
+ | `report-branch` | no | `file-diff-reports` | Branch the HTML report is committed to when `visual-diff` is `branch`. Created as an orphan branch when missing. |
120
+ | `pages-url` | no | — | Base URL that serves `report-branch`. Defaults to the repository's GitHub Pages URL. |
121
+ | `max-diff-size` | no | `262144` | Largest file, in bytes, that gets a line diff. Larger files are listed without one. |
122
+ | `max-diff-lines` | no | `20000` | Most diff lines kept for the whole report. A diff that would go over is left out of the report, with its line counts kept. `0` leaves out every diff. |
123
+
124
+ ## Outputs
125
+
126
+ | Name | Description |
127
+ | ------------- | ------------------------------------------------------------------------------------------------- |
128
+ | `has-changed` | `"true"` when the head total differs from the base total. Always `"false"` when there is no base. |
129
+ | `total-size` | Head total in bytes. |
130
+ | `report-url` | Link to the visual diff (published page or workflow run). Empty when `visual-diff` is `none`. |
131
+
132
+ ## Visual diff
133
+
134
+ The comment includes a stats line (files added, removed and modified; lines added and removed) and a link to a visual diff. Nothing is uploaded as a workflow artifact, so it uses no artifact storage.
135
+
136
+ The visual diff opens with summary cards (total size, files changed, lines, images), then has three parts:
137
+
138
+ 1. **Overview**: base and head size per file, grouped by package, with log-scale bars. File names link to their diff.
139
+ 2. **Code**: a diff per changed text file, with a file list, unified or split layout, and word-level highlights on the lines that were edited into one another. Minified, `.gz` and `.map` siblings are skipped. Binary files, unreadable files and files over `max-diff-size` are listed without a diff, with the reason. Once the report holds `max-diff-lines` lines, further diffs are left out but keep their +/− counts. Each diff is stored once in the page and its rows are built when the file opens, so a large report stays fast; find-in-page only searches files that are open.
140
+ 3. **Visual**: changed images (`png`, `jpg`, `gif`, `svg`, `webp`, `avif`, `ico`, `bmp`), compared side by side, as an overlay with an opacity slider, with a swipe slider (drag the image or use the range control), or as a difference blend. Background (checker, light, dark) and zoom (fit, 1×, 2×, 4×) controls; <kbd>J</kbd>/<kbd>K</kbd> switch images. Only shown when an image changed.
141
+
142
+ The full page (cards, tabs, split view and image compare modes) needs `visual-diff: branch`. The job summary shows the same data as collapsible sections without the interactive controls.
143
+
144
+ The pull request comment is shortened to fit GitHub's 65,536-character limit; when rows are left out it says so and links the full report.
145
+
146
+ ### `visual-diff: summary` (default)
147
+
148
+ - Writes the three parts as collapsible sections in the [job summary](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#adding-a-job-summary). The comment links to the workflow run.
149
+ - No extra permissions or setup.
150
+ - Limits: the summary has no tabs and can't show images (only a list of changed images), and GitHub caps it at 1 MiB, so long diffs are cut off with a note. It is kept for as long as the run's logs.
151
+
152
+ ### `visual-diff: branch`
153
+
154
+ - Commits a self-contained HTML page with tabs to `<report-branch>/pr-<number>/index.html`, plus changed images (up to 5 MB each). The job summary is still written.
155
+ - Each run replaces the branch with a single parentless commit, so older versions of a report don't pile up in the repository's history.
156
+ - When the pull request closes, the action deletes that directory. Add `closed` to the trigger types for this.
157
+ - Overlapping runs are detected and retried. If two land within milliseconds of each other, one report can be overwritten; it comes back on that pull request's next push.
158
+ - If the link in the comment is wrong (a custom domain, or your token can't read the Pages settings), set `pages-url`.
159
+ - If publishing fails (for example, a fork pull request with a read-only token), the action warns and links the job summary instead.
160
+
161
+ Setup:
162
+
163
+ 1. Grant `contents: write` (plus `pull-requests: write` for the comment).
164
+ 2. Turn on GitHub Pages: **Settings → Pages → Build and deployment → Deploy from a branch**, then pick `file-diff-reports` and `/ (root)`. The branch exists after the first run.
165
+ 3. Pages sites are public on public repositories. On private repositories, Pages visibility depends on your plan.
166
+
167
+ ```yaml
168
+ on:
169
+ pull_request:
170
+ types: [opened, synchronize, reopened, closed]
171
+
172
+ permissions:
173
+ contents: write # commit the report to file-diff-reports
174
+ pull-requests: write
175
+
176
+ jobs:
177
+ file-diff:
178
+ runs-on: ubuntu-latest
179
+ steps:
180
+ # ...build head and base as in Usage, skipped when the PR is closed:
181
+ # if: github.event.action != 'closed'
182
+ - uses: allonsy-studio/actions-file-diff@v0
183
+ with:
184
+ base-path: ${{ github.workspace }}/base
185
+ visual-diff: branch
186
+ ```
187
+
188
+ On a `closed` event the action only removes the report, so the build steps can be skipped with `if: github.event.action != 'closed'`.
189
+
190
+ ### Hashed and renamed files
191
+
192
+ Bundlers often put a content hash in output filenames, so an edited file gets a new name. Before comparing, the action pairs each file that is only on base with one that is only on head, then reports the pair as one modified file, with its size change and a line diff against the old name:
193
+
194
+ 1. **By name**, when `normalize-names` is set. A pair needs exactly one base file and one head file with the same name after replacement.
195
+ 2. **By content**, for the rest. Files with the same extension pair when they are at least `rename-threshold` percent alike. This covers names that are entirely a hash, such as Eleventy's `bundle/9pWhpkqfvF.css`. Binary files pair only when identical.
196
+
197
+ Content matching compares at most 200 files per side and extension, skips files over 1 MiB, and stops after 30 seconds with a warning. Files it didn't pair show as added and removed. `normalize-names` is faster, so use it when names follow a pattern.
198
+
199
+ ```yaml
200
+ normalize-names: |
201
+ -[A-Za-z0-9_-]{8}(?=\.)
202
+ ```
203
+
204
+ ### Random IDs and inline source maps
205
+
206
+ Some builds write different text on every run, even with no source change: WebC's generated IDs (`norn-header-webc-x0DiQ`) and inline source maps that embed a random input name are two examples. Set `ignore-patterns` to the regular expressions that match that text:
207
+
208
+ ```yaml
209
+ ignore-patterns: |
210
+ (?<=-webc-)[A-Za-z0-9_-]{5}
211
+ sourceMappingURL=data:application/json;base64,[A-Za-z0-9+/=]+
212
+ ```
213
+
214
+ Each match is replaced with `[ignored]` on both sides before a text file is compared. A file that differs only in matches is left out of the changed files, and the line diff of a file with other changes shows `[ignored]` in place of each match.
215
+
216
+ - It applies to text files up to `max-diff-size`. Larger and binary files compare byte for byte.
217
+ - Sizes in the table still count the real bytes, so a match that changes length can still show a size change.
218
+ - It needs `visual-diff` set to `summary` or `branch`, which is what lists the changed files.
219
+
220
+ ### How sizes are counted
221
+
222
+ - Files ending in `.map`, `.gz`, or `.min.*` are not reported as rows themselves; they appear as columns on their parent file's row.
223
+ - Each reported file counts as its minified sibling's size when one exists in the same tree (for example `index.css` counts as the size of `index.min.css`), otherwise its own size.
224
+ - `total-size` is the sum of those per-file sizes across all reported files.
225
+ - Human-readable sizes use 1024-byte units (`1 KB` = 1024 bytes).
226
+
227
+ ### Examples
228
+
229
+ Single-package repository whose build output lives in `build/`:
230
+
231
+ ```yaml
232
+ - uses: allonsy-studio/actions-file-diff@v0
233
+ with:
234
+ base-path: ${{ github.workspace }}/base
235
+ file-glob-pattern: |
236
+ **/build/**
237
+ !base/**
238
+ !**/node_modules/**
239
+ build-dir: build
240
+ main-file: main.js
241
+ ```
242
+
243
+ CSS-only report:
244
+
245
+ ```yaml
246
+ - uses: allonsy-studio/actions-file-diff@v0
247
+ with:
248
+ base-path: ${{ github.workspace }}/base
249
+ extensions: css
250
+ ```
251
+
252
+ Excluding sourcemaps with a `!` pattern:
253
+
254
+ ```yaml
255
+ - uses: allonsy-studio/actions-file-diff@v0
256
+ with:
257
+ base-path: ${{ github.workspace }}/base
258
+ file-glob-pattern: |
259
+ **/dist/**
260
+ !**/*.map
261
+ !base/**
262
+ ```
263
+
264
+ ## Development
265
+
266
+ Plain JavaScript, no TypeScript. The source under `src/` is bundled with esbuild into `dist/index.js`, which is what `action.yml` runs; the bundle is committed so consumers get the action's dependencies without an install step.
267
+
268
+ ```sh
269
+ yarn lint # opx lint: JS, JSON, Markdown, and license headers
270
+ yarn format # opx lint --fix
271
+ yarn test # vitest (line diffs need git on the PATH)
272
+ yarn coverage # vitest with per-file coverage thresholds
273
+ yarn test:browser # drives the report page in Chrome (CHROME_PATH, or the installed Chrome)
274
+ yarn build # esbuild -> dist/index.js (committed; CI fails if stale)
275
+ yarn preview # render the visual diff page from a sample fixture into preview/
276
+ yarn preview --serve # serve it at http://localhost:4173 with live reload
277
+ ```
278
+
279
+ ### Styling the visual diff page
280
+
281
+ - Styles live in `src/report-page.css`. They're bundled as text (esbuild `--loader:.css=text`), so the published page stays a single self-contained file.
282
+ - Colors, radius and fonts are custom properties on `:root`, with dark values in a `prefers-color-scheme` block.
283
+ - `yarn preview --serve` re-renders on any change under `src/`. The sample data (`scripts/preview-fixture.js`) covers modified, added and removed files, a removed package, a binary file, a file over `max-diff-size`, and PNG/SVG image changes.
284
+ - The preview has two variants, `/base/` and `/no-base/`. Each also writes the `comment.md` and `summary.md` the action would post.
285
+
286
+ Run `yarn build` and commit `dist/index.js` whenever `src/` or a dependency changes. The self-test workflow (`.github/workflows/self-test.yml`) rebuilds the bundle, fails if the committed copy is stale, and runs the action against a fixture tree (`scripts/make-fixture.sh`) on every pull request, asserting the `has-changed` and `total-size` outputs.
287
+
288
+ <!-- weaver:contribution:START -->
289
+ <!-- auto-generated by Weaver — do not edit between these markers -->
290
+
291
+ <h2>Contributing</h2>
292
+
293
+ Contributions are welcome & appreciated! Please open an [issue](https://github.com/allonsy-studio/actions-file-diff/issues/new) (before submitting a pull request) if you find a bug, have a feature request, or have a question. Be sure to follow the [contribution guidelines](https://github.com/allonsy-studio/.github/blob/main/CONTRIBUTING.md) and [code of conduct](https://github.com/allonsy-studio/.github/blob/main/CODE_OF_CONDUCT.md) but most importantly, be excellent to each other. 💖
294
+
295
+ <!-- weaver:contribution:END -->
296
+
297
+ <!-- weaver:license_apache:START -->
298
+ <!-- auto-generated by Weaver — do not edit between these markers -->
299
+
300
+ <h2>License</h2>
301
+
302
+ This project is licensed under the MPL-2.0 License - see the [LICENSE](LICENSE) file for details. This means you can use this however you like as long as you provide attribution back to this one. It's nice to share but it's also nice to get credit for your work. 😉
303
+
304
+ <!-- weaver:license_apache:END -->
305
+
306
+ <!-- weaver:funding:START -->
307
+ <!-- auto-generated by Weaver — do not edit between these markers -->
308
+
309
+ <h2>Funding ☕️</h2>
310
+
311
+ If you find this plugin useful and would like to buy me a coffee/beer as a small thank you, I would greatly appreciate it!
312
+
313
+ <a href="https://www.buymeacoffee.com/castastrophe">
314
+ <img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" height="40"/>
315
+ </a>
316
+
317
+ <!-- weaver:funding:END -->