161 lines
3.2 KiB
PHP
161 lines
3.2 KiB
PHP
<?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 = [];
|
|
|
|
$auditCategories = [];
|
|
|
|
foreach ($categoryIds as $categoryId) {
|
|
$auditRefs = $data['categories'][$categoryId]['auditRefs'] ?? [];
|
|
|
|
foreach ($auditRefs as $auditRef) {
|
|
$auditId = $auditRef['id'] ?? null;
|
|
|
|
if ($auditId === null) {
|
|
continue;
|
|
}
|
|
|
|
$auditCategories[$auditId][] = $categoryId;
|
|
}
|
|
}
|
|
|
|
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'],
|
|
];
|
|
}
|
|
|
|
$findings = [];
|
|
|
|
$skipModes = [
|
|
'notApplicable',
|
|
'informative',
|
|
'manual',
|
|
'error',
|
|
'numeric',
|
|
];
|
|
|
|
foreach ($data['audits'] ?? [] as $auditId => $audit) {
|
|
$score = $audit['score'] ?? null;
|
|
$scoreDisplayMode = $audit['scoreDisplayMode'] ?? null;
|
|
|
|
if ($score === null) {
|
|
continue;
|
|
}
|
|
|
|
if (in_array($scoreDisplayMode, $skipModes, true)) {
|
|
continue;
|
|
}
|
|
|
|
if (!is_numeric($score) || $score >= 1) {
|
|
continue;
|
|
}
|
|
|
|
$categories = $auditCategories[$auditId] ?? [];
|
|
|
|
if ($categories === []) {
|
|
continue;
|
|
}
|
|
|
|
$findings[] = [
|
|
'id' => $auditId,
|
|
'categories' => $categories,
|
|
'title' => $audit['title'] ?? $auditId,
|
|
'problem' => $audit['description'] ?? '',
|
|
'impact' => $audit['displayValue'] ?? null,
|
|
'score' => (int) round($score * 100),
|
|
];
|
|
}
|
|
|
|
$output = [
|
|
'schemaVersion' => 1,
|
|
'scores' => $scores,
|
|
'findings' => $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"; |