103 lines
3.1 KiB
TypeScript
103 lines
3.1 KiB
TypeScript
import { readFileSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import type { Plugin } from 'vite'
|
|
|
|
/**
|
|
* Bundles the reference anatomical sheet (the generated seed draft) into the
|
|
* application as the virtual module `virtual:faunosteo-sheet`.
|
|
*
|
|
* Mode `draft` embeds `document.json`, `sheet-ref.json`,
|
|
* `report-migration-map.json`, the SVG assets of the seed folder and their
|
|
* manifest hashes (`assets.json`, carried into report archives); mode
|
|
* `none` exports null and the app shows "no sheet configured". The default is
|
|
* `draft` for the dev server and `none` for a build: a production bundle never
|
|
* carries the medically unvalidated draft unless `SHEET_BUNDLE=draft` is set
|
|
* on purpose (BDR-037, D2).
|
|
*/
|
|
|
|
export const SHEET_VIRTUAL_ID = 'virtual:faunosteo-sheet'
|
|
const RESOLVED_ID = `\0${SHEET_VIRTUAL_ID}`
|
|
|
|
export type SheetBundleMode = 'draft' | 'none'
|
|
|
|
export function resolveSheetBundleMode(
|
|
env: Record<string, string | undefined>,
|
|
command: 'build' | 'serve',
|
|
): SheetBundleMode {
|
|
const raw = env.SHEET_BUNDLE
|
|
if (raw === 'draft' || raw === 'none') return raw
|
|
if (raw !== undefined && raw !== '') {
|
|
throw new Error(`SHEET_BUNDLE must be "draft" or "none", got "${raw}"`)
|
|
}
|
|
return command === 'serve' ? 'draft' : 'none'
|
|
}
|
|
|
|
interface SeedFiles {
|
|
ref: unknown
|
|
document: unknown
|
|
migrationMap: unknown
|
|
assets: Record<string, string>
|
|
assetHashes: Record<string, string>
|
|
watched: string[]
|
|
}
|
|
|
|
function readSeed(seedDir: string): SeedFiles {
|
|
const json = (name: string) =>
|
|
JSON.parse(readFileSync(join(seedDir, name), 'utf8')) as unknown
|
|
const manifest = json('assets.json') as {
|
|
file: string
|
|
sha256: string
|
|
svg_asset_id: string
|
|
}[]
|
|
const assets: Record<string, string> = {}
|
|
const assetHashes: Record<string, string> = {}
|
|
for (const asset of manifest) {
|
|
assets[asset.svg_asset_id] = readFileSync(
|
|
join(seedDir, 'assets', asset.file),
|
|
'utf8',
|
|
)
|
|
assetHashes[asset.svg_asset_id] = asset.sha256
|
|
}
|
|
return {
|
|
ref: json('sheet-ref.json'),
|
|
document: json('document.json'),
|
|
migrationMap: json('report-migration-map.json'),
|
|
assets,
|
|
assetHashes,
|
|
watched: [
|
|
'sheet-ref.json',
|
|
'document.json',
|
|
'report-migration-map.json',
|
|
'assets.json',
|
|
...manifest.map((a) => `assets/${a.file}`),
|
|
].map((name) => join(seedDir, name)),
|
|
}
|
|
}
|
|
|
|
export function faunosteoSheetBundle(options: {
|
|
mode: SheetBundleMode
|
|
seedDir: string
|
|
}): Plugin {
|
|
return {
|
|
name: 'faunosteo-sheet-bundle',
|
|
resolveId(id) {
|
|
return id === SHEET_VIRTUAL_ID ? RESOLVED_ID : null
|
|
},
|
|
load(id) {
|
|
if (id !== RESOLVED_ID) return null
|
|
if (options.mode === 'none') return 'export const bundledSheet = null\n'
|
|
const seed = readSeed(options.seedDir)
|
|
for (const file of seed.watched) this.addWatchFile(file)
|
|
const payload = {
|
|
status: 'draft',
|
|
ref: seed.ref,
|
|
document: seed.document,
|
|
migrationMap: seed.migrationMap,
|
|
assets: seed.assets,
|
|
assetHashes: seed.assetHashes,
|
|
}
|
|
return `export const bundledSheet = ${JSON.stringify(payload)}\n`
|
|
},
|
|
}
|
|
}
|