Skip to content

Commit 2baddd0

Browse files
committed
feat(comparators): compare more generators
1 parent 5a53779 commit 2baddd0

File tree

8 files changed

+68
-60
lines changed

8 files changed

+68
-60
lines changed

.github/workflows/generate.yml

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,42 @@ jobs:
7474
runs-on: ubuntu-latest
7575
needs: prepare
7676
strategy:
77+
fail-fast: false
7778
matrix:
7879
include:
7980
- target: man-page
80-
input: './node/doc/api/cli.md'
81+
input: 'doc/api/cli.md'
82+
8183
- target: addon-verify
82-
input: './node/doc/api/addons.md'
84+
input: 'doc/api/addons.md'
85+
8386
- target: api-links
84-
input: './node/lib/*.js'
87+
input: 'lib/*.js'
88+
compare: object-assertion
89+
8590
- target: orama-db
86-
input: './node/doc/api/*.md'
91+
input: 'doc/api/*.md'
92+
compare: file-size
93+
8794
- target: json-simple
88-
input: './node/doc/api/*.md'
95+
input: 'doc/api/*.md'
96+
compare: object-assertion
97+
8998
- target: legacy-json
90-
input: './node/doc/api/*.md'
91-
compare: true
99+
input: 'doc/api/*.md'
100+
compare: object-assertion
101+
92102
- target: legacy-html
93-
input: './node/doc/api/*.md'
103+
input: 'doc/api/*.md'
104+
compare: file-size
105+
94106
- target: web
95-
input: './node/doc/api/*.md'
96-
compare: true
97-
- target: llms-txt
98-
input: './node/doc/api/*.md'
99-
fail-fast: false
107+
input: 'doc/api/*.md'
108+
compare: file-size
100109

110+
- target: llms-txt
111+
input: 'doc/api/*.md'
112+
compare: file-size
101113
steps:
102114
- name: Harden Runner
103115
uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
@@ -116,9 +128,9 @@ jobs:
116128
repository: nodejs/node
117129
ref: ${{ needs.prepare.outputs.sha }}
118130
sparse-checkout: |
119-
doc/api
120-
lib
121-
.
131+
${{ matrix.input }}
132+
CHANGELOG.md
133+
doc/api/index.md
122134
path: node
123135

124136
- name: Setup Node.js
@@ -154,8 +166,10 @@ jobs:
154166

155167
- name: Compare to base branch
156168
if: ${{ matrix.compare && needs.prepare.outputs.base-run }}
169+
env:
170+
GENERATOR: ${{ matrix.target }}
157171
run: |
158-
node scripts/compare-builds/${{ matrix.target }}.mjs > out/comparison.txt
172+
node scripts/comparators/${{ matrix.compare }}.mjs > out/comparison.txt
159173
160174
- name: Upload ${{ matrix.target }} artifacts
161175
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0

docs/comparators.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ Comparators are scripts that:
2020

2121
## Comparator Structure
2222

23-
Comparators are standalone ESM scripts located in `scripts/compare-builds/`:
23+
Comparators are standalone ESM scripts located in `scripts/comparators/`:
2424

2525
```
26-
scripts/compare-builds/
27-
├── utils.mjs # Shared utilities (BASE, HEAD paths)
28-
├── legacy-json.mjs # Compare legacy JSON output
29-
├── web.mjs # Compare web bundle sizes
30-
└── your-comparator.mjs # Your new comparator
26+
scripts/comparators/
27+
├── constants.mjs # Shared constants (BASE, HEAD, TITLE paths)
28+
├── file-size.mjs # Compare file sizes between builds
29+
├── object-assertion.mjs # Deep equality assertion for JSON objects
30+
└── your-comparator.mjs # Your new comparator
3131
```
3232

3333
### Naming Convention
3434

35-
**Each comparator must have the same name as the generator it compares.** For example:
35+
Comparators can be reused across multiple generators. You specify which comparator to use in the workflow file using the `compare` field. For example:
3636

37-
- `web.mjs` compares output from the `web` generator
38-
- `legacy-json.mjs` compares output from the `legacy-json` generator
39-
- `my-format.mjs` would compare output from a `my-format` generator
37+
- `file-size.mjs` can compare output from `web`, `legacy-html`, or any generator
38+
- `object-assertion.mjs` can compare JSON output from `legacy-json`, `json-simple`, etc.
39+
- `my-comparator.mjs` would be a custom comparator for specific needs
4040

