modestbench 0.9.0 → 0.9.2

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 (224) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/cli/builder.cjs +259 -0
  3. package/dist/cli/builder.cjs.map +1 -0
  4. package/dist/cli/builder.d.cts +37 -0
  5. package/dist/cli/builder.d.cts.map +1 -0
  6. package/dist/cli/builder.d.ts +37 -0
  7. package/dist/cli/builder.d.ts.map +1 -0
  8. package/dist/cli/builder.js +255 -0
  9. package/dist/cli/builder.js.map +1 -0
  10. package/dist/cli/commands/baseline.cjs +4 -33
  11. package/dist/cli/commands/baseline.cjs.map +1 -1
  12. package/dist/cli/commands/baseline.d.cts.map +1 -1
  13. package/dist/cli/commands/baseline.d.ts.map +1 -1
  14. package/dist/cli/commands/baseline.js +2 -31
  15. package/dist/cli/commands/baseline.js.map +1 -1
  16. package/dist/cli/commands/history.cjs +2 -14
  17. package/dist/cli/commands/history.cjs.map +1 -1
  18. package/dist/cli/commands/history.d.cts.map +1 -1
  19. package/dist/cli/commands/history.d.ts.map +1 -1
  20. package/dist/cli/commands/history.js +1 -13
  21. package/dist/cli/commands/history.js.map +1 -1
  22. package/dist/cli/commands/init.cjs +2 -2
  23. package/dist/cli/commands/init.cjs.map +1 -1
  24. package/dist/cli/commands/init.js +2 -2
  25. package/dist/cli/commands/init.js.map +1 -1
  26. package/dist/cli/commands/run.cjs +3 -3
  27. package/dist/cli/commands/run.cjs.map +1 -1
  28. package/dist/cli/commands/run.d.cts +0 -13
  29. package/dist/cli/commands/run.d.cts.map +1 -1
  30. package/dist/cli/commands/run.d.ts +0 -13
  31. package/dist/cli/commands/run.d.ts.map +1 -1
  32. package/dist/cli/commands/run.js +1 -1
  33. package/dist/cli/commands/run.js.map +1 -1
  34. package/dist/cli/commands/test.cjs +1 -1
  35. package/dist/cli/commands/test.cjs.map +1 -1
  36. package/dist/cli/commands/test.js +1 -1
  37. package/dist/cli/commands/test.js.map +1 -1
  38. package/dist/cli/context.cjs +60 -0
  39. package/dist/cli/context.cjs.map +1 -0
  40. package/dist/cli/context.d.cts +28 -0
  41. package/dist/cli/context.d.cts.map +1 -0
  42. package/dist/cli/context.d.ts +28 -0
  43. package/dist/cli/context.d.ts.map +1 -0
  44. package/dist/cli/context.js +56 -0
  45. package/dist/cli/context.js.map +1 -0
  46. package/dist/cli/handlers.cjs +74 -0
  47. package/dist/cli/handlers.cjs.map +1 -0
  48. package/dist/cli/handlers.d.cts +13 -0
  49. package/dist/cli/handlers.d.cts.map +1 -0
  50. package/dist/cli/handlers.d.ts +13 -0
  51. package/dist/cli/handlers.d.ts.map +1 -0
  52. package/dist/cli/handlers.js +70 -0
  53. package/dist/cli/handlers.js.map +1 -0
  54. package/dist/cli/index.cjs +45 -978
  55. package/dist/cli/index.cjs.map +1 -1
  56. package/dist/cli/index.d.cts +6 -31
  57. package/dist/cli/index.d.cts.map +1 -1
  58. package/dist/cli/index.d.ts +6 -31
  59. package/dist/cli/index.d.ts.map +1 -1
  60. package/dist/cli/index.js +43 -974
  61. package/dist/cli/index.js.map +1 -1
  62. package/dist/cli/parsers/analyze.cjs +54 -0
  63. package/dist/cli/parsers/analyze.cjs.map +1 -0
  64. package/dist/cli/parsers/analyze.d.cts +37 -0
  65. package/dist/cli/parsers/analyze.d.cts.map +1 -0
  66. package/dist/cli/parsers/analyze.d.ts +37 -0
  67. package/dist/cli/parsers/analyze.d.ts.map +1 -0
  68. package/dist/cli/parsers/analyze.js +51 -0
  69. package/dist/cli/parsers/analyze.js.map +1 -0
  70. package/dist/cli/parsers/baseline.cjs +75 -0
  71. package/dist/cli/parsers/baseline.cjs.map +1 -0
  72. package/dist/cli/parsers/baseline.d.cts +59 -0
  73. package/dist/cli/parsers/baseline.d.cts.map +1 -0
  74. package/dist/cli/parsers/baseline.d.ts +59 -0
  75. package/dist/cli/parsers/baseline.d.ts.map +1 -0
  76. package/dist/cli/parsers/baseline.js +72 -0
  77. package/dist/cli/parsers/baseline.js.map +1 -0
  78. package/dist/cli/parsers/global.cjs +49 -0
  79. package/dist/cli/parsers/global.cjs.map +1 -0
  80. package/dist/cli/parsers/global.d.cts +45 -0
  81. package/dist/cli/parsers/global.d.cts.map +1 -0
  82. package/dist/cli/parsers/global.d.ts +45 -0
  83. package/dist/cli/parsers/global.d.ts.map +1 -0
  84. package/dist/cli/parsers/global.js +46 -0
  85. package/dist/cli/parsers/global.js.map +1 -0
  86. package/dist/cli/parsers/history.cjs +138 -0
  87. package/dist/cli/parsers/history.cjs.map +1 -0
  88. package/dist/cli/parsers/history.d.cts +108 -0
  89. package/dist/cli/parsers/history.d.cts.map +1 -0
  90. package/dist/cli/parsers/history.d.ts +108 -0
  91. package/dist/cli/parsers/history.d.ts.map +1 -0
  92. package/dist/cli/parsers/history.js +135 -0
  93. package/dist/cli/parsers/history.js.map +1 -0
  94. package/dist/cli/parsers/index.cjs +35 -0
  95. package/dist/cli/parsers/index.cjs.map +1 -0
  96. package/dist/cli/parsers/index.d.cts +15 -0
  97. package/dist/cli/parsers/index.d.cts.map +1 -0
  98. package/dist/cli/parsers/index.d.ts +15 -0
  99. package/dist/cli/parsers/index.d.ts.map +1 -0
  100. package/dist/cli/parsers/index.js +15 -0
  101. package/dist/cli/parsers/index.js.map +1 -0
  102. package/dist/cli/parsers/init.cjs +39 -0
  103. package/dist/cli/parsers/init.cjs.map +1 -0
  104. package/dist/cli/parsers/init.d.cts +32 -0
  105. package/dist/cli/parsers/init.d.cts.map +1 -0
  106. package/dist/cli/parsers/init.d.ts +32 -0
  107. package/dist/cli/parsers/init.d.ts.map +1 -0
  108. package/dist/cli/parsers/init.js +36 -0
  109. package/dist/cli/parsers/init.js.map +1 -0
  110. package/dist/cli/parsers/run.cjs +99 -0
  111. package/dist/cli/parsers/run.cjs.map +1 -0
  112. package/dist/cli/parsers/run.d.cts +62 -0
  113. package/dist/cli/parsers/run.d.cts.map +1 -0
  114. package/dist/cli/parsers/run.d.ts +62 -0
  115. package/dist/cli/parsers/run.d.ts.map +1 -0
  116. package/dist/cli/parsers/run.js +96 -0
  117. package/dist/cli/parsers/run.js.map +1 -0
  118. package/dist/cli/parsers/test.cjs +42 -0
  119. package/dist/cli/parsers/test.cjs.map +1 -0
  120. package/dist/cli/parsers/test.d.cts +31 -0
  121. package/dist/cli/parsers/test.d.cts.map +1 -0
  122. package/dist/cli/parsers/test.d.ts +31 -0
  123. package/dist/cli/parsers/test.d.ts.map +1 -0
  124. package/dist/cli/parsers/test.js +39 -0
  125. package/dist/cli/parsers/test.js.map +1 -0
  126. package/dist/cli/theme.cjs +35 -0
  127. package/dist/cli/theme.cjs.map +1 -0
  128. package/dist/cli/theme.d.cts +31 -0
  129. package/dist/cli/theme.d.cts.map +1 -0
  130. package/dist/cli/theme.d.ts +31 -0
  131. package/dist/cli/theme.d.ts.map +1 -0
  132. package/dist/cli/theme.js +32 -0
  133. package/dist/cli/theme.js.map +1 -0
  134. package/dist/config/budget-schema.cjs +1 -1
  135. package/dist/config/budget-schema.cjs.map +1 -1
  136. package/dist/config/budget-schema.js +1 -1
  137. package/dist/config/budget-schema.js.map +1 -1
  138. package/dist/core/engines/accurate-engine.cjs +1 -1
  139. package/dist/core/engines/accurate-engine.cjs.map +1 -1
  140. package/dist/core/engines/accurate-engine.d.cts.map +1 -1
  141. package/dist/core/engines/accurate-engine.d.ts.map +1 -1
  142. package/dist/core/engines/accurate-engine.js +1 -1
  143. package/dist/core/engines/accurate-engine.js.map +1 -1
  144. package/dist/errors/base.cjs +3 -12
  145. package/dist/errors/base.cjs.map +1 -1
  146. package/dist/errors/base.d.cts +0 -7
  147. package/dist/errors/base.d.cts.map +1 -1
  148. package/dist/errors/base.d.ts +0 -7
  149. package/dist/errors/base.d.ts.map +1 -1
  150. package/dist/errors/base.js +1 -9
  151. package/dist/errors/base.js.map +1 -1
  152. package/dist/formatters/history/compare.cjs +6 -6
  153. package/dist/formatters/history/compare.cjs.map +1 -1
  154. package/dist/formatters/history/compare.js +6 -6
  155. package/dist/formatters/history/compare.js.map +1 -1
  156. package/dist/formatters/history/show.cjs +2 -2
  157. package/dist/formatters/history/show.cjs.map +1 -1
  158. package/dist/formatters/history/show.js +2 -2
  159. package/dist/formatters/history/show.js.map +1 -1
  160. package/dist/formatters/history/trends.cjs +1 -1
  161. package/dist/formatters/history/trends.cjs.map +1 -1
  162. package/dist/formatters/history/trends.js +1 -1
  163. package/dist/formatters/history/trends.js.map +1 -1
  164. package/dist/reporters/human.cjs +3 -3
  165. package/dist/reporters/human.cjs.map +1 -1
  166. package/dist/reporters/human.d.cts.map +1 -1
  167. package/dist/reporters/human.d.ts.map +1 -1
  168. package/dist/reporters/human.js +3 -3
  169. package/dist/reporters/human.js.map +1 -1
  170. package/dist/reporters/nyan.cjs +1 -1
  171. package/dist/reporters/nyan.cjs.map +1 -1
  172. package/dist/reporters/nyan.js +1 -1
  173. package/dist/reporters/nyan.js.map +1 -1
  174. package/dist/services/budget-evaluator.cjs +2 -2
  175. package/dist/services/budget-evaluator.cjs.map +1 -1
  176. package/dist/services/budget-evaluator.js +2 -2
  177. package/dist/services/budget-evaluator.js.map +1 -1
  178. package/dist/services/config-manager.cjs +2 -2
  179. package/dist/services/config-manager.cjs.map +1 -1
  180. package/dist/services/config-manager.js +2 -2
  181. package/dist/services/config-manager.js.map +1 -1
  182. package/dist/services/profiler/profile-runner.cjs +11 -0
  183. package/dist/services/profiler/profile-runner.cjs.map +1 -1
  184. package/dist/services/profiler/profile-runner.d.cts +2 -0
  185. package/dist/services/profiler/profile-runner.d.cts.map +1 -1
  186. package/dist/services/profiler/profile-runner.d.ts +2 -0
  187. package/dist/services/profiler/profile-runner.d.ts.map +1 -1
  188. package/dist/services/profiler/profile-runner.js +11 -0
  189. package/dist/services/profiler/profile-runner.js.map +1 -1
  190. package/dist/services/reporter-registry.cjs +8 -8
  191. package/dist/services/reporter-registry.cjs.map +1 -1
  192. package/dist/services/reporter-registry.js +8 -8
  193. package/dist/services/reporter-registry.js.map +1 -1
  194. package/package.json +7 -9
  195. package/src/cli/builder.ts +387 -0
  196. package/src/cli/commands/baseline.ts +7 -33
  197. package/src/cli/commands/history.ts +1 -16
  198. package/src/cli/commands/init.ts +2 -2
  199. package/src/cli/commands/run.ts +1 -1
  200. package/src/cli/commands/test.ts +1 -1
  201. package/src/cli/context.ts +117 -0
  202. package/src/cli/handlers.ts +76 -0
  203. package/src/cli/index.ts +49 -1194
  204. package/src/cli/parsers/analyze.ts +61 -0
  205. package/src/cli/parsers/baseline.ts +92 -0
  206. package/src/cli/parsers/global.ts +51 -0
  207. package/src/cli/parsers/history.ts +168 -0
  208. package/src/cli/parsers/index.ts +28 -0
  209. package/src/cli/parsers/init.ts +45 -0
  210. package/src/cli/parsers/run.ts +118 -0
  211. package/src/cli/parsers/test.ts +46 -0
  212. package/src/cli/theme.ts +33 -0
  213. package/src/config/budget-schema.ts +1 -1
  214. package/src/core/engines/accurate-engine.ts +1 -1
  215. package/src/errors/base.ts +1 -10
  216. package/src/formatters/history/compare.ts +6 -6
  217. package/src/formatters/history/show.ts +2 -2
  218. package/src/formatters/history/trends.ts +1 -1
  219. package/src/reporters/human.ts +5 -3
  220. package/src/reporters/nyan.ts +1 -1
  221. package/src/services/budget-evaluator.ts +2 -2
  222. package/src/services/config-manager.ts +2 -2
  223. package/src/services/profiler/profile-runner.ts +15 -0
  224. package/src/services/reporter-registry.ts +8 -8
