All files / repo fix-coverage-paths.cjs

0% Statements 0/38
0% Branches 0/1
0% Functions 0/1
0% Lines 0/38

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                                                                             
const fs = require('fs');
const path = require('path');

function getHtmlFiles(dir, fileList = []) {
  const files = fs.readdirSync(dir);
  for (const file of files) {
    const fullPath = path.join(dir, file);
    const stat = fs.statSync(fullPath);
    if (stat.isDirectory()) {
      getHtmlFiles(fullPath, fileList);
    } else if (file.endsWith('.html')) {
      fileList.push(fullPath);
    }
  }
  return fileList;
}

const coverageDir = path.join(__dirname, 'coverage');
const htmlFiles = getHtmlFiles(coverageDir);

for (const filePath of htmlFiles) {
  let html = fs.readFileSync(filePath, 'utf8');

  html = html.replace(/<script\s+src="./(.+?)"><\/script>/g, '<script src="./$1" type="text/javascript" defer></script>');

  html = html.replace(/(href|src)="\/?(?!http)([^"]+)"/g, '$1="./$2"');

  if (!html.includes('http-equiv="Content-Type"')) {
    html = html.replace(/<meta charset="utf-8">/, `$&\n  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />`);
  }

  fs.writeFileSync(filePath, html);
  console.log(`Patched ${path.relative(__dirname, filePath)}`);
}

const redirectsFile = path.join(coverageDir, '_redirects');
fs.writeFileSync(redirectsFile, '/index.html 200\n');
console.log('Created _redirects file');