prunelle 0.0.0-stage → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. package/LICENSE +21 -0
  2. package/llms.txt +245 -0
  3. package/package.json +62 -4
  4. package/src/a11y/declaration.ts +373 -0
  5. package/src/a11y/index.ts +477 -0
  6. package/src/a11y/rgaa-map.ts +230 -0
  7. package/src/ambient.d.ts +6 -0
  8. package/src/attrs.ts +43 -0
  9. package/src/build/bundle.ts +661 -0
  10. package/src/build/byte-size.ts +11 -0
  11. package/src/build/carry.ts +57 -0
  12. package/src/build/client-entries.ts +107 -0
  13. package/src/build/client-plugin.ts +175 -0
  14. package/src/build/client-prune.ts +168 -0
  15. package/src/build/compile.ts +130 -0
  16. package/src/build/ecoindex.ts +238 -0
  17. package/src/build/files.ts +171 -0
  18. package/src/build/first-load.ts +235 -0
  19. package/src/build/graph.ts +237 -0
  20. package/src/build/index.ts +678 -0
  21. package/src/build/is-inside.ts +12 -0
  22. package/src/build/island-promote.ts +311 -0
  23. package/src/build/islands.ts +452 -0
  24. package/src/build/manifest.ts +204 -0
  25. package/src/build/output.ts +81 -0
  26. package/src/build/plugin-host.ts +294 -0
  27. package/src/build/precompress.ts +139 -0
  28. package/src/build/prerender.ts +288 -0
  29. package/src/build/reach.ts +200 -0
  30. package/src/build/render-rules.ts +205 -0
  31. package/src/build/report.ts +290 -0
  32. package/src/build/resolve.ts +52 -0
  33. package/src/build/routes.ts +342 -0
  34. package/src/build/server-entry.ts +38 -0
  35. package/src/build/static-headers.ts +112 -0
  36. package/src/build/static.ts +209 -0
  37. package/src/build/status-pages.ts +127 -0
  38. package/src/build/style-bundles.ts +526 -0
  39. package/src/cli/check-forms.ts +636 -0
  40. package/src/cli/check-imports.ts +31 -0
  41. package/src/cli/check-links.ts +632 -0
  42. package/src/cli/check-security.ts +64 -0
  43. package/src/cli/check.ts +409 -0
  44. package/src/cli/commands.ts +372 -0
  45. package/src/cli/docs.ts +403 -0
  46. package/src/cli/index.ts +70 -0
  47. package/src/cli/inspect.ts +406 -0
  48. package/src/cli/mcp-tools.ts +492 -0
  49. package/src/cli/mcp.ts +205 -0
  50. package/src/cli/new.ts +52 -0
  51. package/src/cli/render.ts +416 -0
  52. package/src/cli/upgrade-names.ts +209 -0
  53. package/src/cli/upgrade.ts +367 -0
  54. package/src/compiler/analyze.ts +651 -0
  55. package/src/compiler/ast.ts +82 -0
  56. package/src/compiler/browser-globals.ts +250 -0
  57. package/src/compiler/cacheability-private.ts +566 -0
  58. package/src/compiler/cacheability.ts +335 -0
  59. package/src/compiler/compliance-eco.ts +71 -0
  60. package/src/compiler/compliance-rgaa.ts +614 -0
  61. package/src/compiler/compliance-urls.ts +148 -0
  62. package/src/compiler/compliance.ts +653 -0
  63. package/src/compiler/data-keys.ts +302 -0
  64. package/src/compiler/dev-source.ts +87 -0
  65. package/src/compiler/dom-attrs.ts +452 -0
  66. package/src/compiler/dom.ts +641 -0
  67. package/src/compiler/emit.ts +131 -0
  68. package/src/compiler/expr-context.ts +587 -0
  69. package/src/compiler/foreign-imports.ts +88 -0
  70. package/src/compiler/index.ts +8 -0
  71. package/src/compiler/interactivity.ts +366 -0
  72. package/src/compiler/mistakes.ts +520 -0
  73. package/src/compiler/node-env.ts +50 -0
  74. package/src/compiler/patch.ts +97 -0
  75. package/src/compiler/props-body.ts +60 -0
  76. package/src/compiler/props.ts +511 -0
  77. package/src/compiler/scope.ts +635 -0
  78. package/src/compiler/server-only.ts +365 -0
  79. package/src/compiler/shared.ts +397 -0
  80. package/src/compiler/signal-reads.ts +246 -0
  81. package/src/compiler/slots.ts +227 -0
  82. package/src/compiler/spread-handlers.ts +295 -0
  83. package/src/compiler/ssr-helpers.ts +555 -0
  84. package/src/compiler/ssr-island.ts +90 -0
  85. package/src/compiler/ssr-prunelle-tags.ts +69 -0
  86. package/src/compiler/ssr.ts +525 -0
  87. package/src/context.ts +199 -0
  88. package/src/dev/child.ts +30 -0
  89. package/src/dev/error-log.ts +177 -0
  90. package/src/dev/host-check.ts +32 -0
  91. package/src/dev/index.ts +268 -0
  92. package/src/dev/modules.ts +68 -0
  93. package/src/dev/open-editor.ts +228 -0
  94. package/src/dev/protocol.ts +24 -0
  95. package/src/dev/queue.ts +79 -0
  96. package/src/dev/reload.ts +159 -0
  97. package/src/dev/supervisor.ts +212 -0
  98. package/src/dev/watchers.ts +276 -0
  99. package/src/dom/action.ts +187 -0
  100. package/src/dom/attrs.ts +84 -0
  101. package/src/dom/claim.ts +120 -0
  102. package/src/dom/extras.ts +579 -0
  103. package/src/dom/index.ts +329 -0
  104. package/src/dom/island.ts +318 -0
  105. package/src/dom/loop.ts +484 -0
  106. package/src/dom/revive.ts +36 -0
  107. package/src/eco/declaration.ts +328 -0
  108. package/src/eco/index.ts +69 -0
  109. package/src/index.ts +22 -0
  110. package/src/jsx-runtime.ts +566 -0
  111. package/src/plugins/config.ts +92 -0
  112. package/src/plugins/context.ts +109 -0
  113. package/src/plugins/index.ts +14 -0
  114. package/src/plugins/load.ts +90 -0
  115. package/src/plugins/order.ts +148 -0
  116. package/src/plugins/public.ts +15 -0
  117. package/src/plugins/requires.ts +47 -0
  118. package/src/plugins/services.ts +119 -0
  119. package/src/plugins/types.ts +278 -0
  120. package/src/reactivity/graph.ts +259 -0
  121. package/src/reactivity/index.ts +541 -0
  122. package/src/reactivity/selector.ts +78 -0
  123. package/src/reactivity/store.ts +252 -0
  124. package/src/server/action-flash.ts +158 -0
  125. package/src/server/action-protect.ts +199 -0
  126. package/src/server/action-registry.ts +78 -0
  127. package/src/server/action.ts +511 -0
  128. package/src/server/app-entry.ts +23 -0
  129. package/src/server/app-secret.ts +63 -0
  130. package/src/server/assets.ts +168 -0
  131. package/src/server/compress.ts +646 -0
  132. package/src/server/consent.ts +290 -0
  133. package/src/server/cookies.ts +153 -0
  134. package/src/server/document.ts +286 -0
  135. package/src/server/env.ts +47 -0
  136. package/src/server/errors.ts +396 -0
  137. package/src/server/form-schema.ts +337 -0
  138. package/src/server/health.ts +275 -0
  139. package/src/server/http.ts +588 -0
  140. package/src/server/index.ts +615 -0
  141. package/src/server/lifecycle.ts +75 -0
  142. package/src/server/loader.ts +131 -0
  143. package/src/server/page.ts +122 -0
  144. package/src/server/param-matchers.ts +112 -0
  145. package/src/server/plugin-host.ts +267 -0
  146. package/src/server/prerendered.ts +71 -0
  147. package/src/server/public.ts +20 -0
  148. package/src/server/rate-limit.ts +199 -0
  149. package/src/server/region.ts +96 -0
  150. package/src/server/request-body.ts +27 -0
  151. package/src/server/request-path.ts +73 -0
  152. package/src/server/request-scope.ts +22 -0
  153. package/src/server/route-cache.ts +541 -0
  154. package/src/server/route-table.ts +424 -0
  155. package/src/server/router.ts +261 -0
  156. package/src/server/routing.ts +388 -0
  157. package/src/server/sanitize-html.ts +309 -0
  158. package/src/server/security.ts +550 -0
  159. package/src/server/serve-handlers.ts +93 -0
  160. package/src/server/served-file.ts +128 -0
  161. package/src/server/standard-schema.ts +104 -0
  162. package/src/server/start.ts +143 -0
  163. package/src/server/static-dir.ts +246 -0
  164. package/src/server/stream.ts +233 -0
  165. package/src/ssr/consent-banner.ts +58 -0
  166. package/src/ssr/context.ts +399 -0
  167. package/src/ssr/index.ts +624 -0
  168. package/src/ssr/navigation.ts +96 -0
  169. package/src/ssr/region.ts +192 -0
  170. package/src/style.ts +96 -0
  171. package/src/test/browser.ts +201 -0
  172. package/src/test/index.ts +262 -0
  173. package/src/ts-plugin/diagnostics.ts +505 -0
  174. package/src/ts-plugin/fixes.ts +364 -0
  175. package/src/ts-plugin/index.ts +194 -0
  176. package/src/unrenderable.ts +35 -0
  177. package/templates/_shared/AGENTS.md +10 -0
  178. package/templates/_shared/CLAUDE.md +1 -0
  179. package/templates/_shared/gitignore +8 -0
  180. package/templates/_shared/mcp.json +11 -0
  181. package/templates/_shared/public/robots.txt +2 -0
  182. package/templates/_shared/src/routes/legal.tsx +22 -0
  183. package/templates/_shared/tsconfig.json +25 -0
  184. package/templates/app/.env.example +8 -0
  185. package/templates/app/package.json +22 -0
  186. package/templates/app/prunelle.config.ts +13 -0
  187. package/templates/app/src/auth.ts +12 -0
  188. package/templates/app/src/mail.ts +4 -0
  189. package/templates/app/src/routes/_layout.tsx +40 -0
  190. package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
  191. package/templates/app/src/routes/dashboard/index.tsx +47 -0
  192. package/templates/app/src/routes/index.tsx +15 -0
  193. package/templates/app/src/routes/login.tsx +53 -0
  194. package/templates/app/src/routes/privacy.tsx +25 -0
  195. package/templates/app/src/styles/app.css +14 -0
  196. package/templates/app/tests/app.test.ts +73 -0
  197. package/templates/blog/content/posts/welcome.md +9 -0
  198. package/templates/blog/package.json +22 -0
  199. package/templates/blog/prunelle.config.ts +23 -0
  200. package/templates/blog/src/posts.ts +26 -0
  201. package/templates/blog/src/routes/_layout.tsx +25 -0
  202. package/templates/blog/src/routes/blog/[slug].tsx +31 -0
  203. package/templates/blog/src/routes/index.tsx +33 -0
  204. package/templates/blog/src/styles/app.css +15 -0
  205. package/templates/fullstack/package.json +19 -0
  206. package/templates/fullstack/prunelle.config.ts +6 -0
  207. package/templates/fullstack/src/actions/greet.ts +19 -0
  208. package/templates/fullstack/src/components/Counter.tsx +10 -0
  209. package/templates/fullstack/src/components/GreetForm.tsx +28 -0
  210. package/templates/fullstack/src/routes/_layout.tsx +18 -0
  211. package/templates/fullstack/src/routes/api/health.ts +5 -0
  212. package/templates/fullstack/src/routes/index.tsx +13 -0
  213. package/templates/fullstack/src/styles/app.css +39 -0
  214. package/templates/fullstack/tests/app.test.ts +37 -0
  215. package/templates/minimal/package.json +19 -0
  216. package/templates/minimal/prunelle.config.ts +6 -0
  217. package/templates/minimal/src/components/Counter.tsx +10 -0
  218. package/templates/minimal/src/routes/_layout.tsx +18 -0
  219. package/templates/minimal/src/routes/index.tsx +11 -0
  220. package/templates/minimal/src/styles/app.css +32 -0
  221. package/ts-plugin.cjs +4277 -0
  222. package/README.md +0 -3