package/src/cli/index.ts CHANGED
@@ -3,103 +3,24 @@
3
3
  /**
4
4
  * ModestBench CLI Entry Point
5
5
  *
6
- * Command-line interface using yargs for command parsing and routing. Provides
7
- * global options, help generation, and dependency injection setup.
6
+ * Command-line interface using bargs for command parsing and routing. This
7
+ * module provides the main entry points and re-exports key types.
8
+ *
9
+ * @packageDocumentation
8
10
  */
9
11
 
12
+ import { BargsError, HelpError } from '@boneskull/bargs';
10
13
  import { realpathSync } from 'node:fs';
11
14
  import { fileURLToPath } from 'node:url';
12
- import yargs from 'yargs';
13
- import { hideBin } from 'yargs/helpers';
14
-
15
- import type {
16
- BenchmarkEngine,
17
- ConfigurationManager,
18
- Engine,
19
- HistoryStorage,
20
- ProgressManager,
21
- ReporterRegistry,
22
- } from '../types/index.js';
23
15
 
24
- import { bootstrap } from '../bootstrap.js';
25
- import {
26
- ABORT_TIMEOUT,
27
- DEFAULT_BENCHMARK_DIR,
28
- DEFAULT_ENGINE,
29
- DEFAULT_REPORTER,
30
- Engines,
31
- ErrorCodes,
32
- ExitCodes,
33
- Reporters,
34
- } from '../constants.js';
35
- import { AccurateEngine, TinybenchEngine } from '../core/engines/index.js';
36
- import { isError } from '../errors/base.js';
37
- import { isModestBenchError, UnknownError } from '../errors/index.js';
38
- import {
39
- CsvReporter,
40
- HumanReporter,
41
- JsonReporter,
42
- NyanReporter,
43
- SimpleReporter,
44
- } from '../reporters/index.js';
45
- // Import commands
46
- import {
47
- handleAnalyzeCommand as analyzeCommand,
48
- type AnalyzeOptions,
49
- } from './commands/analyze.js';
50
- import {
51
- handleAnalyzeCommand as handleBaselineAnalyzeCommand,
52
- handleDeleteCommand as handleBaselineDeleteCommand,
53
- handleListCommand as handleBaselineListCommand,
54
- handleSetCommand as handleBaselineSetCommand,
55
- handleShowCommand as handleBaselineShowCommand,
56
- } from './commands/baseline.js';
57
- import {
58
- handleCleanCommand,
59
- handleCompareCommand,
60
- handleExportCommand,
61
- handleListCommand,
62
- handleShowCommand,
63
- handleTrendsCommand,
64
- } from './commands/history.js';
65
- import { handleInitCommand as initCommand } from './commands/init.js';
66
- import {
67
- RUN_COMMAND_DEFAULTS,
68
- handleRunCommand as runCommand,
69
- } from './commands/run.js';
70
- import {
71
- handleTestCommand as testCommand,
72
- type TestOptions,
73
- } from './commands/test.js';
16
+ import { ErrorCodes, ExitCodes } from '../constants.js';
17
+ import { isModestBenchError } from '../errors/index.js';
18
+ import { createCli } from './builder.js';
19
+ import { setupSignalHandlers } from './handlers.js';
74
20
 
