fetchLints

tcmts.cli.sync.fetchLints · #ypls2rpi · term · runs anywhere

Fetch the lint findings of a branch's definitions this codebase has none of, from the remote. How many were received.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { chunks, lintedIn } from "#codebase/tcmts/cli/sync";
import type { Remote } from "#codebase/tcmts/cli/sync";
import type { NameRow } from "#codebase/tcmts/store";

export async function fetchLints(cb: Codebase, remote: Remote, names: NameRow[]): Promise<number> {
  const defs = lintedIn(names).filter((h) => cb.store.hasDef(h));
  try {
    const want: [string, string][] = [];
    for (const c of chunks(defs))
      for (const [h, k] of (await remote.lintKeys(c)) ?? [])
        if (cb.store.lintResult(h, k) === undefined) want.push([h, k]);
    let got = 0;
    for (const c of chunks(want, 500)) {
      const ls = (await remote.lints(c)) ?? [];
      const asked = new Set(c.map(([h, k]) => `${h} ${k}`));
      cb.store.transaction(() => {
        for (const l of ls) {
          if (!asked.has(`${l.hash} ${l.key}`) || cb.store.lintResult(l.hash, l.key) !== undefined)
            continue;
          try {
            if (!Array.isArray(JSON.parse(String(l.issues)))) continue;
          } catch {
            continue;
          }
          cb.store.putLintResult(l.hash, l.key, String(l.issues));
          got++;
        }
      });
    }
    return got;
  } catch {
    return 0;
  }
}

uses

used by