@vocento/wpo-check
Version:
An internal CLI tool to measure and validate Web Performance KPIs during development and CI/CD workflows.
63 lines (49 loc) • 1.75 kB
JavaScript
import chalk from 'chalk';
import fs from 'node:fs';
import path from 'node:path';
export function reportPartialResults(url, metrics, thresholds, reportPath) {
const filePath = path.join(process.cwd(), reportPath);
let hasCriticalFailure = false;
try {
fs.accessSync(filePath);
} catch (_error) {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
}
const title = `📊 Metrics Evaluation: ${url}\n`;
fs.appendFileSync(filePath, title);
console.log(chalk.bold('\n' + title));
for (const [key, value] of Object.entries(metrics)) {
const threshold = thresholds[key];
if (!threshold) {
continue;
}
const { value: thresholdValue, critical, unit: thresholdUnit } = threshold;
const passes = value <= thresholdValue;
let status = '';
let color = chalk.white;
if (passes) {
status = '✅ Pass';
color = chalk.green;
} else if (!passes && critical) {
status = '❌ FAIL (critical)';
color = chalk.redBright;
hasCriticalFailure = true;
} else {
status = '⚠️ FAIL (non-critical)';
color = chalk.yellow;
}
const result = `${key.padEnd(18)}: ${`${String(value).substring(0, 8).padStart(8)} (threshold: ≤ ${thresholdValue}${thresholdUnit})`.padEnd(30)} → ${status}`;
fs.appendFileSync(filePath, result + '\n');
console.log(color(result));
}
fs.appendFileSync(filePath, '\n');
console.log('\n');
return !hasCriticalFailure;
}
export function reportResult(ok, reportPath) {
const filePath = path.join(process.cwd(), reportPath);
const result = ok ? '✅ OK' : '❌ KO';
const color = ok ? chalk.green : chalk.redBright;
fs.appendFileSync(filePath, result);
console.log(color(result));
}