|
| 1 | +#!/usr/bin/env node |
| 2 | +import { Command } from "commander"; |
| 3 | +import fs from "fs"; |
| 4 | +import chalk from "chalk"; |
| 5 | +import { logger, OUTPUT_LEVELS } from "./lib/logger.js"; |
| 6 | +import { |
| 7 | + validateInputs, |
| 8 | + getUrlList, |
| 9 | + validateUrlAccessibility, |
| 10 | +} from "./lib/url-utils.js"; |
| 11 | +import { runLighthouseAnalysis } from "./lib/lighthouse.js"; |
| 12 | +import { formatConsoleMetrics } from "./report-formatters/console.js"; |
| 13 | +import { exportMarkdownReport } from "./report-formatters/markdown.js"; |
| 14 | +import { exportJsonReport } from "./report-formatters/json.js"; |
| 15 | + |
| 16 | +const packageJson = JSON.parse(fs.readFileSync("package.json", "utf8")); |
| 17 | +const version = packageJson.version; |
| 18 | + |
| 19 | +const program = new Command(); |
| 20 | + |
| 21 | +program |
| 22 | + .name("frontend-performance-analyzer") |
| 23 | + .description("Analyze frontend performance of a given URL") |
| 24 | + .version(version) |
| 25 | + .option("-u, --url <url...>", "One or more URLs to analyze") |
| 26 | + .option("--input <file>", "Load URLs from a .txt or .json file") |
| 27 | + .option("-o, --output <file>", "Save HTML report to file") |
| 28 | + .option("--json", "Print raw JSON report to stdout") |
| 29 | + .option("--json-file <file>", "Save JSON report to file") |
| 30 | + .option("--markdown", "Save metrics as Markdown report") |
| 31 | + .option( |
| 32 | + "--threshold <score>", |
| 33 | + "Minimum acceptable Lighthouse performance score (0-100)", |
| 34 | + parseFloat |
| 35 | + ) |
| 36 | + .option("-v, --verbose", "Enable verbose output with debugging details") |
| 37 | + .option("-s, --silent", "Minimal output (errors and final results only)") |
| 38 | + .parse(process.argv); |
| 39 | + |
| 40 | +const options = program.opts(); |
| 41 | + |
| 42 | +// Validate mutually exclusive flags |
| 43 | +if (options.verbose && options.silent) { |
| 44 | + console.error( |
| 45 | + chalk.red("❌ Error: --verbose and --silent cannot be used together") |
| 46 | + ); |
| 47 | + process.exit(1); |
| 48 | +} |
| 49 | + |
| 50 | +(async () => { |
| 51 | + const startTime = Date.now(); |
| 52 | + logger.verbose(`Starting frontend-performance-analyzer v${version}`); |
| 53 | + logger.verbose(`Node.js version: ${process.version}`); |
| 54 | + logger.verbose(`Platform: ${process.platform} ${process.arch}`); |
| 55 | + logger.verbose(`Working directory: ${process.cwd()}`); |
| 56 | + logger.verbose(`Command line arguments: ${JSON.stringify(process.argv)}`); |
| 57 | + logger.verbose(`Options: ${JSON.stringify(options, null, 2)}`); |
| 58 | + |
| 59 | + // Validate inputs before processing |
| 60 | + validateInputs(options); |
| 61 | + |
| 62 | + const urls = getUrlList(options); |
| 63 | + |
| 64 | + // Check URL accessibility and get only accessible ones |
| 65 | + const accessibleUrls = await validateUrlAccessibility(urls); |
| 66 | + const allResults = []; // Store all results for batch JSON export |
| 67 | + |
| 68 | + logger.info(chalk.blue.bold("🚀 Starting Lighthouse analysis...\n")); |
| 69 | + |
| 70 | + let successCount = 0; |
| 71 | + let failureCount = 0; |
| 72 | + |
| 73 | + for (let i = 0; i < accessibleUrls.length; i++) { |
| 74 | + const url = accessibleUrls[i]; |
| 75 | + const progress = `[${i + 1}/${accessibleUrls.length}]`; |
| 76 | + const urlStartTime = Date.now(); |
| 77 | + |
| 78 | + logger.info(chalk.blue(`${progress} 🔍 Analyzing ${url}...`)); |
| 79 | + logger.verbose( |
| 80 | + `Starting analysis ${i + 1}/${ |
| 81 | + accessibleUrls.length |
| 82 | + } at ${new Date().toISOString()}` |
| 83 | + ); |
| 84 | + |
| 85 | + try { |
| 86 | + logger.info( |
| 87 | + chalk.gray(" └─ Launching browser..."), |
| 88 | + OUTPUT_LEVELS.NORMAL |
| 89 | + ); |
| 90 | + const { lhr, report } = await runLighthouseAnalysis(url, options); |
| 91 | + const urlAnalysisTime = Date.now() - urlStartTime; |
| 92 | + |
| 93 | + logger.info(chalk.gray(" └─ Analysis complete!"), OUTPUT_LEVELS.NORMAL); |
| 94 | + logger.verbose(`Total analysis time for ${url}: ${urlAnalysisTime}ms`); |
| 95 | + |
| 96 | + // Store result for batch processing |
| 97 | + allResults.push({ lhr, url, report }); |
| 98 | + |
| 99 | + if (!options.json || options.jsonFile) { |
| 100 | + formatConsoleMetrics(lhr); |
| 101 | + } |
| 102 | + |
| 103 | + successCount++; |
| 104 | + |
| 105 | + if (options.json) { |
| 106 | + console.log(JSON.stringify(lhr, null, 2)); |
| 107 | + } |
| 108 | + |
| 109 | + if (options.output) { |
| 110 | + const safeUrl = url.replace(/https?:\/\//, "").replace(/[^\w]/g, "_"); |
| 111 | + const outputFile = `${safeUrl}.html`; |
| 112 | + logger.verbose(`Saving HTML report to: ${outputFile}`); |
| 113 | + fs.writeFileSync(outputFile, report); |
| 114 | + logger.info( |
| 115 | + chalk.gray(` └─ HTML report saved to ${outputFile}`), |
| 116 | + OUTPUT_LEVELS.NORMAL |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + if (options.markdown) { |
| 121 | + const safeUrl = url.replace(/https?:\/\//, "").replace(/[^\w]/g, "_"); |
| 122 | + const markdownFile = `${safeUrl}.md`; |
| 123 | + exportMarkdownReport(lhr, markdownFile); |
| 124 | + } |
| 125 | + |
| 126 | + // Individual JSON file export |
| 127 | + if (options.jsonFile && accessibleUrls.length === 1) { |
| 128 | + exportJsonReport({ lhr, url }, options.jsonFile); |
| 129 | + } |
| 130 | + |
| 131 | + if (options.threshold !== undefined) { |
| 132 | + const actualScore = lhr.categories.performance.score * 100; |
| 133 | + logger.verbose( |
| 134 | + `Comparing score ${actualScore} against threshold ${options.threshold}` |
| 135 | + ); |
| 136 | + if (actualScore < options.threshold) { |
| 137 | + logger.warn( |
| 138 | + chalk.red( |
| 139 | + `Score ${actualScore} is below threshold of ${options.threshold}` |
| 140 | + ) |
| 141 | + ); |
| 142 | + process.exitCode = 1; // does not exit immediately, just sets failure |
| 143 | + } |
| 144 | + } |
| 145 | + } catch (err) { |
| 146 | + const urlAnalysisTime = Date.now() - urlStartTime; |
| 147 | + logger.error(chalk.red(`Failed: ${err.message}`)); |
| 148 | + logger.verbose(`Analysis failed for ${url} after ${urlAnalysisTime}ms`); |
| 149 | + logger.verbose(`Error details: ${err.stack}`); |
| 150 | + failureCount++; |
| 151 | + process.exitCode = 1; |
| 152 | + } |
| 153 | + |
| 154 | + // Add spacing between analyses |
| 155 | + if (i < accessibleUrls.length - 1) { |
| 156 | + logger.info("", OUTPUT_LEVELS.NORMAL); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // Batch JSON export |
| 161 | + if (options.json) { |
| 162 | + logger.verbose("Performing batch JSON export to stdout"); |
| 163 | + exportJsonReport(allResults); |
| 164 | + } |
| 165 | + |
| 166 | + if (options.jsonFile && accessibleUrls.length > 1) { |
| 167 | + logger.verbose(`Performing batch JSON export to file: ${options.jsonFile}`); |
| 168 | + exportJsonReport(allResults, options.jsonFile); |
| 169 | + } |
| 170 | + |
| 171 | + const totalTime = Date.now() - startTime; |
| 172 | + logger.verbose(`Total execution time: ${totalTime}ms`); |
| 173 | + |
| 174 | + // Final summary |
| 175 | + logger.info(chalk.blue.bold("\n📋 Analysis Summary:")); |
| 176 | + logger.info(`${chalk.green("✅ Successful:")} ${successCount}`); |
| 177 | + if (failureCount > 0) { |
| 178 | + logger.info(`${chalk.red("❌ Failed:")} ${failureCount}`); |
| 179 | + } |
| 180 | + logger.info(`${chalk.blue("📊 Total analyzed:")} ${accessibleUrls.length}`); |
| 181 | + |
| 182 | + if (urls.length > accessibleUrls.length) { |
| 183 | + logger.info( |
| 184 | + `${chalk.yellow("⚠️ Skipped (inaccessible):")} ${ |
| 185 | + urls.length - accessibleUrls.length |
| 186 | + }` |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + logger.verbose(`Analysis completed at ${new Date().toISOString()}`); |
| 191 | + logger.verbose( |
| 192 | + `Performance: ${(accessibleUrls.length / (totalTime / 1000)).toFixed( |
| 193 | + 2 |
| 194 | + )} URLs/sec` |
| 195 | + ); |
| 196 | + logger.verbose(`Total execution time: ${totalTime}ms`); |
| 197 | +})(); |
0 commit comments