Parse Lighthouse results into Davidson report
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
$inputPath = $argv[1] ?? '.davidson-reports/lighthouse-report.json';
|
||||
$outputPath = $argv[2] ?? '.davidson-reports/davidson-lighthouse.json';
|
||||
|
||||
if (!file_exists($inputPath)) {
|
||||
fwrite(STDERR, "ERROR: Lighthouse report not found: $inputPath\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$json = file_get_contents($inputPath);
|
||||
|
||||
if ($json === false) {
|
||||
fwrite(STDERR, "ERROR: Could not read Lighthouse report.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if (!is_array($data)) {
|
||||
fwrite(STDERR, "ERROR: Invalid Lighthouse JSON.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
function getScoreStatus(int $score): array
|
||||
{
|
||||
if ($score >= 90) {
|
||||
return [
|
||||
'status' => 'Good',
|
||||
'class' => 'good',
|
||||
];
|
||||
}
|
||||
|
||||
if ($score >= 50) {
|
||||
return [
|
||||
'status' => 'Fix',
|
||||
'class' => 'fix',
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => 'Issue',
|
||||
'class' => 'issue',
|
||||
];
|
||||
}
|
||||
|
||||
$categoryIds = [
|
||||
'performance',
|
||||
'accessibility',
|
||||
'best-practices',
|
||||
'seo',
|
||||
];
|
||||
|
||||
$scores = [];
|
||||
|
||||
foreach ($categoryIds as $categoryId) {
|
||||
$rawScore = $data['categories'][$categoryId]['score'] ?? null;
|
||||
|
||||
if (!is_numeric($rawScore)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$score = (int) round($rawScore * 100);
|
||||
$status = getScoreStatus($score);
|
||||
|
||||
$scores[$categoryId] = [
|
||||
'score' => $score,
|
||||
'status' => $status['status'],
|
||||
'class' => $status['class'],
|
||||
];
|
||||
}
|
||||
|
||||
$output = [
|
||||
'schemaVersion' => 1,
|
||||
'scores' => $scores,
|
||||
'findings' => [],
|
||||
];
|
||||
|
||||
$outputJson = json_encode(
|
||||
$output,
|
||||
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
|
||||
);
|
||||
|
||||
if ($outputJson === false) {
|
||||
fwrite(STDERR, "ERROR: Could not encode Davidson Lighthouse report.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$outputDir = dirname($outputPath);
|
||||
|
||||
if (!is_dir($outputDir)) {
|
||||
mkdir($outputDir, 0777, true);
|
||||
}
|
||||
|
||||
if (file_put_contents($outputPath, $outputJson) === false) {
|
||||
fwrite(STDERR, "ERROR: Could not write Davidson Lighthouse report.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
echo "Davidson Lighthouse report generated.\n";
|
||||
echo "Saved: $outputPath\n";
|
||||
Reference in New Issue
Block a user