All files / src/aggregator/generator render-batch.ts

100% Statements 13/13
83.33% Branches 5/6
100% Functions 2/2
100% Lines 12/12

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                                                      4x 222x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x    
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Aggregator/Generator/RenderBatch
 * @description Batch orchestrator that regenerates every analysis run
 * discovered under `analysis/daily/**`. Runs sharing the same
 * `(date, articleType)` pair are disambiguated by appending the
 * sanitised `runId` as a slug suffix so language variants are never
 * silently overwritten.
 */
 
import type { CliOptions } from './cli.js';
import { discoverAnalysisRuns, groupRunsForCollision } from './discovery.js';
import { sanitizeRunSuffix } from './slug.js';
import { generateArticle, type GenerateResult } from './render-one.js';
 
/**
 * Batch-generate articles for every discovered run. Runs that share a
 * `(date, articleType)` pair are disambiguated by appending the sanitised
 * `runId` as a slug suffix so none of the language variants are ever
 * silently overwritten.
 *
 * @param opts - CLI options (must have `all: true`)
 * @returns Per-run generation results in the order they were processed
 */
export function generateAllArticles(opts: CliOptions): GenerateResult[] {
  const allRuns = discoverAnalysisRuns(opts.repoRoot);
  const filtered = opts.since ? allRuns.filter((r) => r.date >= (opts.since as string)) : allRuns;
  const groups = groupRunsForCollision(filtered);
  const results: GenerateResult[] = [];
  const articleCountOverride = filtered.length;
  for (const run of filtered) {
    const key = `${run.date}|${run.articleType}`;
    const bucket = groups.get(key) ?? [];
    const suffix = bucket.length > 1 ? sanitizeRunSuffix(run.runId) : undefined;
    const runOpts: CliOptions = { ...opts, runDir: run.runDir };
    results.push(generateArticle(runOpts, suffix, articleCountOverride));
  }
  return results;
}