Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kentcdodds/lineage

src/seed-highlights.ts

65 lines · 2.2 KB · TypeScript
import commentary from './commentary.ts'
import getSeries from './series.ts'
import { listCommentaries } from './storage.ts'

/**
 * Attach short labels to unusually large countable swings already in storage.
 * Safe to re-run: existing SHAs keep their current commentary.
 *
 * @returns Newly written highlights
 *
 * @example
 * import seedHighlights from 'kody:@kentcdodds/lineage/seed-highlights'
 *
 * const seeded = await seedHighlights()
 */
export default async function seedHighlights() {
	const existing = new Set((await listCommentaries()).map((item) => item.sha))
	const series = await getSeries({ mode: 'net' })
	if (series.points.length === 0) {
		return { ok: true, seeded: [], reason: 'no-commits' }
	}

	const ranked = [...series.points].sort(
		(left, right) => Math.abs(right.net) - Math.abs(left.net),
	)
	const first = series.points[0]
	const last = series.points[series.points.length - 1]
	const biggestUp = ranked.find((point) => point.net > 0)
	const biggestDown = ranked.find((point) => point.net < 0)
	const candidates = [
		first && {
			sha: first.sha,
			label: 'First counted commit',
			narrative:
				'The earliest stored main commit. Cumulative countable lines start here after generated files, lockfiles, and vendor output are excluded.',
		},
		biggestUp &&
			biggestUp.sha !== first.sha && {
				sha: biggestUp.sha,
				label: 'Largest countable rise',
				narrative: `${biggestUp.message} added a net ${biggestUp.net} countable lines after generated-file filters.`,
			},
		biggestDown && {
			sha: biggestDown.sha,
			label: 'Largest countable drop',
			narrative: `${biggestDown.message} removed a net ${Math.abs(biggestDown.net)} countable lines — usually a deletion or extraction, not a generated-file swing.`,
		},
		last &&
			last.sha !== first.sha && {
				sha: last.sha,
				label: 'Latest counted commit',
				narrative:
					'Newest ingested main commit. Future pushes to main should fill the next point automatically via the GitHub webhook.',
			},
	].filter(Boolean) as Array<{ sha: string; label: string; narrative: string }>

	const seeded = []
	for (const item of candidates) {
		if (existing.has(item.sha)) continue
		await commentary(item)
		seeded.push(item)
		existing.add(item.sha)
	}
	return { ok: true, seeded }
}