@@ -0,0 +1,403 @@
1
+ // `prunelle docs <words>`: discover and search installed `llms.txt` files for `prunelle` and `@prunellejs/*` packages.
2
+
3
+ import { readdirSync, readFileSync, statSync } from 'node:fs'
4
+ import { dirname, join, resolve } from 'node:path'
5
+ import { relPosix } from '../build/files'
6
+
7
+ const HEADING_RE = /^(#{1,3})\s+(.+?)\s*$/
8
+ const BOLD_BULLET_RE = /^- \*\*(.+?)\*\*/
9
+ const FENCE_RE = /^\s*```/
10
+ const WORD_SPLIT_RE = /\s+/
11
+ const SCOPED_PACKAGE_DIRS = ['@prunellejs', '@prunelle'] as const
12
+ const DEFAULT_LIMIT = 20
13
+
14
+ export interface LlmsPackageDoc {
15
+ packageName: string
16
+ version: string | undefined
17
+ file: string
18
+ relFile: string
19
+ content: string
20
+ }
21
+
22
+ export interface DocsMatch {
23
+ packageName: string
24
+ version: string | undefined
25
+ file: string
26
+ relFile: string
27
+ heading: string
28
+ line: number
29
+ score: number
30
+ content: string
31
+ }
32
+
33
+ export interface DocsSearchResult {
34
+ query: string
35
+ words: string[]
36
+ packages: { name: string, version: string | undefined, file: string }[]
37
+ matches: DocsMatch[]
38
+ }
39
+
40
+ interface ParsedSection {
41
+ heading: string
42
+ line: number
43
+ content: string
44
+ }
45
+
46
+ interface HeadingBlock {
47
+ heading: string
48
+ line: number
49
+ lines: { num: number, text: string }[]
50
+ }
51
+
52
+ function isFile(path: string): boolean {
53
+ const st = statSync(path, { throwIfNoEntry: false })
54
+ return st !== undefined && st.isFile()
55
+ }
56
+
57
+ function isDirectory(path: string): boolean {
58
+ const st = statSync(path, { throwIfNoEntry: false })
59
+ return st !== undefined && st.isDirectory()
60
+ }
61
+
62
+ function readPackageMeta(pkgDir: string, fallbackName: string): { name: string, version: string | undefined } {
63
+ const pkgJsonPath = join(pkgDir, 'package.json')
64
+ if (!isFile(pkgJsonPath))
65
+ return { name: fallbackName, version: undefined }
66
+ try {
67
+ const parsed = JSON.parse(readFileSync(pkgJsonPath, 'utf8')) as { name?: unknown, version?: unknown }
68
+ const name = typeof parsed.name === 'string' && parsed.name.length > 0 ? parsed.name : fallbackName
69
+ const version = typeof parsed.version === 'string' && parsed.version.length > 0 ? parsed.version : undefined
70
+ return { name, version }
71
+ }
72
+ catch {
73
+ return { name: fallbackName, version: undefined }
74
+ }
75
+ }
76
+
77
+ function addPackageDoc(
78
+ docs: LlmsPackageDoc[],
79
+ seen: Set<string>,
80
+ root: string,
81
+ pkgDir: string,
82
+ fallbackName: string,
83
+ ): void {
84
+ const llmsPath = join(pkgDir, 'llms.txt')
85
+ if (!isFile(llmsPath))
86
+ return
87
+ const meta = readPackageMeta(pkgDir, fallbackName)
88
+ if (seen.has(meta.name))
89
+ return
90
+ seen.add(meta.name)
91
+ docs.push({
92
+ packageName: meta.name,
93
+ version: meta.version,
94
+ file: llmsPath,
95
+ relFile: relPosix(root, llmsPath),
96
+ content: readFileSync(llmsPath, 'utf8'),
97
+ })
98
+ }
99
+
100
+ function scanScopedPackages(
101
+ docs: LlmsPackageDoc[],
102
+ seen: Set<string>,
103
+ root: string,
104
+ nodeModulesDir: string,
105
+ ): void {
106
+ for (const scope of SCOPED_PACKAGE_DIRS) {
107
+ const scopeDir = join(nodeModulesDir, scope)
108
+ if (!isDirectory(scopeDir))
109
+ continue
110
+ let entries: string[]
111
+ try {
112
+ entries = readdirSync(scopeDir)
113
+ }
114
+ catch {
115
+ continue
116
+ }
117
+ for (const entry of entries)
118
+ addPackageDoc(docs, seen, root, join(scopeDir, entry), `${scope}/${entry}`)
119
+ }
120
+ }
121
+
122
+ /** Discovers `llms.txt` files for `prunelle` and installed `@prunellejs/*` (and `@prunelle/*`) packages from `rootDir` up through ancestor `node_modules`. */
123
+ export function discoverLlmsDocs(rootDir: string): LlmsPackageDoc[] {
124
+ const root = resolve(rootDir)
125
+ const docs: LlmsPackageDoc[] = []
126
+ const seen = new Set<string>()
127
+
128
+ let dir = root
129
+ while (true) {
130
+ const nodeModulesDir = join(dir, 'node_modules')
131
+ if (isDirectory(nodeModulesDir)) {
132
+ if (!seen.has('prunelle'))
133
+ addPackageDoc(docs, seen, root, join(nodeModulesDir, 'prunelle'), 'prunelle')
134
+ scanScopedPackages(docs, seen, root, nodeModulesDir)
135
+ }
136
+ const parent = dirname(dir)
137
+ if (parent === dir)
138
+ break
139
+ dir = parent
140
+ }
141
+
142
+ if (!seen.has('prunelle'))
143
+ addPackageDoc(docs, seen, root, resolve(import.meta.dir, '../..'), 'prunelle')
144
+
145
+ docs.sort((a, b) => {
146
+ if (a.packageName === 'prunelle' && b.packageName !== 'prunelle')
147
+ return -1
148
+ if (b.packageName === 'prunelle' && a.packageName !== 'prunelle')
149
+ return 1
150
+ return a.packageName.localeCompare(b.packageName)
151
+ })
152
+ return docs
153
+ }
154
+
155
+ function splitHeadingBlocks(content: string): HeadingBlock[] {
156
+ const rawLines = content.split(/\r?\n/)
157
+ const blocks: HeadingBlock[] = []
158
+ let current: HeadingBlock | undefined
159
+ let inFence = false
160
+
161
+ for (let i = 0; i < rawLines.length; i++) {
162
+ const text = rawLines[i]!
163
+ const num = i + 1
164
+ if (FENCE_RE.test(text))
165
+ inFence = !inFence
166
+
167
+ if (!inFence) {
168
+ const headingMatch = HEADING_RE.exec(text)
169
+ if (headingMatch !== null) {
170
+ current = { heading: headingMatch[2]!.trim(), line: num, lines: [] }
171
+ blocks.push(current)
172
+ continue
173
+ }
174
+ }
175
+
176
+ if (current === undefined) {
177
+ current = { heading: 'Overview', line: num, lines: [] }
178
+ blocks.push(current)
179
+ }
180
+ current.lines.push({ num, text })
181
+ }
182
+
183
+ return blocks
184
+ }
185
+
186
+ function flushLines(heading: string, line: number, lines: readonly { num: number, text: string }[], out: ParsedSection[]): void {
187
+ const text = lines.map(l => l.text).join('\n').trim()
188
+ if (text.length > 0)
189
+ out.push({ heading, line, content: text })
190
+ }
191
+
192
+ function splitBlockSections(block: HeadingBlock): ParsedSection[] {
193
+ const hasBoldBullet = (() => {
194
+ let inFence = false
195
+ for (const l of block.lines) {
196
+ if (FENCE_RE.test(l.text))
197
+ inFence = !inFence
198
+ else if (!inFence && BOLD_BULLET_RE.test(l.text))
199
+ return true
200
+ }
201
+ return false
202
+ })()
203
+
204
+ if (!hasBoldBullet) {
205
+ const out: ParsedSection[] = []
206
+ flushLines(block.heading, block.line, block.lines, out)
207
+ return out
208
+ }
209
+
210
+ const sections: ParsedSection[] = []
211
+ let activeBullet: { heading: string, line: number, lines: { num: number, text: string }[] } | undefined
212
+ let generalLines: { num: number, text: string }[] = []
213
+ let generalLine = block.line
214
+ let inFence = false
215
+
216
+ const flushActiveBullet = (): void => {
217
+ if (activeBullet !== undefined) {
218
+ flushLines(activeBullet.heading, activeBullet.line, activeBullet.lines, sections)
219
+ activeBullet = undefined
220
+ }
221
+ }
222
+
223
+ const flushGeneral = (): void => {
224
+ if (generalLines.length > 0) {
225
+ flushLines(block.heading, generalLine, generalLines, sections)
226
+ generalLines = []
227
+ }
228
+ }
229
+
230
+ for (const entry of block.lines) {
231
+ if (FENCE_RE.test(entry.text))
232
+ inFence = !inFence
233
+
234
+ if (!inFence) {
235
+ const bulletMatch = BOLD_BULLET_RE.exec(entry.text)
236
+ if (bulletMatch !== null) {
237
+ flushGeneral()
238
+ flushActiveBullet()
239
+ activeBullet = {
240
+ heading: `${block.heading} > ${bulletMatch[1]!.trim()}`,
241
+ line: entry.num,
242
+ lines: [entry],
243
+ }
244
+ continue
245
+ }
246
+
247
+ if (activeBullet !== undefined) {
248
+ if (entry.text.trim() === '' || entry.text.startsWith(' ') || entry.text.startsWith('\t')) {
249
+ activeBullet.lines.push(entry)
250
+ continue
251
+ }
252
+ flushActiveBullet()
253
+ }
254
+ }
255
+ else if (activeBullet !== undefined) {
256
+ activeBullet.lines.push(entry)
257
+ continue
258
+ }
259
+
260
+ if (generalLines.length === 0)
261
+ generalLine = entry.num
262
+ generalLines.push(entry)
263
+ }
264
+
265
+ flushActiveBullet()
266
+ flushGeneral()
267
+ return sections
268
+ }
269
+
270
+ function parseSections(content: string): ParsedSection[] {
271
+ const blocks = splitHeadingBlocks(content)
272
+ const sections: ParsedSection[] = []
273
+ for (const block of blocks) {
274
+ for (const section of splitBlockSections(block))
275
+ sections.push(section)
276
+ }
277
+ return sections
278
+ }
279
+
280
+ function countOccurrences(haystack: string, needle: string, maxCount: number): number {
281
+ let count = 0
282
+ let pos = 0
283
+ while (count < maxCount) {
284
+ const idx = haystack.indexOf(needle, pos)
285
+ if (idx === -1)
286
+ break
287
+ count++
288
+ pos = idx + needle.length
289
+ }
290
+ return count
291
+ }
292
+
293
+ function escapeRegExp(text: string): string {
294
+ return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
295
+ }
296
+
297
+ function scoreSection(
298
+ pkgLower: string,
299
+ headingLower: string,
300
+ contentLower: string,
301
+ words: readonly string[],
302
+ phrase: string,
303
+ ): number {
304
+ let score = 0
305
+ if (headingLower.includes(phrase))
306
+ score += 30
307
+ if (pkgLower.includes(phrase))
308
+ score += 25
309
+ if (contentLower.includes(phrase))
310
+ score += 10
311
+
312
+ for (const word of words) {
313
+ const wordBoundary = new RegExp(`(?:^|[^a-z0-9])${escapeRegExp(word)}(?:$|[^a-z0-9])`)
314
+ if (pkgLower.includes(word))
315
+ score += wordBoundary.test(pkgLower) ? 25 : 15
316
+ if (headingLower.includes(word))
317
+ score += wordBoundary.test(headingLower) ? 25 : 20
318
+ const bodyHits = countOccurrences(contentLower, word, 10)
319
+ score += bodyHits * 2
320
+ if (bodyHits > 0 && wordBoundary.test(contentLower))
321
+ score += 3
322
+ }
323
+
324
+ return score
325
+ }
326
+
327
+ /** Searches installed `llms.txt` docs for every whitespace-separated word in `query` (case-insensitive AND). */
328
+ export function searchLlmsDocs(
329
+ rootDir: string,
330
+ query: string,
331
+ options?: { limit?: number | undefined },
332
+ ): DocsSearchResult {
333
+ const limit = options?.limit ?? DEFAULT_LIMIT
334
+ if (!Number.isInteger(limit) || limit <= 0)
335
+ throw new Error(`prunelle docs: invalid limit ${limit}: give a positive integer`)
336
+ const docs = discoverLlmsDocs(rootDir)
337
+ const packages = docs.map(doc => ({ name: doc.packageName, version: doc.version, file: doc.relFile }))
338
+ const words = query.trim().toLowerCase().split(WORD_SPLIT_RE).filter(Boolean)
339
+ if (words.length === 0)
340
+ return { query, words: [], packages, matches: [] }
341
+
342
+ const phrase = words.join(' ')
343
+ const matches: DocsMatch[] = []
344
+
345
+ for (const doc of docs) {
346
+ const pkgLower = doc.packageName.toLowerCase()
347
+ for (const section of parseSections(doc.content)) {
348
+ const headingLower = section.heading.toLowerCase()
349
+ const contentLower = section.content.toLowerCase()
350
+ const combined = `${pkgLower} ${headingLower} ${contentLower}`
351
+ if (!words.every(word => combined.includes(word)))
352
+ continue
353
+
354
+ const score = scoreSection(pkgLower, headingLower, contentLower, words, phrase)
355
+ matches.push({
356
+ packageName: doc.packageName,
357
+ version: doc.version,
358
+ file: doc.file,
359
+ relFile: doc.relFile,
360
+ heading: section.heading,
361
+ line: section.line,
362
+ score,
363
+ content: section.content,
364
+ })
365
+ }
366
+ }
367
+
368
+ matches.sort((a, b) => {
369
+ if (b.score !== a.score)
370
+ return b.score - a.score
371
+ const aCore = a.packageName === 'prunelle' ? 0 : 1
372
+ const bCore = b.packageName === 'prunelle' ? 0 : 1
373
+ if (aCore !== bCore)
374
+ return aCore - bCore
375
+ const byPkg = a.packageName.localeCompare(b.packageName)
376
+ if (byPkg !== 0)
377
+ return byPkg
378
+ return a.line - b.line
379
+ })
380
+
381
+ return { query, words, packages, matches: matches.slice(0, limit) }
382
+ }
383
+
384
+ function formatPackageLabel(name: string, version: string | undefined): string {
385
+ return version !== undefined ? `${name}@${version}` : name
386
+ }
387
+
388
+ /** Formats a `DocsSearchResult` for terminal output. */
389
+ export function formatDocsSearchResult(result: DocsSearchResult): string {
390
+ if (result.matches.length === 0) {
391
+ const pkgList = result.packages.length > 0
392
+ ? result.packages.map(pkg => `${formatPackageLabel(pkg.name, pkg.version)} (${pkg.file})`).join(', ')
393
+ : '(none)'
394
+ if (result.words.length === 0)
395
+ return `No search words provided.\nAvailable packages (${result.packages.length}): ${pkgList}`
396
+ return `No docs matched "${result.query}".\nSearched packages (${result.packages.length}): ${pkgList}`
397
+ }
398
+
399
+ return result.matches.map((match) => {
400
+ const pkgLabel = formatPackageLabel(match.packageName, match.version)
401
+ return `=== ${pkgLabel} — ${match.heading} (${match.relFile}:${match.line}) ===\n${match.content}`
402
+ }).join('\n\n')
403
+ }
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env bun
2
+ // The prunelle CLI: `prunelle new`, `prunelle check`, `prunelle dev`, `prunelle build`, `prunelle start`, `prunelle upgrade`, plus plugin-registered commands
3
+ // (e.g. `prunelle ui-eject <grammar> [dest]`, `prunelle mcp-footprint`). Parsing, help, typo hints, completion and exit codes come from @kaynooo/cli-kit.
4
+
5
+ import type { Cli, Commands } from '@kaynooo/cli-kit'
6
+ import { Exit, parse, runCli as runKit } from '@kaynooo/cli-kit'
7
+ import { loadConfig } from '../plugins/index'
8
+ import { builtinCommands, takeRoot } from './commands'
9
+
10
+ export interface RunCliOptions {
11
+ cwd?: string | undefined
12
+ }
13
+
14
+ // The plugin commands of the app's prunelle.config.ts, read only when the first word names no built-in command (or for --help and completion).
15
+ function pluginCommands(cwd: string): (words: string[]) => Promise<Commands> {
16
+ return async (words) => {
17
+ const help = words.includes('--help') || words.includes('-h')
18
+ let config: Awaited<ReturnType<typeof loadConfig>>
19
+ try {
20
+ config = await loadConfig(takeRoot(words.filter(word => word !== '--help' && word !== '-h'), cwd).root)
21
+ }
22
+ catch (error) {
23
+ // `prunelle --help` and completion still list the built-ins; the command typed names why its config did not load.
24
+ const name = words[0]
25
+ if (help || name === undefined || name.startsWith('-'))
26
+ return {}
27
+ const reason = error instanceof Exit ? error : new Exit(error instanceof Error ? error.message : String(error), 1)
28
+ return { [name]: { summary: '', hidden: true, run: async () => { throw reason } } }
29
+ }
30
+ const commands: Commands = {}
31
+ for (const plugin of config.plugins ?? []) {
32
+ for (const [name, run] of Object.entries(plugin.commands ?? {})) {
33
+ commands[name] = {
34
+ summary: `command of the ${plugin.name} plugin`,
35
+ run: async (args) => {
36
+ // Only --help is the kit's to read: the plugin parses its own arguments.
37
+ parse(args.filter(arg => arg === '--help' || arg === '-h'), `prunelle ${name} [args...]: the ${name} command of the ${plugin.name} plugin`, {
38
+ root: { type: 'string', description: 'the app directory (default: the current one)' },
39
+ })
40
+ const taken = takeRoot(args, cwd)
41
+ await run(taken.rest, taken.root)
42
+ return 0
43
+ },
44
+ }
45
+ }
46
+ }
47
+ return commands
48
+ }
49
+ }
50
+
51
+ export function prunelleCli(cwd = process.cwd()): Cli {
52
+ return {
53
+ name: 'prunelle',
54
+ about: 'the Bun-native full-stack web framework',
55
+ commands: builtinCommands(cwd),
56
+ extraCommands: pluginCommands(cwd),
57
+ // A public framework writes no usage log into its users' home.
58
+ usage: false,
59
+ }
60
+ }
61
+
62
+ export function runCli(argv: readonly string[], options: RunCliOptions = {}): Promise<number> {
63
+ return runKit(prunelleCli(options.cwd), [...argv])
64
+ }
65
+
66
+ if (import.meta.main) {
67
+ const code = await runCli(Bun.argv.slice(2))
68
+ if (code !== 0)
69
+ process.exit(code)
70
+ }