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

run.mjs

259 lines7.5 KB

1#!/usr/bin/env node
2​
3import { spawnSync } from 'node:child_process'
4import fs from 'node:fs'
5import path from 'node:path'
6import { fileURLToPath, pathToFileURL } from 'node:url'
7import { parseArgs } from 'node:util'
8​
9const scriptDir = path.dirname(fileURLToPath(import.meta.url))
10const repoRoot = path.resolve(scriptDir, '../../..')
11​
12/**
13 * @typedef {object} CommandOptions
14 * @property {string} cwd
15 * @property {NodeJS.ProcessEnv} env
16 * @property {'inherit' | ['ignore', number, number]} stdio
17 * @typedef {(command: string, args: string[], options: CommandOptions) => {status: number | null, error?: Error, signal?: string | null}} Execute
18 */
19​
20/**
21 * @param {string} resultsRoot
22 * @param {string} name
23 */
24function namedRunDir(resultsRoot, name) {
25 if (!/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/.test(name)) {
26 throw new Error(
27 'Run names must start with a letter or digit and contain only letters, digits, hyphens, or underscores.',
28 )
29 }
30 return path.join(resultsRoot, 'runs', name)
32​
33/**
34 * @param {string[]} argv
35 * @param {Execute} [execute]
36 */
37export function run(argv, execute = spawnSync) {
38 const separator = argv.indexOf('--')
39 const testArgs = separator === -1 ? [] : argv.slice(separator + 1)
40 const { values } = parseArgs({
41 args: separator === -1 ? argv : argv.slice(0, separator),
42 allowPositionals: false,
43 options: {
44 name: { type: 'string' },
45 baseline: { type: 'string' },
46 'test-projects': { type: 'string' },
47 scenario: { type: 'string' },
48 'results-dir': { type: 'string' },
49 'dist-dir': { type: 'string' },
50 analysis: { type: 'boolean' },
51 sourcemap: { type: 'boolean' },
52 'skip-package-builds': { type: 'boolean' },
53 help: { type: 'boolean', short: 'h' },
54 },
55 })
56​
57 if (values.help) {
58 console.log(`Usage: pnpm benchmark:bundle-size:run [options] [-- test arguments]
59​
60 --scenario <ids> Comma-separated scenarios; omit for all scenarios
61 --name <name> Save a named run; existing results are not overwritten
62 --baseline <name|file> Compare against a named run or a current.json file
63 --test-projects <names> Run Nx unit tests before measurement
64 --results-dir <dir> Results root (default: benchmarks/bundle-size/results)
65 --dist-dir <dir> Override the emitted bundle directory
66 --analysis Include source attribution in current.json
67 --sourcemap Emit hidden source maps
68 --skip-package-builds Reuse package builds only when they are unchanged
69​
70Named runs use <results-root>/runs/<name>/current.json.
71Without --name, current.json is replaced in the results root.
72Logs stay beside current.json; failures print a bounded log tail.
73Arguments after -- go to the selected unit tests, not the measurement.`)
74 return 0
75 }
76​
77 if (testArgs.length > 0 && !values['test-projects']) {
78 throw new Error('Test arguments after -- require --test-projects.')
79 }
80 if (
81 values['test-projects'] !== undefined &&
82 !values['test-projects'].split(',').some((name) => name.trim())
83 ) {
84 throw new Error('--test-projects requires at least one project.')
85 }
86 if (
87 values.scenario !== undefined &&
88 !values.scenario.split(',').some((name) => name.trim())
89 ) {
90 throw new Error('--scenario requires at least one scenario.')
91 }
92​
93 const resultsRoot = values['results-dir']
94 ? path.resolve(values['results-dir'])
95 : path.join(repoRoot, 'benchmarks/bundle-size/results')
96 const resultsDir =
97 values.name === undefined
98 ? resultsRoot
99 : namedRunDir(resultsRoot, values.name)
100 const currentPath = path.join(resultsDir, 'current.json')
101 let baselinePath
102​
103 if (values.name !== undefined && fs.existsSync(currentPath)) {
104 throw new Error(`Named run already exists: ${values.name}`)
105 }
106 if (values.baseline !== undefined) {
107 baselinePath = fs.realpathSync(
108 /[/\\]|\.json$/i.test(values.baseline)
109 ? path.resolve(values.baseline)
110 : path.join(namedRunDir(resultsRoot, values.baseline), 'current.json'),
111 )
112 const baseline = JSON.parse(fs.readFileSync(baselinePath, 'utf8'))
113 if (!Array.isArray(baseline.metrics) || baseline.metrics.length === 0) {
114 throw new Error(`Baseline contains no metrics: ${baselinePath}`)
115 }
116 if (
117 fs.existsSync(currentPath) &&
118 fs.realpathSync(currentPath) === baselinePath
119 ) {
120 throw new Error(
121 'The baseline cannot be the current output. Use --name to save it first.',
122 )
123 }
124 }
125​
126 fs.mkdirSync(resultsDir, { recursive: true })
127 const env = {
128 ...process.env,
129 CI: '1',
130 NX_DAEMON: 'false',
131 FORCE_COLOR: '0',
132 }
133​
134 /**
135 * @param {string} label
136 * @param {string} command
137 * @param {string[]} args
138 */
139 function step(label, command, args, logged = true) {
140 const logPath = path.join(resultsDir, `${label}.log`)
141 const fd = logged ? fs.openSync(logPath, 'w') : undefined
142 let result
143 try {
144 if (logged) {
145 console.error(`${label}: ${path.relative(repoRoot, logPath)}`)
146 }
147 result = execute(command, args, {
148 cwd: repoRoot,
149 env,
150 stdio: fd === undefined ? 'inherit' : ['ignore', fd, fd],
151 })
152 } finally {
153 if (fd !== undefined) {
154 fs.closeSync(fd)
155 }
156 }
157​
158 if (result.error || result.status !== 0) {
159 console.error(
160 `${label} failed: ${result.error?.message || result.signal || `exit ${result.status}`}`,
161 )
162 if (logged) {
163 const tail = fs
164 .readFileSync(logPath, 'utf8')
165 .slice(-8000)
166 .trimEnd()
167 .split(/\r?\n/)
168 .slice(-40)
169 .join('\n')
170 if (tail) {
171 console.error(tail)
172 }
173 }
174 return result.status || 1
175 }
176 return 0
177 }
178​
179 if (values['test-projects']) {
180 const pnpmPath = process.env.npm_execpath
181 const status = step(
182 'tests',
183 pnpmPath
184 ? process.execPath
185 : process.platform === 'win32'
186 ? 'pnpm.cmd'
187 : 'pnpm',
188 [
189 ...(pnpmPath ? [pnpmPath] : []),
190 'nx',
191 'run-many',
192 '--target=test:unit',
193 `--projects=${values['test-projects']}`,
194 '--parallel=1',
195 '--outputStyle=stream',
196 '--skipRemoteCache',
197 ...(testArgs.length > 0 ? ['--', ...testArgs] : []),
198 ],
199 )
200 if (status !== 0) {
201 return status
202 }
203 }
204​
205 const measureArgs = [
206 path.join(scriptDir, 'measure.mjs'),
207 '--results-dir',
208 resultsDir,
209 ]
210 for (const [option, value] of Object.entries({
211 scenario: values.scenario,
212 'dist-dir':
213 values['dist-dir'] === undefined
214 ? undefined
215 : path.resolve(values['dist-dir']),
216 })) {
217 if (value !== undefined) {
218 measureArgs.push(`--${option}`, value)
219 }
220 }
221 for (const [option, value] of Object.entries({
222 analysis: values.analysis,
223 sourcemap: values.sourcemap,
224 'skip-package-builds': values['skip-package-builds'],
225 })) {
226 if (value) {
227 measureArgs.push(`--${option}`)
228 }
229 }
230​
231 const status = step('measure', process.execPath, measureArgs)
232 if (status !== 0) {
233 return status
234 }
235​
236 return step(
237 'report',
238 process.execPath,
239 [
240 path.join(scriptDir, baselinePath ? 'diff.mjs' : 'query.mjs'),
241 '--current',
242 currentPath,
243 ...(baselinePath ? ['--baseline', baselinePath] : []),
244 ],
245 false,
246 )
248​
249if (
250 process.argv[1] &&
251 import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href
252) {
253 try {
254 process.exitCode = run(process.argv.slice(2))
255 } catch (error) {
256 console.error(error instanceof Error ? error.message : error)
257 process.exitCode = 1
258 }