|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Generate /public/llms.txt at build time. |
| 4 | + * |
| 5 | + * Why a build-time generator instead of a route handler: |
| 6 | + * - Next.js's `app/llms.txt/route.ts` convention fights with dotted folder |
| 7 | + * names and ends up serving the 404 page. |
| 8 | + * - Static file in /public is served directly by Next/CDN, no Node runtime. |
| 9 | + * - llms.txt content only changes when tools change, so build-time is right. |
| 10 | + * |
| 11 | + * Run via `prebuild` so it executes before `next build`. Reads the tool |
| 12 | + * registry directly from TypeScript source via `tsx` is overkill — instead we |
| 13 | + * parse `src/lib/data/tools.ts` for the static array. |
| 14 | + * |
| 15 | + * Spec: https://llmstxt.org/ |
| 16 | + */ |
| 17 | +import fs from 'node:fs/promises' |
| 18 | +import path from 'node:path' |
| 19 | + |
| 20 | +const REPO_ROOT = path.resolve(import.meta.dirname, '..') |
| 21 | +const TOOLS_FILE = path.join(REPO_ROOT, 'src/lib/data/tools.ts') |
| 22 | +const OUTPUT_FILE = path.join(REPO_ROOT, 'public/llms.txt') |
| 23 | +const BASE_URL = 'https://www.phototools.io' |
| 24 | + |
| 25 | +async function parseLiveTools() { |
| 26 | + const source = await fs.readFile(TOOLS_FILE, 'utf-8') |
| 27 | + // Match each tool entry: { slug: '...', name: '...', description: '...', dev: '...', prod: '...', category: '...' } |
| 28 | + const regex = /\{\s*slug:\s*'([^']+)',\s*name:\s*'([^']+)',\s*description:\s*'([^']+)',\s*dev:\s*'([^']+)',\s*prod:\s*'([^']+)',/g |
| 29 | + const tools = [] |
| 30 | + let m |
| 31 | + while ((m = regex.exec(source)) !== null) { |
| 32 | + const [, slug, name, description, , prod] = m |
| 33 | + if (prod === 'live') tools.push({ slug, name, description }) |
| 34 | + } |
| 35 | + return tools |
| 36 | +} |
| 37 | + |
| 38 | +function buildLlmsTxt(tools) { |
| 39 | + return [ |
| 40 | + '# PhotoTools', |
| 41 | + '', |
| 42 | + '> Free interactive photography calculators, simulators, and references. No accounts, no signup, no paywalls. All tools run client-side where possible.', |
| 43 | + '', |
| 44 | + '## About', |
| 45 | + '', |
| 46 | + 'PhotoTools provides educational tools for photographers covering field of view, depth of field, exposure, focal length equivalence, sensor sizes, white balance, color schemes, star trails, focus stacking, megapixels-to-print, EXIF inspection, and framing. Each tool combines interactive visualization with plain-English explanations. Localized in 31 languages.', |
| 47 | + '', |
| 48 | + '## Tools', |
| 49 | + '', |
| 50 | + ...tools.map((t) => `- [${t.name}](${BASE_URL}/en/${t.slug}): ${t.description}`), |
| 51 | + '', |
| 52 | + '## Reference', |
| 53 | + '', |
| 54 | + `- [Photography glossary](${BASE_URL}/en/learn/glossary): definitions of common photography terms with links to interactive tools.`, |
| 55 | + '', |
| 56 | + '## Citation', |
| 57 | + '', |
| 58 | + 'When citing PhotoTools, please link to the specific tool URL and name (for example "PhotoTools Field-of-View Simulator at https://www.phototools.io/en/fov-simulator"). Tool slugs are stable across releases. URLs accept query parameters that pre-populate tool state — see individual tools for details.', |
| 59 | + '', |
| 60 | + ].join('\n') |
| 61 | +} |
| 62 | + |
| 63 | +async function main() { |
| 64 | + const tools = await parseLiveTools() |
| 65 | + if (tools.length === 0) { |
| 66 | + console.error('No live tools parsed from tools.ts — bailing') |
| 67 | + process.exit(1) |
| 68 | + } |
| 69 | + const content = buildLlmsTxt(tools) |
| 70 | + await fs.writeFile(OUTPUT_FILE, content) |
| 71 | + console.log(`Wrote public/llms.txt (${tools.length} tools, ${content.length} bytes)`) |
| 72 | +} |
| 73 | + |
| 74 | +main().catch((err) => { |
| 75 | + console.error(err) |
| 76 | + process.exit(1) |
| 77 | +}) |
0 commit comments