feat(species): auto-discover species manifests + scaffold script
Registry
--------
src/domain/species/registry.ts previously hard-coded the species list,
which meant adding a new animal forced an edit in two places. The
registry now uses Vite's `import.meta.glob('./*/index.ts', { eager: true })`
and registers every manifest that default-exports a SpeciesManifest.
Duplicate ids log a warn and the first wins. Existing named imports
keep working — equine/index.ts adds `export default equine` next to the
named export.
The species dropdown is also now sorted alphabetically by labelFr (FR
locale) instead of insertion order, so two species don't shuffle
position based on glob ordering.
Scaffold script
---------------
`pnpm scaffold:species` (scripts/scaffold-species.mjs) is an
interactive Node ESM prompt that asks for the id, FR/EN labels, asset
folder, main SVG filename, detail SVG mappings, and the vertebral
formula, then writes:
src/domain/species/<id>/index.ts (with `export default`)
src/domain/species/<id>/vertebrae.ts
src/domain/species/<id>/pathologies.ts (Zod-parses the JSON)
src/domain/species/<id>/pathologies.json (minimal valid skeleton)
public/species/<assetFolder>/ (directory placeholder)
The script refuses to overwrite existing files, so it's safe to rerun
after typos. SVG assets are not generated — drop them in the public
folder yourself.
Findings JSON updates
---------------------
No runtime hot-reload mechanism is needed in dev: `pathologies.ts`
imports `pathologies.json` and runs `SpeciesFindingsSchema.parse(data)`
at module load, so a JSON edit triggers Vite HMR + an immediate Zod
validation pass. In prod, rebuild and redeploy.
Docs
----
docs/ADDING-A-SPECIES.md rewritten around the new flow: quick start via
the scaffold script first, manual instructions second, and removed the
stale references to the flat `Pathology[]` model and the no-longer-
required registry edit step.
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
700567fc4f
commit
70b7f20f6f
5 files changed
+432
-170
No files matched your search
@@ -11,3 +11,8 @@ export const equine: SpeciesManifest = {
|
||||
vertebralFormula: EQUINE_VERTEBRAL_FORMULA,
|
||||
findings: () => import('./pathologies'),
|
||||
}
|
||||
|
||||
// Default export is the convention picked up by the registry's
|
||||
// import.meta.glob() auto-discovery. Keep the named export for any
|
||||
// code that still imports the manifest by name.
|
||||
export default equine
|
||||
@@ -1,17 +1,51 @@
|
||||
import type { SpeciesManifest } from './types'
|
||||
import { equine } from './equine'
|
||||
|
||||
/** Map of speciesId → manifest. Add new species here. */
|
||||
const SPECIES_REGISTRY: Record<string, SpeciesManifest> = {
|
||||
[equine.id]: equine,
|
||||
/**
|
||||
* 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. */
|
||||
/** Returns all registered species manifests, sorted alphabetically by labelFr. */
|
||||
export function getAllSpecies(): SpeciesManifest[] {
|
||||
return Object.values(SPECIES_REGISTRY)
|
||||
return Object.values(SPECIES_REGISTRY).sort((a, b) =>
|
||||
a.labelFr.localeCompare(b.labelFr, 'fr'),
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user