4141
## Creating a Comparator
4242

@@ -48,7 +48,7 @@ Create a new file in `scripts/compare-builds/` with the same name as your genera
4848
// scripts/compare-builds/my-format.mjs
4949
import { readdir, readFile } from 'node:fs/promises';
5050
import { join } from 'node:path';
51-
import { BASE, HEAD } from './utils.mjs';
51+
import { BASE, HEAD, TITLE } from './utils.mjs';
5252

5353
// Fetch files from both directories
5454
const [baseFiles, headFiles] = await Promise.all([BASE, HEAD].map(() => await readdir(dir)));
@@ -105,7 +105,7 @@ const differences = results.filter(Boolean);
105105

106106
// Output markdown results
107107
if (differences.length > 0) {
108-
console.log('## `my-format` Generator');
108+
console.log(TITLE);
109109
console.log('');
110110
console.log(`Found ${differences.length} difference(s):`);
111111
console.log('');
@@ -161,5 +161,5 @@ node scripts/compare-builds/my-format.mjs
161161

162162
The comparator will automatically run in GitHub Actions when:
163163

164-
1. Your generator is configured with `compare: true` in the workflow
164+
1. Your generator is configured with `compare: <my-comparator>` in the workflow
165165
2. The comparator filename matches the generator name

npm-shrinkwrap.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/comparators/constants.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { fileURLToPath } from 'node:url';
2+
3+
export const BASE =
4+
process.env.HEAD || fileURLToPath(import.meta.resolve('../../base'));
5+
6+
export const HEAD =
7+
process.env.BASE || fileURLToPath(import.meta.resolve('../../out'));
8+
9+
export const TITLE =
10+
process.env.TITLE || `## \`${process.env.GENERATOR ?? '...'}\` Generator`;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { stat, readdir } from 'node:fs/promises';
22
import path from 'node:path';
33

4-
import { BASE, HEAD } from './utils.mjs';
4+
import { BASE, HEAD, TITLE } from './constants.mjs';
55

66
const UNITS = ['B', 'KB', 'MB', 'GB'];
77

@@ -63,7 +63,7 @@ if (changed.length) {
6363
return `| \`${file}\` | ${formatBytes(base)} | ${formatBytes(head)} | ${diffFormatted} |`;
6464
});
6565

66-
console.log('## Web Generator');
66+
console.log(TITLE);
6767
console.log('| File | Base | Head | Diff |');
6868
console.log('|-|-|-|-|');
6969
console.log(rows.join('\n'));
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'node:assert';
22
import { readdir, readFile } from 'node:fs/promises';
33
import { join } from 'node:path';
44

5-
import { BASE, HEAD } from './utils.mjs';
5+
import { BASE, HEAD, TITLE } from './constants.mjs';
66

77
const files = await readdir(BASE);
88

@@ -29,6 +29,6 @@ const results = await Promise.all(files.map(getFileDiff));
2929
const filteredResults = results.filter(Boolean);
3030

3131
if (filteredResults.length) {
32-
console.log('## `legacy-json` generator');
32+
console.log(TITLE);
3333
console.log(filteredResults.join('\n'));
3434
}

scripts/compare-builds/utils.mjs

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/generators/legacy-html/template.html

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,7 @@
1313
<link rel="stylesheet" href="assets/style.css" />
1414
<link rel="canonical" href="https://nodejs.org/api/__FILENAME__.html" />
1515
<script async defer src="assets/api.js" type="text/javascript"></script>
16-
<script>
17-
const storedTheme = localStorage.getItem('theme');
18-
19-
// Follow operating system theme preference
20-
if (storedTheme === null && window.matchMedia) {
21-
const mq = window.matchMedia('(prefers-color-scheme: dark)');
22-
if (mq.matches) {
23-
document.documentElement.classList.add('dark-mode');
24-
}
25-
} else if (storedTheme === 'dark') {
26-
document.documentElement.classList.add('dark-mode');
27-
}
28-
</script>
16+
<script>(localStorage.getItem('theme') === 'dark' || (localStorage.getItem('theme') === null && window.matchMedia?.('(prefers-color-scheme: dark)').matches)) && document.documentElement.classList.add('dark-mode');</script>
2917
</head>
3018
<body class="alt apidoc" id="api-section-__FILENAME__">
3119
<a href="#apicontent" class="skip-to-content">Skip to content</a>

0 commit comments

Comments
 (0)