Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

llms-generate.mjs

177 lines4.8 KB

1#!/usr/bin/env node
2import fs from 'node:fs'
3import path from 'node:path'
4​
5/*
6This script generates LLM rules in CJS and ESM formats in the dist/llms directory.
7​
8It takes a package name as an argument and generates the rules for that package.
9​
10We generate the files directly without compilation because we couldn't get TSC to generate the correct CJS format directly.
11We have to generate both the ESM version and the CJS version because our publint requires that we support both formats,
12even though the `vibe-rules` package only really supports CJS.
13*/
14​
15const DOCS_DIR = '../../docs'
16​
17const LLMS_DIR = './llms'
18const RULES_DIR = './llms/rules'
19​
20const packages = {
21 'react-router': [
22 {
23 paths: [`${DOCS_DIR}/router/framework/react/api/router`],
24 description: 'TanStack Router: API',
25 name: 'api',
26 globs: ['src/**/*.ts', 'src/**/*.tsx'],
27 },
28 {
29 paths: [`${DOCS_DIR}/router/framework/react/guide`],
30 description: 'TanStack Router: Guide',
31 name: 'guide',
32 globs: ['src/**/*.ts', 'src/**/*.tsx'],
33 },
34 {
35 paths: [`${DOCS_DIR}/router/framework/react/routing`],
36 description: 'TanStack Router: Routing',
37 name: 'routing',
38 globs: ['src/**/*.ts', 'src/**/*.tsx'],
39 },
40 {
41 paths: [`${DOCS_DIR}/router/framework/react/installation`],
42 description: 'TanStack Router: Installation',
43 name: 'installation',
44 globs: ['src/**/*.ts', 'src/**/*.tsx'],
45 },
46 {
47 paths: [
48 `${DOCS_DIR}/router/framework/react/overview.md`,
49 `${DOCS_DIR}/router/framework/react/quick-start.md`,
50 `${DOCS_DIR}/router/framework/react/decisions-on-dx.md`,
51 `${DOCS_DIR}/router/framework/react/devtools.md`,
52 `${DOCS_DIR}/router/framework/react/faq.md`,
53 ],
54 description: 'TanStack Router: Setup and Architecture',
55 name: 'setup-and-architecture',
56 globs: [
57 'package.json',
58 'vite.config.ts',
59 'tsconfig.json',
60 'src/**/*.ts',
61 'src/**/*.tsx',
62 ],
63 },
64 ],
66​
67const pkg = process.argv[2]
68if (!pkg) {
69 console.error('Usage: node scripts/llms-generate.mjs <package-name>')
70 process.exit(1)
72if (!packages[pkg]) {
73 console.error(`Package '${pkg}' not found`)
74 process.exit(1)
76​
77function camelCase(str) {
78 return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase())
80​
81function extractFrontMatter(content) {
82 const frontMatterEndIndex = content.indexOf('---', 3) + 3
83 const frontMatter = content.slice(0, frontMatterEndIndex)
84 const bodyContent = content.slice(frontMatterEndIndex).trim()
85 return { frontMatter, bodyContent }
87​
88function sanitizeMarkdown(markdownContent) {
89 return markdownContent.replace(/`/g, '\\`').replace(/\$\{/g, '\\${')
91​
92function convertMarkdownToTypeScriptESM(markdownContent) {
93 return `export default \`${sanitizeMarkdown(markdownContent)}\`;`
95​
96function mergeFiles(files, outputFile) {
97 let mergedContent = ''
98 for (const file of files) {
99 const content = fs.readFileSync(file, 'utf-8')
100 const { frontMatter, bodyContent } = extractFrontMatter(content)
101 const title = frontMatter.match(/title:\s*(.+)/)[1].trim()
102 mergedContent += `# ${title}\n\n${bodyContent}\n\n`
103 }
104 fs.writeFileSync(
105 outputFile,
106 convertMarkdownToTypeScriptESM(mergedContent),
107 'utf-8',
108 )
110​
111if (!fs.existsSync(RULES_DIR)) {
112 fs.mkdirSync(RULES_DIR, { recursive: true })
114​
115// Create the rules files
116const imports = []
117const rules = []
118for (const { paths, name, description, globs } of packages[pkg]) {
119 const files = []
120 for (const p of paths) {
121 if (fs.existsSync(p) && fs.statSync(p).isDirectory()) {
122 files.push(
123 ...fs
124 .readdirSync(p)
125 .filter((file) => file.endsWith('.md'))
126 .map((file) => path.join(p, file)),
127 )
128 } else {
129 files.push(p)
130 }
131 }
132 mergeFiles(files.flat(), path.join(RULES_DIR, `${name}.ts`))
133 imports.push(`import ${camelCase(name)} from './rules/${name}.js'`)
134​
135 rules.push(`{
136 name: '${name}',
137 description: '${description}',
138 rule: ${camelCase(name)},
139 alwaysApply: false,
140 globs: [${globs.map((glob) => `'${glob}'`).join(', ')}],
141}`)
143// Create the index.ts file
144const indexFile = path.join(LLMS_DIR, 'index.ts')
145const indexContent = `${imports.join('\n')}
146​
147import type { PackageRuleItem } from 'vibe-rules'
148​
149const rules: Array<PackageRuleItem> = [
150 ${rules.join(',\n')}
152​
153export default rules
155fs.writeFileSync(indexFile, indexContent, 'utf-8')
156​
157fs.writeFileSync(
158 path.join(LLMS_DIR, 'tsconfig.json'),
159 JSON.stringify(
160 {
161 compilerOptions: {
162 module: 'ESNext',
163 moduleResolution: 'bundler',
164 target: 'ESNext',
165 lib: ['ESNext', 'DOM'],
166 declaration: true,
167 outDir: '../dist/llms',
168 strict: true,
169 },
170 include: ['./index.ts', './rules/*.ts'],
171 exclude: ['node_modules', 'dist'],
172 },
173 null,
174 2,
175 ),
176 'utf-8',