Open History to see the last commit
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: [
42 `${DOCS_DIR}/router/framework/react/overview.md`,
43 `${DOCS_DIR}/router/framework/react/quick-start.md`,
44 `${DOCS_DIR}/router/framework/react/devtools.md`,
45 `${DOCS_DIR}/router/framework/react/migrate-from-react-router.md`,
46 `${DOCS_DIR}/router/framework/react/migrate-from-react-location.md`,
47 `${DOCS_DIR}/router/framework/react/faq.md`,
48 ],
49 description: 'TanStack Router: Setup and Architecture',
50 name: 'setup-and-architecture',
51 globs: [
52 'package.json',
53 'vite.config.ts',
54 'tsconfig.json',
55 'src/**/*.ts',
56 'src/**/*.tsx',
57 ],
58 },
59 ],
60}
61β
62const pkg = process.argv[2]
63if (!pkg) {
64 console.error('Usage: node scripts/llms-generate.mjs <package-name>')
65 process.exit(1)
66}
67if (!packages[pkg]) {
68 console.error(`Package '${pkg}' not found`)
69 process.exit(1)
70}
71β
72function camelCase(str) {
73 return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase())
74}
75β
76function extractFrontMatter(content) {
77 const frontMatterEndIndex = content.indexOf('---', 3) + 3
78 const frontMatter = content.slice(0, frontMatterEndIndex)
79 const bodyContent = content.slice(frontMatterEndIndex).trim()
80 return { frontMatter, bodyContent }
81}
82β
83function sanitizeMarkdown(markdownContent) {
84 return markdownContent.replace(/`/g, '\\`').replace(/\$\{/g, '\\${')
85}
86β
87function convertMarkdownToTypeScriptESM(markdownContent) {
88 return `export default \`${sanitizeMarkdown(markdownContent)}\`;`
89}
90β
91function mergeFiles(files, outputFile) {
92 let mergedContent = ''
93 for (const file of files) {
94 const content = fs.readFileSync(file, 'utf-8')
95 const { frontMatter, bodyContent } = extractFrontMatter(content)
96 const title = frontMatter.match(/title:\s*(.+)/)[1].trim()
97 mergedContent += `# ${title}\n\n${bodyContent}\n\n`
98 }
99 fs.writeFileSync(
100 outputFile,
101 convertMarkdownToTypeScriptESM(mergedContent),
102 'utf-8',
103 )
104}
105β
106if (!fs.existsSync(RULES_DIR)) {
107 fs.mkdirSync(RULES_DIR, { recursive: true })
108}
109β
110// Create the rules files
111const imports = []
112const rules = []
113for (const { paths, name, description, globs } of packages[pkg]) {
114 const files = []
115 for (const p of paths) {
116 if (fs.existsSync(p) && fs.statSync(p).isDirectory()) {
117 files.push(
118 ...fs
119 .readdirSync(p)
120 .filter((file) => file.endsWith('.md'))
121 .map((file) => path.join(p, file)),
122 )
123 } else {
124 files.push(p)
125 }
126 }
127 mergeFiles(files.flat(), path.join(RULES_DIR, `${name}.ts`))
128 imports.push(`import ${camelCase(name)} from './rules/${name}.js'`)
129β
130 rules.push(`{
131 name: '${name}',
132 description: '${description}',
133 rule: ${camelCase(name)},
134 alwaysApply: false,
135 globs: [${globs.map((glob) => `'${glob}'`).join(', ')}],
136}`)
137}
138// Create the index.ts file
139const indexFile = path.join(LLMS_DIR, 'index.ts')
140const indexContent = `${imports.join('\n')}
141β
142import type { PackageRuleItem } from 'vibe-rules'
143β
144const rules: Array<PackageRuleItem> = [
145 ${rules.join(',\n')}
146]
147β
148export default rules
149`
150fs.writeFileSync(indexFile, indexContent, 'utf-8')
151β
152fs.writeFileSync(
153 path.join(LLMS_DIR, 'tsconfig.json'),
154 JSON.stringify(
155 {
156 compilerOptions: {
157 module: 'ESNext',
158 moduleResolution: 'bundler',
159 target: 'ESNext',
160 lib: ['ESNext', 'DOM'],
161 declaration: true,
162 outDir: '../dist/llms',
163 strict: true,
164 },
165 include: ['./index.ts', './rules/*.ts'],
166 exclude: ['node_modules', 'dist'],
167 },
168 null,
169 2,
170 ),
171 'utf-8',
172)