All files / packages/design-system/scripts/validation/lib baseline-file-utils.js

100% Statements 13/13
100% Branches 2/2
100% Functions 4/4
100% Lines 13/13

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            2x       2x         2x       1x 1x           1x 2x 2x 2x           1x       1x 1x 1x    
import crypto from 'node:crypto';
import fs from 'fs-extra';
import path from 'node:path';
import glob from 'glob';
 
export function getChecksum(content) {
  return crypto.createHash('md5').update(content).digest('hex');
}
 
export function findFiles(directory, pattern = '**/*') {
  const files = glob.sync(pattern, {
    cwd: directory,
    nodir: true,
    dot: false,
  });
  return files.sort();
}
 
export function createManifest(directory, pattern = '**/*') {
  const files = findFiles(directory, pattern);
  const manifest = {
    version: '1.0.0',
    timestamp: new Date().toISOString(),
    files: {},
  };
 
  for (const file of files) {
    const fullPath = path.join(directory, file);
    const content = fs.readFileSync(fullPath);
    manifest.files[file] = {
      checksum: getChecksum(content),
      size: content.length,
    };
  }
 
  return manifest;
}
 
export function writeManifest(baselineDir, manifest) {
  const manifestPath = path.join(baselineDir, 'manifest.json');
  fs.ensureDirSync(baselineDir);
  fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2), 'utf8');
}