All files / src/plugins/thumbnails check-thumbnails.ts

0% Statements 0/20
0% Branches 0/10
0% Functions 0/2
0% Lines 0/20

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 49 50 51 52 53 54 55 56 57 58 59 60 61                                                                                                                         
/**
 * Checks that every face's committed module-thumbnails.g.js matches its panel PNGs.
 *
 * A new module or a re-shot PNG without a `paf gen <face> thumbnails` run ships a settings page showing old
 * previews. A PNG naming no module or a size the face does not list, or a module with no PNG,
 * means the previews and the catalogue disagree. A face that commits the asset but has no thumbnails folder is reported too.
 *
 * paf check runs this from the unit when it lists the thumbnails plugin.
 */
import fs from 'node:fs';
import { checkFace, runCheck } from '../../tools/shared/checks.ts';
import { describeMissing, describeStray, encodeThumbnails, outFile, thumbsDir } from './embed-thumbnails.ts';
import { readText } from '../../tools/shared/files.ts';
import { listFaceNames } from '../../tools/shared/faces.ts';
import { isMainScript } from '../../tools/shared/entry.ts';
 
/**
 * Every face whose thumbnail asset is stale or disagrees with its module list.
 *
 * @return One line per problem, empty when every face is current.
 */
export async function checkThumbnails(): Promise<string[]> {
  const problems: string[] = [];
 
  for (const face of listFaceNames()) {
    await checkFace(face, problems, () => {
      const hasFolder = fs.existsSync(thumbsDir(face));
      const committed = fs.existsSync(outFile(face));
 
      if (!hasFolder) {
        if (committed) {
          problems.push(`${face}: commits module-thumbnails.g.js, but has no resources/thumbnails folder to build it from`);
        }
 
        return;
      }
 
      const built = encodeThumbnails(face);
      const current = committed ? readText(outFile(face)) : null;
 
      if (current !== built.source) {
        problems.push(`${face}: module-thumbnails.g.js is stale, run paf gen ${face} thumbnails`);
      }
 
      for (const stray of built.stray) {
        problems.push(`${face}: ${describeStray(stray, built.sizes)}`);
      }
 
      for (const slug of built.missing) {
        problems.push(`${face}: ${describeMissing(slug)}`);
      }
    });
  }
 
  return problems;
}
 
if (isMainScript(import.meta)) {
  runCheck('thumbnails', checkThumbnails);
}