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

report.mjs

82 lines2.2 KB

1import { execFileSync } from 'node:child_process'
2import fs from 'node:fs'
3import path from 'node:path'
4import { fileURLToPath, pathToFileURL } from 'node:url'
5import { parseArgs } from 'node:util'
6​
7const repoRoot = fileURLToPath(new URL('../../../', import.meta.url))
8​
9function getGitStatus() {
10 try {
11 return {
12 branch: execFileSync('git', ['branch', '--show-current'], {
13 cwd: repoRoot,
14 encoding: 'utf8',
15 }).trim(),
16 dirty:
17 execFileSync('git', ['status', '--porcelain'], {
18 cwd: repoRoot,
19 encoding: 'utf8',
20 }).trim().length > 0,
21 }
22 } catch {
23 return { branch: '', dirty: undefined }
24 }
26​
27/**
28 * Attach the current checkout's metadata after Nx has restored measurements.
29 * The measurement time and duration still describe the original build.
30 * @param {string} resultsDir
31 * @param {{ sha?: string }} [options]
32 */
33export function writeReport(resultsDir, { sha: providedSha } = {}) {
34 const measurements = JSON.parse(
35 fs.readFileSync(path.join(resultsDir, 'measurements.json'), 'utf8'),
36 )
37 const sha =
38 providedSha ||
39 process.env.GITHUB_SHA ||
40 execFileSync('git', ['rev-parse', 'HEAD'], {
41 cwd: repoRoot,
42 encoding: 'utf8',
43 }).trim()
44 const report = {
45 ...measurements,
46 generatedAt: new Date().toISOString(),
47 sha,
48 status: {
49 ...measurements.status,
50 git: { sha, ...getGitStatus() },
51 },
52 }
53 fs.writeFileSync(
54 path.join(resultsDir, 'current.json'),
55 JSON.stringify(report, null, 2) + '\n',
56 )
57 return report
59​
60if (
61 process.argv[1] &&
62 import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href
63) {
64 try {
65 const { values } = parseArgs({
66 options: {
67 'results-dir': { type: 'string' },
68 sha: { type: 'string' },
69 },
70 })
71 const resultsDir = values['results-dir']
72 ? path.resolve(values['results-dir'])
73 : path.join(repoRoot, 'benchmarks/bundle-size/results')
74 writeReport(resultsDir, { sha: values.sha })
75 console.log(
76 `Wrote ${path.relative(repoRoot, path.join(resultsDir, 'current.json'))}`,
77 )
78 } catch (error) {
79 console.error(error instanceof Error ? error.message : error)
80 process.exitCode = 1
81 }