75
- /**
76
- * CLI context with initialized services
77
- */
78
- export interface CliContext {
79
- readonly abortController: AbortController;
80
- readonly configManager: ConfigurationManager;
81
- readonly engine: BenchmarkEngine;
82
- readonly historyStorage: HistoryStorage;
83
- readonly options: GlobalOptions;
84
- readonly progressManager: ProgressManager;
85
- readonly reporterRegistry: ReporterRegistry;
86
- }
87
-
88
- /**
89
- * Global CLI options shared across all commands
90
- */
91
- interface GlobalOptions {
92
- /** Configuration file path */
93
- config?: string | undefined;
94
- /** Working directory */
95
- cwd?: string;
96
- /** JSON output for machine parsing */
97
- json?: boolean;
98
- /** Disable colored output */
99
- noColor?: boolean;
100
- /** Enable verbose output */
101
- verbose?: boolean;
102
- }
21
+ // Re-export types and utilities for external use
22
+ export { createCliContext } from './context.js';
23
+ export type { CliContext } from './context.js';
103
24
 
104
25
  /**
105
26
  * Initialize and run the CLI
@@ -120,986 +41,49 @@ export const main = async (
120
41
  argv?: string[],
121
42
  abortController?: AbortController,
122
43
  ): Promise<void> => {
44
+ const controller = abortController ?? new AbortController();
45
+
123
46
  try {
124
- const args = argv || hideBin(process.argv);
47
+ const cliBuilder = createCli(controller);
48
+ await cliBuilder.parseAsync(argv);
49
+ } catch (error) {
50
+ // Handle bargs errors
51
+ if (error instanceof HelpError) {
52
+ // Help was requested or invalid args - message already printed
53
+ process.exit(ExitCodes.CONFIG_ERROR);
54
+ }
55
+
56
+ if (error instanceof BargsError) {
57
+ console.error('Error:', error.message);
58
+ process.exit(ExitCodes.CONFIG_ERROR);
59
+ }
125
60
 
126
- const cli = yargs(args);
61
+ // Handle bargs validation errors (thrown as plain Error, not BargsError)
62
+ if (
63
+ error instanceof Error &&
64
+ (error.message.startsWith('Invalid value for --') ||
65
+ error.message.startsWith('Missing required'))
66
+ ) {
67
+ console.error('Error:', error.message);
68
+ process.exit(ExitCodes.CONFIG_ERROR);
69
+ }
127
70
 
128
- // Configure global options and commands
71
+ // Handle ModestBench errors
72
+ if (isModestBenchError(error)) {
73
+ console.error('Error:', error.message);
74
+ if (process.env.DEBUG) {
75
+ console.error(error.stack);
76
+ }
129
77
 
130
- await cli
131
- .scriptName('modestbench')
132
- .option('config', {
133
- alias: 'c',
134
- description: 'Path to configuration file',
135
- global: true,
136
- type: 'string',
137
- })
138
- .option('verbose', {
139
- alias: 'v',
140
- defaultDescription: String(RUN_COMMAND_DEFAULTS.verbose),
141
- description: 'Enable verbose output',
142
- global: true,
143
- type: 'boolean',
144
- })
145
- .option('no-color', {
146
- defaultDescription: 'false',
147
- description: 'Disable colored output',
148
- global: true,
149
- type: 'boolean',
150
- })
151
- .option('progress', {
152
- defaultDescription: 'true',
153
- description: 'Show animated progress bar',
154
- global: true,
155
- type: 'boolean',
156
- })
157
- .option('json', {
158
- defaultDescription: 'false',
159
- description: 'Output results in JSON format',
160
- global: true,
161
- type: 'boolean',
162
- })
163
- .option('cwd', {
164
- defaultDescription: '.',
165
- description: 'Working directory',
166
- global: true,
167
- normalize: true,
168
- type: 'string',
169
- })
170
- .help()
171
- .alias('help', 'h')
172
- .version()
173
- .alias('version', 'V')
174
- .strict()
175
- .demandCommand(1)
176
- .recommendCommands()
177
- .completion()
178
- .wrap(Math.min(120, cli.terminalWidth()))
179
- .command(
180
- ['$0 [pattern..]', 'run [pattern..]'],
181
- 'Run benchmark files',
182
- (yargs) =>
183
- yargs
184
- .positional('pattern', {
185
- array: true,
186
- defaultDescription: `(auto-discovered from ${DEFAULT_BENCHMARK_DIR} directory)`,
187
- describe:
188
- 'File paths, directory paths, or glob patterns for benchmark files',
189
- type: 'string',
190
- })
191
- .option('config', {
192
- alias: 'c',
193
- description: 'Path to configuration file',
194
- type: 'string',
195
- })
196
- .option('reporter', {
197
- alias: 'r',
198
- array: true,
199
- choices: Object.values(Reporters).sort(),
200
- defaultDescription: DEFAULT_REPORTER,
201
- description: 'Output reporters to use (human,json,csv)',
202
- type: 'string',
203
- })
204
- .option('output', {
205
- alias: 'o',
206
- description: 'Output directory for reports',
207
- type: 'string',
208
- })
209
- .option('output-file', {
210
- alias: ['of', 'file'],
211
- description:
212
- 'Custom filename for reporter output (use with single reporter only)',
213
- requiresArg: true,
214
- type: 'string',
215
- })
216
- .option('iterations', {
217
- alias: 'i',
218
- description: 'Number of iterations per benchmark',
219
- type: 'number',
220
- })
221
- .option('time', {
222
- alias: 't',
223
- description: 'Time budget per benchmark in milliseconds',
224
- type: 'number',
225
- })
226
- .option('warmup', {
227
- alias: ['w', 'warm'],
228
- description: 'Number of warmup iterations',
229
- type: 'number',
230
- })
231
- .option('limit-by', {
232
- alias: ['l', 'limit'],
233
- choices: ['time', 'iterations', 'any', 'all'],
234
- description:
235
- 'How to limit benchmarks: time (time budget), iterations (sample count), any (either threshold), all (both thresholds)',
236
- type: 'string',
237
- })
238
- .option('bail', {
239
- alias: 'b',
240
- defaultDescription: String(RUN_COMMAND_DEFAULTS.bail),
241
- description: 'Stop on first failure',
242
- type: 'boolean',
243
- })
244
- .option('exclude', {
245
- alias: 'X',
246
- array: true,
247
- description: 'Exclude patterns (comma-separated)',
248
- type: 'string',
249
- })
250
- .option('timeout', {
251
- description: 'Timeout per benchmark in milliseconds',
252
- type: 'number',
253
- })
254
- .option('quiet', {
255
- alias: 'q',
256
- defaultDescription: String(RUN_COMMAND_DEFAULTS.quiet),
257
- description: 'Minimal output',
258
- type: 'boolean',
259
- })
260
- .option('tag', {
261
- array: true,
262
- description: 'Include only benchmarks with any of these tags',
263
- type: 'string',
264
- })
265
- .option('exclude-tag', {
266
- alias: 'T',
267
- array: true,
268
- description: 'Exclude benchmarks with any of these tags',
269
- type: 'string',
270
- })
271
- .option('engine', {
272
- alias: 'e',
273
- choices: Object.values(Engines),
274
- defaultDescription: DEFAULT_ENGINE,
275
- description:
276
- 'Benchmark engine: tinybench (default) or accurate (requires --allow-natives-syntax)',
277
- type: 'string',
278
- })
279
- .option('json-pretty', {
280
- defaultDescription: 'false',
281
- description:
282
- 'Pretty-print JSON output (only affects json reporter)',
283
- type: 'boolean',
284
- })
285
- .example([
286
- ['$0 run', 'Run benchmarks in current directory and bench/'],
287
- ['$0 run benchmarks/', 'Run all benchmarks in a directory'],
288
- ['$0 run src/perf/', 'Run benchmarks in specific directory'],
289
- ['$0 run "src/**/*.bench.js"', 'Run specific glob pattern'],
290
- ['$0 run file1.bench.js file2.bench.js', 'Run specific files'],
291
- ['$0 run benchmarks/ tests/perf/', 'Run multiple directories'],
292
- ['$0 run -r json -r csv', 'Use multiple reporters'],
293
- ['$0 run --iterations 1000', 'Set iteration count'],
294
- ['$0 run --engine accurate', 'Use high-accuracy engine'],
295
- ['$0 run --bail', 'Stop on first failure'],
296
- ])
297
- .check((argv) => {
298
- if (
299
- argv.reporter &&
300
- argv.reporter.length > 1 &&
301
- argv['output-file']
302
- ) {
303
- throw new Error(
304
- '--output-file can only be used with a single reporter. Use --output <dir> for multiple reporters.',
305
- );
306
- }
307
- return true;
308
- }),
309
- async (argv) => {
310
- const context = await createCliContext(
311
- argv,
312
- abortController!,
313
- argv.engine,
314
- );
315
- const exitCode = await runCommand(context, {
316
- bail: argv.bail,
317
- config: argv.config,
318
- cwd: argv.cwd,
319
- engine: argv.engine,
320
- exclude: argv.exclude,
321
- excludeTags: argv['exclude-tag'],
322
- iterations: argv.iterations,
323
- json: argv.json,
324
- jsonPretty: argv['json-pretty'],
325
- noColor: argv.noColor,
326
- outputDir: argv.output,
327
- outputFile: argv['output-file'],
328
- pattern: argv.pattern,
329
- progress: argv.progress,
330
- quiet: argv.quiet,
331
- reporters: argv.reporter,
332
- tags: argv.tag,
333
- time: argv.time,
334
- timeout: argv.timeout,
335
- verbose: argv.verbose,
336
- warmup: argv.warmup,
337
- });
338
- process.exit(exitCode);
339
- },
340
- )
341
- .command('history', 'View and manage benchmark history', (yargs) =>
342
- yargs
343
- .command(
344
- 'list',
345
- 'List recent benchmark runs',
346
- (yargs) =>
347
- yargs
348
- .option('since', {
349
- description:
350
- 'Show runs since date (ISO 8601 or relative like "1 week ago")',
351
- type: 'string',
352
- })
353
- .option('until', {
354
- description:
355
- 'Show runs until date (ISO 8601 or relative like "1 day ago")',
356
- type: 'string',
357
- })
358
- .option('pattern', {
359
- description: 'Filter by benchmark name pattern',
360
- type: 'string',
361
- })
362
- .option('tag', {
363
- alias: 't',
364
- array: true,
365
- description: 'Filter by tags (comma-separated)',
366
- type: 'string',
367
- })
368
- .option('limit', {
369
- defaultDescription: '10',
370
- description: 'Maximum number of results',
371
- type: 'number',
372
- })
373
- .option('format', {
374
- choices: ['human', 'json', 'csv'] as const,
375
- defaultDescription: 'human' as const,
376
- description: 'Output format',
377
- type: 'string',
378
- })
379
- .example([
380
- ['$0 history list', 'List recent benchmark runs'],
381
- [
382
- '$0 history list --since "1 week ago"',
383
- 'List runs from last week',
384
- ],
385
- ['$0 history list --limit 20', 'List 20 most recent runs'],
386
- ['$0 history list --format json', 'List runs in JSON format'],
387
- ]),
388
- async (argv) => {
389
- const context = await createCliContext(argv, abortController!);
390
- const exitCode = await handleListCommand(context, {
391
- cwd: argv.cwd,
392
- format: argv.format,
393
- limit: argv.limit,
394
- pattern: argv.pattern,
395
- since: argv.since,
396
- tags: argv.tag,
397
- until: argv.until,
398
- verbose: argv.verbose,
399
- });
400
- process.exit(exitCode);
401
- },
402
- )
403
- .command(
404
- 'show <run-id>',
405
- 'Show detailed results for a specific run',
406
- (yargs) =>
407
- yargs
408
- .positional('run-id', {
409
- demandOption: true,
410
- describe: 'ID of the benchmark run to show',
411
- type: 'string',
412
- })
413
- .option('format', {
414
- choices: ['human', 'json', 'csv'] as const,
415
- defaultDescription: 'human' as const,
416
- description: 'Output format',
417
- type: 'string',
418
- })
419
- .example([
420
- [
421
- '$0 history show abc123',
422
- 'Show detailed results for run abc123',
423
- ],
424
- [
425
- '$0 history show abc123 --format json',
426
- 'Show run in JSON format',
427
- ],
428
- ]),
429
- async (argv) => {
430
- const context = await createCliContext(argv, abortController!);
431
- const exitCode = await handleShowCommand(context, {
432
- cwd: argv.cwd,
433
- format: argv.format,
434
- runId: argv['run-id'],
435
- verbose: argv.verbose,
436
- });
437
- process.exit(exitCode);
438
- },
439
- )
440
- .command(
441
- 'compare <run-id1> <run-id2>',
442
- 'Compare two benchmark runs',
443
- (yargs) =>
444
- yargs
445
- .positional('run-id1', {
446
- demandOption: true,
447
- describe: 'ID of the first benchmark run',
448
- type: 'string',
449
- })
450
- .positional('run-id2', {
451
- demandOption: true,
452
- describe: 'ID of the second benchmark run',
453
- type: 'string',
454
- })
455
- .option('format', {
456
- choices: ['human', 'json'] as const,
457
- defaultDescription: 'human' as const,
458
- description: 'Output format',
459
- type: 'string',
460
- })
461
- .example([
462
- ['$0 history compare abc123 def456', 'Compare two runs'],
463
- [
464
- '$0 history compare abc123 def456 --format json',
465
- 'Compare in JSON format',
466
- ],
467
- ]),
468
- async (argv) => {
469
- const context = await createCliContext(argv, abortController!);
470
- const exitCode = await handleCompareCommand(context, {
471
- cwd: argv.cwd,
472
- format: argv.format,
473
- runId1: argv['run-id1'],
474
- runId2: argv['run-id2'],
475
- verbose: argv.verbose,
476
- });
477
- process.exit(exitCode);
478
- },
479
- )
480
- .command(
481
- 'trends [pattern]',
482
- 'Show performance trends over time',
483
- (yargs) =>
484
- yargs
485
- .positional('pattern', {
486
- describe: 'Filter by benchmark name pattern',
487
- type: 'string',
488
- })
489
- .option('since', {
490
- description:
491
- 'Show trends since date (ISO 8601 or relative like "1 week ago")',
492
- type: 'string',
493
- })
494
- .option('until', {
495
- description:
496
- 'Show trends until date (ISO 8601 or relative like "1 day ago")',
497
- type: 'string',
498
- })
499
- .option('tag', {
500
- alias: 't',
501
- array: true,
502
- description: 'Filter by tags (comma-separated)',
503
- type: 'string',
504
- })
505
- .option('limit', {
506
- description: 'Maximum number of runs to analyze',
507
- type: 'number',
508
- })
509
- .option('all', {
510
- alias: 'a',
511
- defaultDescription: 'false',
512
- description: 'Analyze all runs (ignore limit)',
513
- type: 'boolean',
514
- })
515
- .option('format', {
516
- choices: ['human', 'json'] as const,
517
- defaultDescription: 'human' as const,
518
- description: 'Output format',
519
- type: 'string',
520
- })
521
- .example([
522
- [
523
- '$0 history trends',
524
- 'Show performance trends for all benchmarks',
525
- ],
526
- [
527
- '$0 history trends --since "1 month ago"',
528
- 'Show trends from last month',
529
- ],
530
- [
531
- '$0 history trends "array-*"',
532
- 'Show trends for array benchmarks',
533
- ],
534
- [
535
- '$0 history trends --format json',
536
- 'Output trends in JSON format',
537
- ],
538
- ]),
539
- async (argv) => {
540
- const context = await createCliContext(argv, abortController!);
541
- const exitCode = await handleTrendsCommand(context, {
542
- all: argv.all,
543
- cwd: argv.cwd,
544
- format: argv.format,
545
- limit: argv.limit,
546
- pattern: argv.pattern,
547
- since: argv.since,
548
- tags: argv.tag,
549
- until: argv.until,
550
- verbose: argv.verbose,
551
- });
552
- process.exit(exitCode);
553
- },
554
- )
555
- .command(
556
- 'clean',
557
- 'Clean up old benchmark history',
558
- (yargs) =>
559
- yargs
560
- .option('max-age', {
561
- description: 'Remove runs older than this many days',
562
- type: 'number',
563
- })
564
- .option('max-runs', {
565
- description: 'Keep only this many most recent runs',
566
- type: 'number',
567
- })
568
- .option('max-size', {
569
- description: 'Keep history under this size in bytes',
570
- type: 'number',
571
- })
572
- .option('yes', {
573
- alias: 'y',
574
- description: 'Confirm cleanup without prompting',
575
- type: 'boolean',
576
- })
577
- .option('quiet', {
578
- default: false,
579
- description: 'Minimal output',
580
- type: 'boolean',
581
- })
582
- .check((argv) => {
583
- if (
584
- !argv['max-age'] &&
585
- !argv['max-runs'] &&
586
- !argv['max-size']
587
- ) {
588
- throw new Error(
589
- 'At least one cleanup criterion must be specified (--max-age, --max-runs, or --max-size)',
590
- );
591
- }
592
- return true;
593
- })
594
- .example([
595
- [
596
- '$0 history clean --max-runs 50 --yes',
597
- 'Keep only latest 50 runs',
598
- ],
599
- [
600
- '$0 history clean --max-age 30',
601
- 'Preview removing runs older than 30 days',
602
- ],
603
- [
604
- '$0 history clean --max-size 10485760',
605
- 'Keep history under 10MB',
606
- ],
607
- ]),
608
- async (argv) => {
609
- const context = await createCliContext(argv, abortController!);
610
- const exitCode = await handleCleanCommand(context, {
611
- confirm: argv.yes,
612
- cwd: argv.cwd,
613
- maxAge: argv['max-age'],
614
- maxRuns: argv['max-runs'],
615
- maxSize: argv['max-size'],
616
- quiet: argv.quiet,
617
- verbose: argv.verbose,
618
- });
619
- process.exit(exitCode);
620
- },
621
- )
622
- .command(
623
- 'export',
624
- 'Export benchmark history to a file',
625
- (yargs) =>
626
- yargs
627
- .option('format', {
628
- choices: ['json', 'csv'] as const,
629
- defaultDescription: 'json' as const,
630
- description: 'Export format',
631
- type: 'string',
632
- })
633
- .option('output', {
634
- alias: 'o',
635
- demandOption: true,
636
- description: 'Output file path',
637
- type: 'string',
638
- })
639
- .option('since', {
640
- description: 'Export runs since date',
641
- type: 'string',
642
- })
643
- .option('until', {
644
- description: 'Export runs until date',
645
- type: 'string',
646
- })
647
- .example([
648
- [
649
- '$0 history export -o history.json',
650
- 'Export all history to JSON',
651
- ],
652
- [
653
- '$0 history export -o history.csv --format csv',
654
- 'Export to CSV',
655
- ],
656
- [
657
- '$0 history export -o recent.json --since "1 week ago"',
658
- 'Export recent runs',
659
- ],
660
- ]),
661
- async (argv) => {
662
- const context = await createCliContext(argv, abortController!);
663
- const exitCode = await handleExportCommand(context, {
664
- cwd: argv.cwd,
665
- format: argv.format,
666
- outputPath: argv.output,
667
- quiet: Boolean(argv.quiet),
668
- since: argv.since,
669
- until: argv.until,
670
- verbose: argv.verbose,
671
- });
672
- process.exitCode = exitCode;
673
- },
674
- )
675
- .demandCommand(1, 'You must specify a history subcommand')
676
- .strict()
677
- .example([
678
- ['$0 history list', 'List recent benchmark runs'],
679
- ['$0 history show <run-id>', 'Show detailed results'],
680
- ['$0 history compare <run-id1> <run-id2>', 'Compare two runs'],
681
- ['$0 history trends', 'Show performance trends'],
682
- ['$0 history clean --max-runs 50', 'Keep only latest 50 runs'],
683
- ['$0 history export -o data.json', 'Export history'],
684
- ]),
685
- )
686
- .command('baseline', 'Manage performance baselines', (yargs) => {
687
- return yargs
688
- .command(
689
- 'set <name>',
690
- 'Save a benchmark run as a baseline',
691
- (yargs) => {
692
- return yargs
693
- .positional('name', {
694
- describe: 'Name for the baseline',
695
- type: 'string',
696
- })
697
- .option('run-id', {
698
- description: 'Specific run ID to save (default: most recent)',
699
- type: 'string',
700
- })
701
- .option('commit', {
702
- description: 'Git commit SHA (40 characters)',
703
- type: 'string',
704
- })
705
- .option('branch', {
706
- description: 'Git branch name',
707
- type: 'string',
708
- })
709
- .option('default', {
710
- defaultDescription: 'false',
711
- description: 'Set as default baseline',
712
- type: 'boolean',
713
- })
714
- .example([
715
- [
716
- '$0 baseline set production-v1.0',
717
- 'Save most recent run as baseline',
718
- ],
719
- ['$0 baseline set v1.0 --default', 'Save and set as default'],
720
- [
721
- '$0 baseline set v1.0 --commit abc123...',
722
- 'Save with commit info',
723
- ],
724
- ]);
725
- },
726
- async (argv) => {
727
- const context = await createCliContext(argv, abortController!);
728
- const exitCode = await handleBaselineSetCommand(context, {
729
- branch: argv.branch,
730
- commit: argv.commit,
731
- cwd: argv.cwd,
732
- default: argv.default,
733
- name: String(argv.name),
734
- quiet: Boolean(argv.quiet),
735
- runId: argv['run-id'],
736
- verbose: argv.verbose,
737
- });
738
- process.exit(exitCode);
739
- },
740
- )
741
- .command(
742
- 'list',
743
- 'List all saved baselines',
744
- (yargs) => {
745
- return yargs
746
- .option('format', {
747
- choices: ['human', 'json'] as const,
748
- defaultDescription: 'human' as const,
749
- description: 'Output format',
750
- type: 'string',
751
- })
752
- .example([
753
- ['$0 baseline list', 'List all baselines'],
754
- ['$0 baseline list --format json', 'List in JSON format'],
755
- ]);
756
- },
757
- async (argv) => {
758
- const context = await createCliContext(argv, abortController!);
759
- const exitCode = await handleBaselineListCommand(context, {
760
- cwd: argv.cwd,
761
- format: argv.format,
762
- quiet: Boolean(argv.quiet),
763
- verbose: argv.verbose,
764
- });
765
- process.exit(exitCode);
766
- },
767
- )
768
- .command(
769
- 'show <name>',
770
- 'Show baseline details',
771
- (yargs) => {
772
- return yargs
773
- .positional('name', {
774
- describe: 'Baseline name to show',
775
- type: 'string',
776
- })
777
- .option('format', {
778
- choices: ['human', 'json'] as const,
779
- defaultDescription: 'human' as const,
780
- description: 'Output format',
781
- type: 'string',
782
- })
783
- .example([
784
- ['$0 baseline show production-v1.0', 'Show baseline details'],
785
- [
786
- '$0 baseline show v1.0 --format json',
787
- 'Show in JSON format',
788
- ],
789
- ]);
790
- },
791
- async (argv) => {
792
- const context = await createCliContext(argv, abortController!);
793
- const exitCode = await handleBaselineShowCommand(context, {
794
- cwd: argv.cwd,
795
- format: argv.format,
796
- name: String(argv.name),
797
- quiet: Boolean(argv.quiet),
798
- verbose: argv.verbose,
799
- });
800
- process.exit(exitCode);
801
- },
802
- )
803
- .command(
804
- 'delete <name>',
805
- 'Delete a baseline',
806
- (yargs) => {
807
- return yargs
808
- .positional('name', {
809
- describe: 'Baseline name to delete',
810
- type: 'string',
811
- })
812
- .example([
813
- ['$0 baseline delete old-baseline', 'Delete a baseline'],
814
- ]);
815
- },
816
- async (argv) => {
817
- const context = await createCliContext(argv, abortController!);
818
- const exitCode = await handleBaselineDeleteCommand(context, {
819
- cwd: argv.cwd,
820
- name: String(argv.name),
821
- quiet: Boolean(argv.quiet),
822
- verbose: argv.verbose,
823
- });
824
- process.exit(exitCode);
825
- },
826
- )
827
- .command(
828
- 'analyze',
829
- 'Analyze history and suggest performance budgets',
830
- (yargs) => {
831
- return yargs
832
- .option('runs', {
833
- defaultDescription: '10',
834
- description: 'Number of recent runs to analyze',
835
- type: 'number',
836
- })
837
- .option('confidence', {
838
- defaultDescription: '0.95',
839
- description: 'Confidence level (0.5-0.999, default 0.95)',
840
- type: 'number',
841
- })
842
- .example([
843
- [
844
- '$0 baseline analyze',
845
- 'Analyze last 10 runs with 95% confidence',
846
- ],
847
- ['$0 baseline analyze --runs 20', 'Analyze last 20 runs'],
848
- [
849
- '$0 baseline analyze --confidence 0.90',
850
- 'Use 90% confidence level',
851
- ],
852
- ]);
853
- },
854
- async (argv) => {
855
- const context = await createCliContext(argv, abortController!);
856
- const exitCode = await handleBaselineAnalyzeCommand(context, {
857
- confidence: argv.confidence,
858
- cwd: argv.cwd,
859
- quiet: Boolean(argv.quiet),
860
- runs: argv.runs,
861
- verbose: argv.verbose,
862
- });
863
- process.exit(exitCode);
864
- },
865
- )
866
- .demandCommand(1, 'You must specify a baseline subcommand')
867
- .strict()
868
- .example([
869
- ['$0 baseline set production-v1.0', 'Save current run as baseline'],
870
- ['$0 baseline list', 'List all baselines'],
871
- ['$0 baseline show production-v1.0', 'Show baseline details'],
872
- ['$0 baseline delete old-baseline', 'Delete a baseline'],
873
- ['$0 baseline analyze', 'Suggest budgets from history'],
874
- ]);
875
- })
876
- .command(
877
- 'init [type]',
878
- 'Initialize a new benchmark project',
879
- (yargs) => {
880
- return yargs
881
- .positional('type', {
882
- choices: ['basic', 'advanced', 'library'] as const,
883
- defaultDescription: 'basic' as const,
884
- describe: 'Type of project to initialize',
885
- type: 'string',
886
- })
887
- .option('examples', {
888
- defaultDescription: 'true',
889
- description: 'Include example benchmark files',
890
- type: 'boolean',
891
- })
892
- .option('config-type', {
893
- choices: ['json', 'yaml', 'js', 'ts'] as const,
894
- defaultDescription: 'json' as const,
895
- description: 'Configuration file format',
896
- type: 'string',
897
- })
898
- .option('force', {
899
- defaultDescription: 'false',
900
- description: 'Overwrite existing files',
901
- type: 'boolean',
902
- })
903
- .option('yes', {
904
- alias: 'y',
905
- defaultDescription: 'false',
906
- description: 'Accept all prompts automatically',
907
- type: 'boolean',
908
- })
909
- .option('quiet', {
910
- alias: 'q',
911
- defaultDescription: 'false',
912
- description: 'Minimal output',
913
- type: 'boolean',
914
- })
915
- .example([
916
- ['$0 init', 'Initialize a basic project'],
917
- [
918
- '$0 init advanced --config-type ts',
919
- 'Initialize advanced project with TypeScript config',
920
- ],
921
- [
922
- '$0 init library --no-examples',
923
- 'Initialize library project without examples',
924
- ],
925
- ]);
926
- },
927
- async (argv) => {
928
- const context = await createCliContext(argv, abortController!);
929
- const exitCode = await initCommand(context, {
930
- configType: argv['config-type'],
931
- cwd: argv.cwd,
932
- examples: argv.examples,
933
- force: argv.force,
934
- quiet: argv.quiet,
935
- type: argv.type,
936
- verbose: argv.verbose,
937
- yes: argv.yes,
938
- });
939
- process.exitCode = exitCode;
940
- },
941
- )
942
- .command(
943
- ['analyze [command]', 'profile [command]'],
944
- 'Analyze code execution and identify benchmark candidates',
945
- (yargs) => {
946
- return yargs
947
- .positional('command', {
948
- description: 'Command to analyze (e.g., "npm test")',
949
- type: 'string',
950
- })
951
- .option('input', {
952
- alias: 'i',
953
- description: 'Path to existing *.cpuprofile file',
954
- type: 'string',
955
- })
956
- .option('filter-file', {
957
- description: 'Filter functions by file glob pattern',
958
- type: 'string',
959
- })
960
- .option('min-percent', {
961
- alias: ['m', 'min'],
962
- default: 0.5,
963
- description: 'Minimum execution percentage to show',
964
- type: 'number',
965
- })
966
- .option('top', {
967
- alias: 'n',
968
- default: 25,
969
- description: 'Number of top functions to show',
970
- type: 'number',
971
- })
972
- .option('group-by-file', {
973
- default: false,
974
- description: 'Group results by file',
975
- type: 'boolean',
976
- })
977
- .check((argv) => {
978
- if (!argv.command && !argv.input) {
979
- throw new Error('Either [command] or --input must be provided');
980
- }
981
- return true;
982
- });
983
- },
984
- async (argv) => {
985
- // Context not needed for analyze command currently
986
- const context = {} as CliContext;
78
+ // Show help for file discovery errors
79
+ if (error.code === ErrorCodes.FILE_DISCOVERY_FAILED) {
80
+ process.exit(ExitCodes.DISCOVERY_ERROR);
81
+ }
987
82
 
988
- const options: AnalyzeOptions = {
989
- color: !argv.noColor,
990
- command: argv.command,
991
- cwd: argv.cwd || process.cwd(),
992
- filterFile: argv.filterFile,
993
- groupByFile: argv.groupByFile,
994
- input: argv.input,
995
- minPercent: argv.minPercent,
996
- top: argv.top,
997
- };
83
+ process.exit(ExitCodes.RUNTIME_ERROR);
84
+ }
998
85
 
999
- process.exitCode = await analyzeCommand(context, options);
1000
- },
1001
- )
1002
- .command(
1003
- 'test <framework> [files..]',
1004
- 'Run test files as benchmarks (captures tests from Jest, Mocha, node:test, or AVA)',
1005
- (yargs) => {
1006
- return yargs
1007
- .positional('framework', {
1008
- choices: ['ava', 'jest', 'mocha', 'node-test'] as const,
1009
- demandOption: true,
1010
- describe: 'Test framework to use',
1011
- nargs: 1,
1012
- type: 'string',
1013
- })
1014
- .positional('files', {
1015
- array: true,
1016
- describe: 'Test file paths or glob patterns',
1017
- type: 'string',
1018
- })
1019
- .option('iterations', {
1020
- alias: 'i',
1021
- default: 100,
1022
- description: 'Number of iterations per test',
1023
- type: 'number',
1024
- })
1025
- .option('warmup', {
1026
- alias: 'w',
1027
- default: 5,
1028
- description: 'Number of warmup iterations',
1029
- type: 'number',
1030
- })
1031
- .option('bail', {
1032
- alias: 'b',
1033
- default: false,
1034
- description: 'Stop on first failure',
1035
- type: 'boolean',
1036
- })
1037
- .option('quiet', {
1038
- alias: 'q',
1039
- default: false,
1040
- description: 'Minimal output',
1041
- type: 'boolean',
1042
- })
1043
- .example([
1044
- ['$0 test mocha test/*.spec.js', 'Run Mocha tests as benchmarks'],
1045
- [
1046
- '$0 test node-test test/*.test.js',
1047
- 'Run node:test tests as benchmarks',
1048
- ],
1049
- [
1050
- '$0 test ava test/*.js --iterations 500',
1051
- 'Run AVA tests with 500 iterations',
1052
- ],
1053
- [
1054
- '$0 test mocha test/unit.spec.js --json',
1055
- 'Output results as JSON',
1056
- ],
1057
- ]);
1058
- },
1059
- async (argv) => {
1060
- const context = await createCliContext(argv, abortController!);
1061
- const options: TestOptions = {
1062
- bail: argv.bail,
1063
- cwd: argv.cwd,
1064
- framework: argv.framework as TestOptions['framework'],
1065
- iterations: argv.iterations,
1066
- json: argv.json,
1067
- noColor: argv.noColor,
1068
- pattern: argv.files,
1069
- quiet: argv.quiet,
1070
- verbose: argv.verbose,
1071
- warmup: argv.warmup,
1072
- };
1073
- const exitCode = await testCommand(context, options);
1074
- process.exit(exitCode);
1075
- },
1076
- )
1077
- .fail((msg, err, yargs) => {
1078
- if (err) {
1079
- console.error('Error:', err.message);
1080
- if (process.env.DEBUG) {
1081
- console.error(err.stack);
1082
- }
1083
- // Show help for file discovery errors (similar to usage errors)
1084
- if (
1085
- isModestBenchError(err) &&
1086
- err.code === ErrorCodes.FILE_DISCOVERY_FAILED
1087
- ) {
1088
- console.error();
1089
- yargs.showHelp();
1090
- process.exit(ExitCodes.DISCOVERY_ERROR);
1091
- }
1092
- process.exit(ExitCodes.RUNTIME_ERROR);
1093
- } else {
1094
- // Show help for usage errors (unknown arguments, etc.)
1095
- console.error(msg);
1096
- console.error();
1097
- yargs.showHelp();
1098
- process.exit(ExitCodes.CONFIG_ERROR);
1099
- }
1100
- })
1101
- .parse();
1102
- } catch (error) {
86
+ // Unexpected error
1103
87
  console.error(
1104
88
  'Unexpected error:',
1105
89
  error instanceof Error ? error.message : String(error),
@@ -1111,125 +95,6 @@ export const main = async (
1111
95
  }
1112
96
  };
1113
97
 
1114
- /**
1115
- * Create CLI context with dependency injection
1116
- */
1117
- const createCliContext = async (
1118
- options: GlobalOptions,
1119
- abortController: AbortController,
1120
- engineType: Engine = DEFAULT_ENGINE,
1121
- ): Promise<CliContext> => {
1122
- try {
1123
- const dependencies = bootstrap();
1124
-
1125
- // Select engine based on type
1126
- const engine =
1127
- engineType === Engines.ACCURATE
1128
- ? new AccurateEngine(dependencies)
1129
- : new TinybenchEngine(dependencies);
1130
-
1131
- // Register built-in reporters
1132
- engine.registerReporter(
1133
- Reporters.HUMAN,
1134
- new HumanReporter({
1135
- color: !options.noColor,
1136
- verbose: options.verbose,
1137
- }),
1138
- );
1139
-
1140
- engine.registerReporter(
1141
- 'json',
1142
- new JsonReporter({
1143
- prettyPrint: false,
1144
- }),
1145
- );
1146
-
1147
- engine.registerReporter(
1148
- 'csv',
1149
- new CsvReporter({
1150
- includeHeaders: true,
1151
- includeMetadata: true,
1152
- }),
1153
- );
1154
-
1155
- engine.registerReporter(
1156
- 'simple',
1157
- new SimpleReporter({
1158
- verbose: options.verbose,
1159
- }),
1160
- );
1161
-
1162
- engine.registerReporter(
1163
- 'nyan',
1164
- new NyanReporter({
1165
- color: !options.noColor,
1166
- }),
1167
- );
1168
-
1169
- return {
1170
- abortController,
1171
- configManager: engine.configManager,
1172
- engine,
1173
- historyStorage: engine.historyStorage,
1174
- options,
1175
- progressManager: engine.progressManager,
1176
- reporterRegistry: engine.reporterRegistry,
1177
- };
1178
- } catch (error) {
1179
- console.error(
1180
- 'Failed to initialize ModestBench:',
1181
- error instanceof Error ? error.message : String(error),
1182
- );
1183
- process.exit(ExitCodes.CONFIG_ERROR);
1184
- }
1185
- };
1186
-
1187
- /**
1188
- * Handle process signals gracefully
1189
- */
1190
- const setupSignalHandlers = (abortController: AbortController): void => {
1191
- let abortRequested = false;
1192
-
1193
- const handleSignal = (signal: NodeJS.Signals) => {
1194
- if (abortRequested) {
1195
- // Second signal, force exit
1196
- console.log(`\nReceived ${signal} again, forcing exit...`);
1197
- process.exit(computeExitCode(signal));
1198
- }
1199
-
1200
- console.log(`\nReceived ${signal}, aborting benchmarks...`);
1201
- abortRequested = true;
1202
- abortController.abort();
1203
-
1204
- // Give a short grace period for cleanup, then exit
1205
- setTimeout(() => {
1206
- console.log('\nBenchmark aborted.');
1207
- process.exit(computeExitCode(signal));
1208
- }, ABORT_TIMEOUT);
1209
- };
1210
-
1211
- process
1212
- .once('SIGINT', handleSignal)
1213
- .once('SIGQUIT', handleSignal)
1214
- .once('SIGTERM', handleSignal)
1215
- .once('uncaughtException', (error) => {
1216
- // Wrap non-ModestBench errors with UnknownError
1217
- const wrappedError: Error = isModestBenchError(error)
1218
- ? error
1219
- : new UnknownError(error.message, { cause: error });
1220
- console.error(`${wrappedError}`);
1221
- process.exit(ExitCodes.RUNTIME_ERROR);
1222
- })
1223
- .once('unhandledRejection', (reason) => {
1224
- const wrappedError = new UnknownError(
1225
- isError(reason) ? reason.message : String(reason),
1226
- { cause: reason },
1227
- );
1228
- console.error(`${wrappedError}`);
1229
- process.exit(ExitCodes.RUNTIME_ERROR);
1230
- });
1231
- };
1232
-
1233
98
  // Run CLI if this file is executed directly
1234
99
  const scriptPath = fileURLToPath(import.meta.url);
1235
100
  const argPath = process.argv[1];
@@ -1248,13 +113,3 @@ try {
1248
113
  cli();
1249
114
  }
1250
115
  }
1251
-
1252
- /**
1253
- * Compute the exit code based on the signal
1254
- *
1255
- * @param signal - The signal that caused the exit
1256
- * @returns The exit code
1257
- */
1258
- const computeExitCode = (signal: NodeJS.Signals): number => {
1259
- return 128 + (signal === 'SIGINT' ? 2 : signal === 'SIGQUIT' ? 3 : 15);
1260
- };