Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

history.mjs

85 lines2.2 KB

1#!/usr/bin/env node
2​
3import fs from 'node:fs'
4import { execFileSync } from 'node:child_process'
5import vm from 'node:vm'
6import { parseArgs } from 'node:util'
7​
8const HISTORY_PATH = 'benchmarks/bundle-size/data.js'
9​
10const { values } = parseArgs({
11 allowPositionals: false,
12 options: {
13 history: { type: 'string' },
14 id: { type: 'string' },
15 'top-deltas': { type: 'string', default: '20' },
16 json: { type: 'boolean' },
17 },
18})
19​
20function parseHistory(raw) {
21 const trimmed = raw.trim()
22 if (trimmed.startsWith('window.BENCHMARK_DATA')) {
23 const sandbox = { window: {} }
24 vm.runInNewContext(trimmed, sandbox, { timeout: 1000 })
25 return sandbox.window.BENCHMARK_DATA
26 }
27 return JSON.parse(trimmed)
29​
30function readHistoryFromGit() {
31 for (const ref of ['origin/gh-pages', 'gh-pages']) {
32 try {
33 return execFileSync('git', ['show', `${ref}:${HISTORY_PATH}`], {
34 encoding: 'utf8',
35 })
36 } catch {}
37 }
38​
39 throw new Error(
40 `Could not read ${HISTORY_PATH} from origin/gh-pages or gh-pages. Run: git fetch origin gh-pages`,
41 )
43​
44const raw = values.history
45 ? fs.readFileSync(values.history, 'utf8')
46 : readHistoryFromGit()
47const history = parseHistory(raw)
48const entries = history.entries?.['Bundle Size (gzip)'] || []
49const previous = new Map()
50const deltas = []
51​
52for (const entry of entries) {
53 for (const bench of entry.benches || []) {
54 if (values.id && bench.name !== values.id) {
55 continue
56 }
57​
58 const prior = previous.get(bench.name)
59 if (prior !== undefined && prior !== bench.value) {
60 deltas.push({
61 id: bench.name,
62 delta: bench.value - prior,
63 value: bench.value,
64 sha: entry.commit?.id,
65 message: String(entry.commit?.message || '').split('\n')[0],
66 timestamp: entry.commit?.timestamp,
67 })
68 }
69​
70 previous.set(bench.name, bench.value)
71 }
73​
74deltas.sort((a, b) => Math.abs(b.delta) - Math.abs(a.delta))
75const rows = deltas.slice(0, Number.parseInt(values['top-deltas'], 10))
76​
77if (values.json) {
78 process.stdout.write(JSON.stringify(rows, null, 2) + '\n')
79} else {
80 for (const row of rows) {
81 process.stdout.write(
82 `${row.id} ${row.delta >= 0 ? '+' : ''}${row.delta} => ${row.value} ${row.sha?.slice(0, 12)} ${row.message}\n`,
83 )
84 }