/** 지역 이야기 프롬프트를 백엔드가 읽을 JSON 으로 뽑는다. */ import {mkdirSync, writeFileSync} from 'node:fs'; import {dirname, resolve} from 'node:path'; import {fileURLToPath} from 'node:url'; const here = dirname(fileURLToPath(import.meta.url)); const OUT = resolve(here, '../../backend/services/prompts/section_prompts.json'); // TS 를 그대로 읽을 수 없으므로 원문에서 값을 떼어 낸다 — 이 파일은 순수 상수 선언이라 파서를 세울 이유가 없다. const src = await import('node:fs').then((fs) => fs.readFileSync(resolve(here, '../src/lib/section-prompts.ts'), 'utf8'), ); function block(name) { const m = src.match(new RegExp(`export const ${name} = \`([\\s\\S]*?)\`;`)); if (!m) throw new Error(`${name} 를 찾지 못했다 — section-prompts.ts 의 모양이 바뀌었다`); return m[1]; } const rules = block('SECTION_PROMPT_RULES'); // 목록을 여기 손으로 적지 않는다. const kinds = (() => { const m = src.match(/export const STORY_KINDS: StoryKind\[\] = \[([\s\S]*?)\];/); if (!m) throw new Error('STORY_KINDS 를 찾지 못했다 — section-prompts.ts 의 모양이 바뀌었다'); return [...m[1].matchAll(/'([^']+)'/g)].map((hit) => hit[1]); })(); const specs = {}; for (const kind of kinds) { const body = src.match(new RegExp(`\\n ${kind}: \\{([\\s\\S]*?)\\n \\},`)); if (!body) throw new Error(`${kind} 항목을 찾지 못했다`); const take = (field) => { const m = body[1].match(new RegExp(`${field}: \`([\\s\\S]*?)\`,`)); if (!m) throw new Error(`${kind}.${field} 를 찾지 못했다`); return m[1]; }; const label = body[1].match(/label: '([^']*)'/); const maxItems = body[1].match(/maxItems: (\d+)/); if (!label || !maxItems) throw new Error(`${kind} 의 label/maxItems 를 찾지 못했다`); specs[kind] = { kind, label: label[1], maxItems: Number(maxItems[1]), task: take('task'), rules: take('rules'), }; } mkdirSync(dirname(OUT), {recursive: true}); writeFileSync( OUT, `${JSON.stringify({_generated: 'npm run export:prompts — 손으로 고치지 않는다', rules, specs}, null, 2)}\n`, 'utf8', ); console.log(`프롬프트 ${kinds.length}종 → ${OUT}`);