133 lines
4.1 KiB
TypeScript
133 lines
4.1 KiB
TypeScript
import { code, table } from '../markdown'
|
|
import { KIND_LABELS, LEVEL_LABELS, LEVELS } from './labels'
|
|
import { needsConfirmation, type ChangeLevel } from './schema'
|
|
import { statRows, type DocumentStats } from './stats'
|
|
import { isItem } from './targets'
|
|
import type { MigrationMapStats } from './migration-map'
|
|
import type { ChangeRecord } from './types'
|
|
|
|
/**
|
|
* French CHANGELOG of the working revision: volumes before / after, then
|
|
* one table per change level with every elementary edit. Pure formatting
|
|
* of the records, deterministic (no dates).
|
|
*/
|
|
|
|
const MAX_VALUE_LENGTH = 120
|
|
|
|
function isLabel(value: unknown): value is Record<string, string> {
|
|
if (!isItem(value)) return false
|
|
const entries = Object.entries(value)
|
|
return (
|
|
entries.length > 0 &&
|
|
entries.every(
|
|
([k, v]) => (k === 'fr' || k === 'en') && typeof v === 'string',
|
|
)
|
|
)
|
|
}
|
|
|
|
/** Compact text of a recorded value: labels as text, objects as JSON. */
|
|
export function compactValue(value: unknown): string {
|
|
let text: string
|
|
if (typeof value === 'string') text = value
|
|
else if (isLabel(value)) text = Object.values(value).join(' / ')
|
|
else text = JSON.stringify(value)
|
|
if (text.length <= MAX_VALUE_LENGTH) return text
|
|
return `${text.slice(0, MAX_VALUE_LENGTH - 1)}…`
|
|
}
|
|
|
|
export function recordSubject(record: ChangeRecord): string {
|
|
return record.field === null
|
|
? record.target
|
|
: `${record.target}.${record.field}`
|
|
}
|
|
|
|
function recordRow(record: ChangeRecord): string[] {
|
|
const { change } = record
|
|
return [
|
|
`${code(change.id)} ${change.title.fr}`,
|
|
code(recordSubject(record)),
|
|
compactValue(record.before),
|
|
compactValue(record.after),
|
|
KIND_LABELS[change.kind],
|
|
change.rationale,
|
|
change.reference_ids.map(code).join(', ') || '-',
|
|
needsConfirmation(change.level) ? 'oui' : 'non',
|
|
]
|
|
}
|
|
|
|
function levelSection(level: ChangeLevel, records: readonly ChangeRecord[]) {
|
|
const rows = records.filter((r) => r.change.level === level).map(recordRow)
|
|
const head = [
|
|
'Changement',
|
|
'Objet',
|
|
'Avant',
|
|
'Après',
|
|
'Nature',
|
|
'Justification',
|
|
'Références',
|
|
'Confirmation requise',
|
|
]
|
|
const body =
|
|
rows.length === 0 ? 'Aucun changement de ce niveau.' : table(head, rows)
|
|
return `## ${LEVEL_LABELS[level]}\n\n${body}`
|
|
}
|
|
|
|
function comparison(before: DocumentStats, after: DocumentStats): string {
|
|
const afterRows = new Map(statRows(after))
|
|
const rows = statRows(before).map(([label, value]) => [
|
|
label,
|
|
value,
|
|
afterRows.get(label) ?? 0,
|
|
])
|
|
return `## Comparaison quantitative\n\n${table(['Indicateur', 'Avant', 'Après'], rows)}`
|
|
}
|
|
|
|
function intro(): string {
|
|
return [
|
|
'# Journal des changements : révision de travail',
|
|
'',
|
|
'Généré par `pnpm import:equine` : import historique (`legacy-import/`) ' +
|
|
'puis changements rédigés (`curation/changes.json`). Ne pas modifier ' +
|
|
'à la main : relancer la commande.',
|
|
'',
|
|
'Niveaux : A = erreur démontrée, B = amélioration étayée, C = point ' +
|
|
'incertain. Les niveaux B et C demandent une confirmation professionnelle.',
|
|
].join('\n')
|
|
}
|
|
|
|
/** Fate of the legacy finding nodes a Report v2 may cite (report-migration-map.json). */
|
|
function migration(stats: MigrationMapStats): string {
|
|
const rows = [
|
|
['Nœuds du catalogue historique', String(stats.nodes)],
|
|
['Chemin identique', String(stats.identical)],
|
|
[
|
|
'Chemin renommé ou déplacé (suivi des renommages)',
|
|
String(stats.remapped),
|
|
],
|
|
[
|
|
'Relogé sous une autre catégorie (correspondance unique)',
|
|
String(stats.rehomed),
|
|
],
|
|
[
|
|
'Retiré : le bilan garde le constat avec ses libellés figés',
|
|
String(stats.removed),
|
|
],
|
|
]
|
|
return `## Migration des bilans v2\n\n${table(['Nœuds', 'Nombre'], rows)}`
|
|
}
|
|
|
|
export function renderChangelog(
|
|
result: { records: readonly ChangeRecord[] },
|
|
before: DocumentStats,
|
|
after: DocumentStats,
|
|
migrationStats: MigrationMapStats,
|
|
): string {
|
|
const sections = [
|
|
intro(),
|
|
comparison(before, after),
|
|
migration(migrationStats),
|
|
...LEVELS.map((level) => levelSection(level, result.records)),
|
|
]
|
|
return `${sections.join('\n\n')}\n`
|
|
}
|