|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import {fileURLToPath} from "node:url"; |
| 4 | +import path from "node:path"; |
| 5 | +import fs from "node:fs"; |
| 6 | +import BenchmarkRunner from "./lib/BenchmarkRunner.js"; |
| 7 | +import git from "./lib/utils/git.js"; |
| 8 | +import npm from "./lib/utils/npm.js"; |
| 9 | +import {spawnProcess} from "./lib/utils/process.js"; |
| 10 | + |
| 11 | +const __filename = fileURLToPath(import.meta.url); |
| 12 | +const __dirname = path.dirname(__filename); |
| 13 | + |
| 14 | +function printUsageAndExit() { |
| 15 | + console.error( |
| 16 | + "Usage:\n\t" + |
| 17 | + "ui5-cli-benchmark run <path-to-config> [<project-dir>...]" |
| 18 | + ); |
| 19 | + process.exit(1); |
| 20 | +} |
| 21 | + |
| 22 | +export const commands = { |
| 23 | + async run(args, options = {}) { |
| 24 | + const configFilePath = args[0]; |
| 25 | + const projectDirs = args.slice(1); |
| 26 | + |
| 27 | + // Validate arguments |
| 28 | + if (!configFilePath) { |
| 29 | + return printUsageAndExit(); |
| 30 | + } |
| 31 | + |
| 32 | + // Determine repository and CLI paths |
| 33 | + const repositoryPath = path.resolve(__dirname, "../.."); |
| 34 | + const ui5CliPath = path.resolve(repositoryPath, "packages/cli/bin/ui5.cjs"); |
| 35 | + |
| 36 | + // Create BenchmarkRunner with injected dependencies |
| 37 | + const benchmarkRunner = new BenchmarkRunner({ |
| 38 | + git: options.git || git, |
| 39 | + npm: options.npm || npm, |
| 40 | + spawnProcess: options.spawnProcess || spawnProcess, |
| 41 | + fs: options.fs || fs |
| 42 | + }); |
| 43 | + |
| 44 | + // Run benchmarks |
| 45 | + const result = await benchmarkRunner.run({ |
| 46 | + configFilePath, |
| 47 | + repositoryPath, |
| 48 | + ui5CliPath, |
| 49 | + projectDirs: projectDirs.length > 0 ? projectDirs : undefined, |
| 50 | + timestamp: options.timestamp |
| 51 | + }); |
| 52 | + |
| 53 | + if (!result.success) { |
| 54 | + process.exit(1); |
| 55 | + } |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +async function main() { |
| 60 | + const args = process.argv.slice(2); |
| 61 | + |
| 62 | + if (args.length === 0 || args[0] === "-h" || args[0] === "--help") { |
| 63 | + return printUsageAndExit(); |
| 64 | + } |
| 65 | + |
| 66 | + const command = args[0]; |
| 67 | + const commandArgs = args.slice(1); |
| 68 | + const fn = commands[command]; |
| 69 | + |
| 70 | + // Validate command name |
| 71 | + if (!fn) { |
| 72 | + process.stderr.write(`Unknown command: '${command}'\n\n`); |
| 73 | + return process.exit(1); |
| 74 | + } |
| 75 | + |
| 76 | + // Execute handler |
| 77 | + try { |
| 78 | + await fn(commandArgs); |
| 79 | + } catch (error) { |
| 80 | + console.error(`Unexpected error: ${error.message}`); |
| 81 | + console.error("Stack trace:", error.stack); |
| 82 | + |
| 83 | + process.exit(1); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +// Handle uncaught exceptions |
| 89 | +process.on("uncaughtException", (error) => { |
| 90 | + console.error("Uncaught exception:", error.message); |
| 91 | + process.exit(1); |
| 92 | +}); |
| 93 | + |
| 94 | +process.on("unhandledRejection", (reason, promise) => { |
| 95 | + console.error("Unhandled rejection at:", promise, "reason:", reason); |
| 96 | + process.exit(1); |
| 97 | +}); |
| 98 | + |
| 99 | +main(); |
0 commit comments