Files
Maurice 88d196bae8 ci(client): lint:dup holds copied code to a baseline that only shrinks
Most features have a desktop view and a phone view, and each carried its
own copy of the feature's logic, so SonarCloud's duplication condition
was the gate a pull request touching both failed most often, and only
after a scan of twenty minutes. lint:dup finds the same copies before
the push.

It runs jscpd's detector (@jscpd/core with @jscpd/tokenizer) over the
source files under src/ at the thresholds the SonarCloud gate applies to
TypeScript, 100 tokens over at least 10 lines, and counts per file the
lines inside a block that repeats elsewhere or in the same file. Tests
and declaration files are left out as Sonar leaves them out, and .ts and
.tsx are read as one language, so a hook that repeats a view's code is a
copy too. The files are read sorted and with readText, so every machine
counts the same. scripts/dup-baseline.json holds the files that still
hold copies with their count; a file may hold no more than its entry, a
file without an entry none, and an entry above its file or for a file
that is gone fails until --update lowers it. --list prints every block
with its other side.

The engine packages are used instead of the jscpd command, which walks
the tree in file system order, skips files past its size limits without
a word and drops every result once it meets a file it has no format for.
2026-10-09 15:20:10 +02:00

30 lines
1.5 KiB
JavaScript

#!/usr/bin/env node
/*
* lint:dup: keeps copied code from coming back.
*
* Most features have a desktop view and a phone view, and each used to carry
* its own copy of the feature's logic, so a fix reached one of them and the
* other kept the bug. The logic now lives in one shared hook or module per
* feature, and this check holds the copies that remain to what they are. It
* counts, per file under src/, the lines inside a block of code that repeats
* elsewhere (or in the same file), at the thresholds SonarCloud uses on a
* pull request. The files that still hold such lines are listed in
* scripts/dup-baseline.json with their count, and a file may hold no more
* than its entry (a file without an entry holds none). An entry above what
* its file holds now, or for a file that is gone, fails as well until
* --update lowers it.
*
* npm run lint:dup check against the baseline (CI)
* npm run lint:dup -- --list print every copied block with its other side
* npm run lint:dup -- --update lower the baseline to what the files hold now;
* it never raises an entry
*
* The measuring lives in scripts/lib/dup.mjs.
*/
import { fileURLToPath } from 'node:url';
import { check } from './lib/dup.mjs';
import { runCli } from './lib/ratchet.mjs';
const root = fileURLToPath(new URL('..', import.meta.url));
await runCli('lint:dup', (args) => check({ root, update: args.includes('--update'), list: args.includes('--list') }));