Read-only technical audit before the SaaS redesign: 13 reports, a logical anatomical-sheet JSON built from real horse data, verbatim copies of the 52 relevant source files, and a self-contained ZIP.
58 lines
2.0 KiB
TypeScript
58 lines
2.0 KiB
TypeScript
import type { SpeciesManifest } from './types'
|
|
import type { Locale } from '@/i18n/types'
|
|
|
|
/**
|
|
* Auto-discovers every species manifest in `src/domain/species/*\/index.ts`
|
|
* via Vite's `import.meta.glob`. Each manifest module must default-export
|
|
* its `SpeciesManifest`. Drop a new folder with a conforming `index.ts`
|
|
* (and its `vertebrae.ts` + `pathologies.ts` siblings) and the species
|
|
* shows up everywhere — the AnimalForm dropdown, the editor canvas,
|
|
* the PDF export — without any registry edit.
|
|
*
|
|
* `eager: true` resolves modules synchronously at build time so the
|
|
* registry is a plain object literal in the bundled output (no async
|
|
* loading required to enumerate species).
|
|
*/
|
|
const modules = import.meta.glob<{ default: SpeciesManifest }>(
|
|
'./*/index.ts',
|
|
{ eager: true },
|
|
)
|
|
|
|
function buildRegistry(): Record<string, SpeciesManifest> {
|
|
const reg: Record<string, SpeciesManifest> = {}
|
|
for (const [path, mod] of Object.entries(modules)) {
|
|
const manifest = mod.default
|
|
if (!manifest || typeof manifest.id !== 'string' || !manifest.id) {
|
|
console.warn(`Species manifest at ${path} has no valid default export — skipping.`)
|
|
continue
|
|
}
|
|
if (reg[manifest.id]) {
|
|
console.warn(
|
|
`Duplicate species id "${manifest.id}" — keeping the first one (from ${path} ignored).`,
|
|
)
|
|
continue
|
|
}
|
|
reg[manifest.id] = manifest
|
|
}
|
|
return reg
|
|
}
|
|
|
|
const SPECIES_REGISTRY: Record<string, SpeciesManifest> = buildRegistry()
|
|
|
|
/** Returns a species manifest by id, or undefined if not registered. */
|
|
export function getSpecies(id: string): SpeciesManifest | undefined {
|
|
return SPECIES_REGISTRY[id]
|
|
}
|
|
|
|
/** Returns all registered species manifests, sorted alphabetically by labelFr. */
|
|
export function getAllSpecies(): SpeciesManifest[] {
|
|
return Object.values(SPECIES_REGISTRY).sort((a, b) =>
|
|
a.labelFr.localeCompare(b.labelFr, 'fr'),
|
|
)
|
|
}
|
|
|
|
/** Display label of a species for the given locale. */
|
|
export function speciesLabel(s: SpeciesManifest, locale: Locale): string {
|
|
return locale === 'en' ? s.labelEn : s.labelFr
|
|
}
|