dev-lamp
Version:
Your friendly lighthouse performance companion - 100% local
113 lines • 4.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnalyzeCommand = void 0;
const ora_1 = __importDefault(require("ora"));
const chalk_1 = __importDefault(require("chalk"));
const lighthouse_runner_1 = require("../../core/lighthouse-runner");
const formatter_factory_1 = require("../../formatters/formatter-factory");
const file_writer_1 = require("../../utils/file-writer");
const error_handler_1 = require("../../utils/error-handler");
const url_validator_1 = require("../../utils/url-validator");
class AnalyzeCommand {
runner = new lighthouse_runner_1.LighthouseRunner();
errorHandler = new error_handler_1.ErrorHandler();
async execute(url, options) {
const spinner = (0, ora_1.default)({
text: 'Illuminating performance metrics...',
spinner: 'dots12'
}).start();
try {
// Validate and sanitize URL
const sanitizedUrl = this.validateUrl(url);
// Run Lighthouse
spinner.text = `Analyzing ${chalk_1.default.cyan(url)}...`;
const report = await this.runner.analyze(sanitizedUrl, {
...options,
chrome: {
headless: options.headless !== false,
keepAlive: options.keepAlive
}
});
// Format output
spinner.text = 'Generating report...';
const formatter = formatter_factory_1.FormatterFactory.create(options.format || 'md');
const output = await formatter.format(report, {
template: options.template,
includeEmoji: options.emoji !== false
});
// Write output
const writer = new file_writer_1.FileWriter();
const outputPath = await writer.write(output, options.output || 'lighthouse-report.md');
spinner.succeed(`Report saved to ${chalk_1.default.green(outputPath)}`);
// Display summary
this.displaySummary(report, options);
// Check threshold
if (options.threshold && report.scores.performance) {
this.checkThreshold(report.scores.performance, options.threshold);
}
}
catch (error) {
spinner.fail('Analysis failed');
this.errorHandler.handle(error);
process.exit(1);
}
}
validateUrl(url) {
const validation = url_validator_1.UrlValidator.validate(url);
if (!validation.valid) {
const error = new Error(validation.error || 'Invalid URL');
error.code = 'INVALID_URL';
error.url = url;
throw error;
}
return validation.sanitized || url;
}
displaySummary(report, options) {
if (options.quiet)
return;
console.log('\n' + chalk_1.default.bold('Performance Summary:'));
console.log('─'.repeat(40));
const score = report.scores.performance || 0;
const emoji = this.getScoreEmoji(score);
const color = this.getScoreColor(score);
console.log(`${emoji} Performance: ${color(score + '/100')}`);
// Core Web Vitals
if (report.metrics.lcp) {
console.log(` LCP: ${report.metrics.lcp.displayValue}`);
}
if (report.metrics.fid) {
console.log(` FID: ${report.metrics.fid.displayValue}`);
}
if (report.metrics.cls) {
console.log(` CLS: ${report.metrics.cls.displayValue}`);
}
if (options.fun) {
console.log(chalk_1.default.yellow('\nPerformance analysis complete!'));
}
}
getScoreEmoji(score) {
if (score >= 90)
return '[GOOD]';
if (score >= 50)
return '[OK]';
return '[POOR]';
}
getScoreColor(score) {
if (score >= 90)
return chalk_1.default.green;
if (score >= 50)
return chalk_1.default.yellow;
return chalk_1.default.red;
}
checkThreshold(score, threshold) {
if (score < threshold) {
console.error(chalk_1.default.red(`\nWARNING: Performance score (${score}) is below threshold (${threshold})`));
process.exit(1);
}
}
}
exports.AnalyzeCommand = AnalyzeCommand;
//# sourceMappingURL=analyze.js.map