All files / scripts shields.js

0% Statements 0/17
0% Branches 0/10
100% Functions 0/0
0% Lines 0/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                                                                                               
import { makeBadge } from 'badge-maker';
import arg from 'arg';
import path from 'path';
import fs from 'fs-extra';
import { fileURLToPath } from 'url';
import config from '../vitest.config.js';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const root = path.resolve(__dirname, '../');
 
const args = arg({
  '--stats': String,
  '--output': String,
  // Aliases
  '-s': '--stats',
  '-o': '--output',
});
 
// optional --stats argument
const statsPath = args['--stats'] || path.resolve(root, 'coverage/coverage-summary.json');
const outputPath = args['--output'] || path.resolve(root, 'coverage/');
 
if (!fs.existsSync(statsPath)) {
  throw new Error(
    `File not found: ${statsPath} - Please provide a valid path to the coverage-summary.json file.`,
  );
}
 
const stats = JSON.parse(fs.readFileSync(statsPath, 'utf-8'));
const coverage = stats.total.lines.pct;
const coverageThreshold = config.test.coverage.thresholds.lines;
const color = coverage < coverageThreshold ? 'red' : coverage < 85 ? 'yellow' : 'brightgreen';
 
// Create a badge for the coverage percentage
try {
  const badge = makeBadge({
    label: 'Coverage',
    message: `${coverage}%`,
    color: color,
  });
  console.log(badge);
  // store the badge in the coverage directory
  fs.writeFileSync(path.resolve(root, outputPath, 'badge.svg'), badge);
} catch (e) {
  console.error(e);
}