|
| 1 | +#!/usr/bin/env node |
| 2 | +import { Command } from "commander"; |
| 3 | +import puppeteer from "puppeteer"; |
| 4 | +import lighthouse from "lighthouse"; |
| 5 | +import fs from "fs"; |
| 6 | +import path from "path"; |
| 7 | + |
| 8 | +const program = new Command(); |
| 9 | + |
| 10 | +program |
| 11 | + .name("frontend-performance-analyzer") |
| 12 | + .description("Analyze frontend performance of a given URL") |
| 13 | + .version("0.1.1") |
| 14 | + .requiredOption("-u, --url <url>", "URL to analyze") |
| 15 | + .option("-o, --output <file>", "Save HTML report to file") |
| 16 | + .option("--json", "Print raw JSON report to stdout") |
| 17 | + .parse(process.argv); |
| 18 | + |
| 19 | +const options = program.opts(); |
| 20 | + |
| 21 | +async function runLighthouse(url) { |
| 22 | + const browser = await puppeteer.launch({ |
| 23 | + headless: true, |
| 24 | + args: ["--remote-debugging-port=9222"], |
| 25 | + }); |
| 26 | + |
| 27 | + const result = await lighthouse(url, { |
| 28 | + port: 9222, |
| 29 | + output: "html", |
| 30 | + logLevel: "info", |
| 31 | + }); |
| 32 | + |
| 33 | + await browser.close(); |
| 34 | + return result; |
| 35 | +} |
| 36 | + |
| 37 | +(async () => { |
| 38 | + try { |
| 39 | + const { lhr, report } = await runLighthouse(options.url); |
| 40 | + |
| 41 | + console.log( |
| 42 | + `✅ Performance score: ${lhr.categories.performance.score * 100}` |
| 43 | + ); |
| 44 | + |
| 45 | + if (options.json) { |
| 46 | + console.log(JSON.stringify(lhr, null, 2)); |
| 47 | + } |
| 48 | + |
| 49 | + if (options.output) { |
| 50 | + const outputPath = path.resolve(process.cwd(), options.output); |
| 51 | + fs.writeFileSync(outputPath, report); |
| 52 | + console.log(`💾 Report saved to ${outputPath}`); |
| 53 | + } |
| 54 | + } catch (err) { |
| 55 | + console.error("❌ Failed to analyze:", err.message); |
| 56 | + process.exit(1); |
| 57 | + } |
| 58 | +})(); |
0 commit comments