#!/usr/bin/env node /* Generates the two committed artefacts under api/coach/core/ that both runtimes import: * * api/coach/core/library-data.js the exercise catalogue, from frontend/src/lib/exercises-data.js * api/coach/core/prompts.js the prompt texts, from api/coach/prompts/*.md * * node scripts/build-coach-assets.mjs # write * node scripts/build-coach-assets.mjs --check # fail if stale (CI) * * The API needs the catalogue for two things the client cannot do for it: validating that * every exercise id a proposal references actually exists (FR-16), and building payloads for * scheduled reviews, which run with nobody's browser open. Rather than teach the api build to * reach into frontend/, the index is generated and committed — the same arrangement the * translated instruction packs in src/instr/ already use. * * Only the fields the Coach reasons over are kept: an id, a name, and the three taxonomy * fields it filters on. Instructions, images and secondary muscles stay out — they would * quadruple a payload the model has no use for. * * The prompts are generated for a different reason: the server runs under bare node and the * phone under Vite, and neither `fs.readFileSync` nor a `?raw` import works in both. A plain * ES module does. The .md files stay the thing you edit. */ import { readdirSync, readFileSync, writeFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); const core = join(root, 'api', 'coach', 'core'); const check = process.argv.includes('--check'); const outputs = []; // --- library --- { const src = join(root, 'frontend', 'src', 'lib', 'exercises-data.js'); const { EXDB } = await import(pathToFileURL(src).href); const index = EXDB.map(e => ({ id: e.id, n: e.n, bp: e.bp, tg: e.tg, eq: e.eq })); const body = '/* GENERATED by scripts/build-coach-assets.mjs from frontend/src/lib/exercises-data.js — do not edit. */\n' + `export const GENERATED_FROM = 'frontend/src/lib/exercises-data.js';\n` + `export const COUNT = ${index.length};\n` + 'export const EXERCISES = ' + JSON.stringify(index) + ';\n'; outputs.push({ file: join(core, 'library-data.js'), body, note: `${index.length} exercises` }); } // --- prompts --- { const dir = join(root, 'api', 'coach', 'prompts'); const names = readdirSync(dir).filter(f => f.endsWith('.md')).sort(); const entries = names.map(f => ` ${JSON.stringify(f.replace(/\.md$/, ''))}: ${JSON.stringify(readFileSync(join(dir, f), 'utf8'))}`); const body = '/* GENERATED by scripts/build-coach-assets.mjs from api/coach/prompts/*.md — edit those, not this. */\n' + 'export const PROMPTS = Object.freeze({\n' + entries.join(',\n') + '\n});\n'; outputs.push({ file: join(core, 'prompts.js'), body, note: `${names.length} prompts` }); } let stale = 0; for (const { file, body, note } of outputs) { const rel = file.slice(root.length + 1); if (check) { let current = null; try { current = readFileSync(file, 'utf8'); } catch { /* missing counts as stale */ } if (current !== body) { stale++; console.error(`${rel} is out of date — run: node scripts/build-coach-assets.mjs`); } else console.log(`${rel} in sync (${note}).`); } else { writeFileSync(file, body); console.log(`Wrote ${rel} — ${note}, ${(body.length / 1024).toFixed(0)} KB.`); } } if (stale) process.exit(1);