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,372 @@
1
+ // The built-in `prunelle` commands, declared for @kaynooo/cli-kit: each parses its own arguments first (so `--help` and shell completion work), then loads what it runs.
2
+
3
+ import type { Commands, ExitRow } from '@kaynooo/cli-kit'
4
+ import type { ScaffoldTemplate } from './new'
5
+ import { resolve } from 'node:path'
6
+ import { badUsage, Exit, fail, parse } from '@kaynooo/cli-kit'
7
+ import { parseByteSize } from '../build/byte-size'
8
+
9
+ const TEMPLATES = ['minimal', 'fullstack', 'blog', 'app'] as const
10
+
11
+
12
+ const ROOT = { type: 'string', description: 'the app folder, when it is not the current one or the [dir] argument' } as const
13
+ const PORT = { type: 'string', description: 'the port to listen on (PORT, then prunelle.config.ts port, then 3000; 0 picks a free one)' } as const
14
+ const HOST = { type: 'string', optional: { bare: '0.0.0.0' }, description: 'the address to bind (HOST, then 127.0.0.1); bare, every interface' } as const
15
+
16
+ const OK: ExitRow = [0, 'it did what was asked']
17
+ const USAGE_EXIT: ExitRow = [2, 'a usage error']
18
+
19
+ // `--max-js 40kb`, `--max-js 40KB` or `--max-js 40960` (bytes).
20
+ function parseSize(raw: string): number {
21
+ return parseByteSize(raw) ?? badUsage(`invalid --max-js "${raw}": give the gzipped size of a route's eager JS in bytes or KB, e.g. --max-js 40kb`)
22
+ }
23
+
24
+ function parsePort(raw: string | number | undefined, fallback: number): number {
25
+ if (raw === undefined || raw === '')
26
+ return fallback
27
+ const n = Number(raw)
28
+ if (!Number.isInteger(n) || n < 0 || n > 65535)
29
+ return badUsage(`invalid port "${raw}": give an integer between 0 and 65535`)
30
+ return n
31
+ }
32
+
33
+ // A prunelle error carries its own `prunelle: ` or `prunelle <command>: ` prefix, which the kit prints already: strip it and exit 1.
34
+ async function failing<T>(command: string, run: () => Promise<T>): Promise<T> {
35
+ try {
36
+ return await run()
37
+ }
38
+ catch (error) {
39
+ if (error instanceof Exit || !(error instanceof Error))
40
+ throw error
41
+ const prefix = [`prunelle ${command}: `, 'prunelle: '].find(each => error.message.startsWith(each))
42
+ if (prefix === undefined)
43
+ throw error
44
+ return fail(error.message.slice(prefix.length), 1)
45
+ }
46
+ }
47
+
48
+ // The app folder: `--root`, else the `[dir]` argument, else where prunelle runs.
49
+ function rootOf(cwd: string, root: string | undefined, dir: string | undefined): string {
50
+ return resolve(cwd, root ?? dir ?? '.')
51
+ }
52
+
53
+ function newCommand(cwd: string): Commands[string] {
54
+ return {
55
+ summary: 'create an app from a template',
56
+ run: async (args) => {
57
+ const { values, positionals: [dir] } = parse(args, 'prunelle new [options] <dir>: creates an app from a template', {
58
+ template: { type: 'string', choices: TEMPLATES, description: 'the template (minimal)' },
59
+ force: { type: 'boolean', description: 'write into a folder that is not empty' },
60
+ root: ROOT,
61
+ }, { positionals: ['<dir>'], exits: [OK, [1, 'the folder is not empty'], USAGE_EXIT] })
62
+ const { scaffoldProject } = await import('./new')
63
+ const template = (values.template ?? 'minimal') as ScaffoldTemplate
64
+ const res = await failing('new', async () => scaffoldProject(rootOf(cwd, values.root, dir), { template, force: values.force === true }))
65
+
66
+ for (const file of res.overwritten)
67
+ console.log(`prunelle new: overwrote ${file}`)
68
+ console.log(`Created Prunelle (${template}) project in ${res.root} (${res.files.length} files)`)
69
+ console.log(`\nNext steps:\n cd ${dir}\n bun install\n bun run dev`)
70
+ return 0
71
+ },
72
+ }
73
+ }
74
+
75
+ function checkCommand(cwd: string): Commands[string] {
76
+ return {
77
+ summary: 'report every mistake prunelle can find in the app',
78
+ run: async (args) => {
79
+ const { values, positionals: [dir] } = parse(args, 'prunelle check [options] [dir]: reports every mistake prunelle can find in the app: the compile, the config, the routes and the types', {
80
+ 'no-typecheck': { type: 'boolean', description: 'skip the TypeScript check' },
81
+ 'json': { type: 'boolean', description: 'print { ok, checkedFiles, typecheck, errors, warnings } as JSON, each diagnostic with file, line, col, severity and message' },
82
+ 'strict': { type: 'boolean', description: 'fail on a warning too' },
83
+ 'root': ROOT,
84
+ }, { positionals: ['[dir]'], exits: [[0, 'no error found (warnings count only with --strict)'], [1, 'at least one error found, or a warning with --strict'], USAGE_EXIT] })
85
+ const { checkProject, diagnosticData } = await import('./check')
86
+ const t0 = performance.now()
87
+ const res = await checkProject(rootOf(cwd, values.root, dir), { skipTypecheck: values['no-typecheck'] === true })
88
+ const ms = (performance.now() - t0).toFixed(1)
89
+ const failed = !res.ok || (values.strict === true && res.warnings.length > 0)
90
+ if (values.json === true) {
91
+ const errors = res.errors.map(e => diagnosticData(e, 'error'))
92
+ const warnings = res.warnings.map(w => diagnosticData(w, 'warning'))
93
+ console.log(JSON.stringify({ ok: !failed, checkedFiles: res.checkedFiles, typecheck: res.typecheck, errors, warnings }, null, 2))
94
+ return failed ? 1 : 0
95
+ }
96
+ for (const warning of res.warnings)
97
+ console.warn(warning)
98
+ for (const e of res.errors)
99
+ console.error(e)
100
+ const typecheck = res.typecheck.ran ? `typechecked with ${res.typecheck.tool}` : `no typecheck: ${res.typecheck.reason}`
101
+ if (failed) {
102
+ if (res.ok)
103
+ console.error(`prunelle check: ${res.warnings.length} warning${res.warnings.length === 1 ? '' : 's'}, which --strict counts as errors.`)
104
+ if (!res.typecheck.ran)
105
+ console.error(`prunelle check: ${typecheck}.`)
106
+ return 1
107
+ }
108
+ console.log(`prunelle check passed (${res.checkedFiles} files, ${typecheck}) in ${ms} ms`)
109
+ return 0
110
+ },
111
+ }
112
+ }
113
+
114
+ function upgradeCommand(cwd: string): Commands[string] {
115
+ return {
116
+ summary: 'move an app written for kay v2 onto prunelle',
117
+ run: async (args) => {
118
+ const { values, positionals: [dir] } = parse(args, 'prunelle upgrade [options] [dir]: moves an app written for kay v2 (@kayjs/kay) onto prunelle', {
119
+ 'no-install': { type: 'boolean', description: 'do not run bun install after rewriting package.json' },
120
+ 'root': ROOT,
121
+ }, { positionals: ['[dir]'], exits: [[0, 'every file was rewritten and nothing is left to do by hand'], [1, 'bun install failed, or steps are left to do by hand (printed)'], USAGE_EXIT] })
122
+ const root = rootOf(cwd, values.root, dir)
123
+ const { notAnApp, upgradeProject } = await import('./upgrade')
124
+ const refused = notAnApp(root)
125
+ if (refused !== undefined)
126
+ return fail(refused, 1, 'run prunelle upgrade in the app\'s folder, or pass that folder as [dir]')
127
+ const res = upgradeProject(root)
128
+ for (const file of res.changed)
129
+ console.log(` ${file}`)
130
+ console.log(`prunelle upgrade rewrote ${res.changed.length} file${res.changed.length === 1 ? '' : 's'}`)
131
+ // The dependencies moved: the first run for this app, which renames its cookies; a later run only fixes what an older prunelle left.
132
+ const moved = res.changed.some(file => file === 'package.json' || file.endsWith('/package.json'))
133
+ if (moved && values['no-install'] !== true) {
134
+ const install = Bun.spawnSync(['bun', 'install'], { cwd: root, stdout: 'inherit', stderr: 'inherit' })
135
+ if (install.exitCode !== 0)
136
+ return 1
137
+ }
138
+ if (moved)
139
+ console.log('Signed-in users sign in again and visitors answer the cookie banner again: prunelle names its cookies pru_session and pru_consent.')
140
+ for (const line of res.manual)
141
+ console.error(line)
142
+ if (res.manual.length > 0)
143
+ return 1
144
+ console.log('Next: prunelle check')
145
+ return 0
146
+ },
147
+ }
148
+ }
149
+
150
+ function inspectCommand(cwd: string): Commands[string] {
151
+ return {
152
+ summary: 'inspect routes, islands, actions and plugins without bundling',
153
+ run: async (args) => {
154
+ const { values, positionals: [dir] } = parse(args, 'prunelle inspect [options] [dir]: inspects routes, islands, actions and plugins statically without bundling', {
155
+ json: { type: 'boolean', description: 'print { routes, islands, actions, plugins } as JSON' },
156
+ root: ROOT,
157
+ }, { positionals: ['[dir]'], exits: [OK, [1, 'the config or a source file failed to load'], USAGE_EXIT] })
158
+ const { formatInspectReport, inspectProject } = await import('./inspect')
159
+ const report = await failing('inspect', () => inspectProject(rootOf(cwd, values.root, dir)))
160
+ console.log(values.json === true ? JSON.stringify(report, null, 2) : formatInspectReport(report))
161
+ return 0
162
+ },
163
+ }
164
+ }
165
+
166
+ function buildCommand(cwd: string): Commands[string] {
167
+ return {
168
+ summary: 'build the app into dist/',
169
+ run: async (args) => {
170
+ const { values, positionals: [dir] } = parse(args, 'prunelle build [options] [dir]: builds the app into dist/', {
171
+ 'static': { type: 'boolean', description: 'export plain HTML files instead of a server' },
172
+ 'auto-prerender': { type: 'boolean', description: 'prerender static pages that read nothing from the request or clock' },
173
+ 'compile': { type: 'string', optional: { bare: '' }, description: 'also produce one binary, at [out] when given' },
174
+ 'analyze': { type: 'boolean', description: 'list each island with why it is one' },
175
+ 'json': { type: 'boolean', description: 'with --analyze, print the bundle report as JSON' },
176
+ 'max-js': { type: 'string', description: 'fail a route whose eager JS gzips over this size (40kb, or bytes)' },
177
+ 'root': ROOT,
178
+ }, { positionals: ['[dir]'], exits: [[0, 'the build succeeded'], [1, 'the build failed, or a route is over --max-js'], USAGE_EXIT] })
179
+ if (values.json === true && values.analyze !== true)
180
+ return badUsage('--json requires --analyze: pass prunelle build --analyze --json')
181
+ const maxJs = values['max-js'] === undefined ? undefined : parseSize(values['max-js'])
182
+ const compile = values.compile === undefined ? undefined : values.compile === '' ? true : values.compile
183
+ const analyzeJson = values.analyze === true && values.json === true
184
+ const { build } = await import('../build/index')
185
+ const t0 = performance.now()
186
+ const autoPrerender = values['auto-prerender'] === true ? true : undefined
187
+ const manifest = await failing('build', async () => build({ root: rootOf(cwd, values.root, dir), static: values.static === true, autoPrerender, compile, analyze: values.analyze === true, analyzeJson, maxJs }))
188
+ const ms = (performance.now() - t0).toFixed(1)
189
+ if (!analyzeJson)
190
+ console.log(`prunelle build done in ${ms} ms (${manifest.routes.length} routes, ${manifest.assets.length} client assets, ${manifest.actions.length} actions)`)
191
+ return 0
192
+ },
193
+ }
194
+ }
195
+
196
+ function ecoCommand(cwd: string): Commands[string] {
197
+ return {
198
+ summary: 'judge the RGESN eco-design criteria prunelle can measure, and draft the declaration',
199
+ run: async (args) => {
200
+ const { values, positionals: [dir] } = parse(args, 'prunelle eco [options] [dir]: builds the app and judges the RGESN 2024 criteria prunelle can measure', {
201
+ declaration: { type: 'string', optional: { bare: '' }, description: 'write an editable RGESN 2024 eco-design declaration page (default: src/routes/ecoconception.tsx)' },
202
+ service: { type: 'string', description: 'the service\'s name, written into --declaration' },
203
+ org: { type: 'string', description: 'the entity running the evaluation, written into --declaration' },
204
+ contact: { type: 'string', description: 'the contact email, written into --declaration' },
205
+ root: ROOT,
206
+ }, { positionals: ['[dir]'], exits: [OK, [1, 'the build failed'], USAGE_EXIT] })
207
+ const { formatEcoResult, runEco } = await import('../eco/index')
208
+ const result = await failing('eco', () => runEco({ root: rootOf(cwd, values.root, dir), declaration: values.declaration, service: values.service, organization: values.org, contactEmail: values.contact }))
209
+ console.log(formatEcoResult(result))
210
+ return 0
211
+ },
212
+ }
213
+ }
214
+
215
+ const SERVE_EXITS: ExitRow[] = [[0, 'the server started'], [1, 'the config or the server failed to load'], USAGE_EXIT]
216
+
217
+ function startCommand(cwd: string): Commands[string] {
218
+ return {
219
+ summary: 'serve the built app',
220
+ run: async (args) => {
221
+ const { values, positionals: [dir] } = parse(args, 'prunelle start [options] [dir]: serves the built app', { port: PORT, host: HOST, root: ROOT }, { positionals: ['[dir]'], exits: SERVE_EXITS })
222
+ const root = rootOf(cwd, values.root, dir)
223
+ process.env.NODE_ENV ??= 'production'
224
+ const { loadConfig } = await import('../plugins/index')
225
+ const config = await failing('start', async () => loadConfig(root))
226
+ const port = parsePort(values.port ?? (process.env.PORT || config.port), 3000)
227
+ const hostname = values.host ?? (process.env.HOST || '127.0.0.1')
228
+ const { startServer } = await import('../server/start')
229
+ const { handleSignals } = await import('../server/lifecycle')
230
+ const { app, server } = await failing('start', async () => startServer({ root, port, hostname }))
231
+ handleSignals(app, server)
232
+ console.log(`prunelle server listening on http://${hostname}:${server.port}`)
233
+ const { loopbackBindNotice } = await import('../server/security')
234
+ const notice = process.env.NODE_ENV === 'production' ? loopbackBindNotice(hostname, values.host !== undefined || Boolean(process.env.HOST)) : undefined
235
+ if (notice !== undefined)
236
+ console.warn(notice)
237
+ return 0
238
+ },
239
+ }
240
+ }
241
+
242
+ function devCommand(cwd: string): Commands[string] {
243
+ return {
244
+ summary: 'serve the app with rebuild on change',
245
+ run: async (args) => {
246
+ const { values, positionals: [dir] } = parse(args, 'prunelle dev [options] [dir]: serves the app and rebuilds it when a file changes', { port: PORT, host: HOST, root: ROOT }, { positionals: ['[dir]'], exits: SERVE_EXITS })
247
+ const root = rootOf(cwd, values.root, dir)
248
+ const { loadConfig, resolveServerConfig } = await import('../plugins/index')
249
+ const config = await failing('dev', async () => loadConfig(root, { dev: true }))
250
+ const port = parsePort(values.port ?? (process.env.PORT || config.port), 3000)
251
+ const hostname = values.host ?? (process.env.HOST || '127.0.0.1')
252
+ const { devSupervisor } = await import('../dev/supervisor')
253
+ const dev = await failing('dev', async () => devSupervisor({ root, port, hostname, maxRequestBodySize: resolveServerConfig(config, {}).maxRequestBodySize, allowedOrigins: [config.site, ...config.trustedOrigins ?? []] }))
254
+ for (const signal of ['SIGINT', 'SIGTERM'] as const)
255
+ process.once(signal, () => void dev.stop().finally(() => process.exit(0)))
256
+ return 0
257
+ },
258
+ }
259
+ }
260
+
261
+ function docsCommand(cwd: string): Commands[string] {
262
+ return {
263
+ summary: 'search llms.txt across prunelle and installed plugins',
264
+ run: async (args) => {
265
+ const { values, positionals } = parse(args, 'prunelle docs [options] <words...>: searches llms.txt of prunelle and every installed @prunellejs/* package', {
266
+ json: { type: 'boolean', description: 'print { query, words, packages, matches } as JSON' },
267
+ limit: { type: 'string', description: 'maximum matching sections to print (default: 20)' },
268
+ root: ROOT,
269
+ }, { positionals: ['<words...>'], exits: [OK, [1, 'no matching section found'], USAGE_EXIT] })
270
+ const limit = values.limit === undefined ? undefined : Number(values.limit)
271
+ if (limit !== undefined && (!Number.isInteger(limit) || limit <= 0))
272
+ return badUsage(`invalid --limit "${values.limit}": give a positive integer`)
273
+ const query = positionals.join(' ').trim()
274
+ if (query === '')
275
+ return badUsage('give at least one search word, e.g. prunelle docs action')
276
+ const { formatDocsSearchResult, searchLlmsDocs } = await import('./docs')
277
+ const result = searchLlmsDocs(rootOf(cwd, values.root, undefined), query, { limit })
278
+ if (values.json === true) {
279
+ console.log(JSON.stringify(result, null, 2))
280
+ return result.matches.length > 0 ? 0 : 1
281
+ }
282
+ console.log(formatDocsSearchResult(result))
283
+ return result.matches.length > 0 ? 0 : 1
284
+ },
285
+ }
286
+ }
287
+
288
+ function renderCommand(cwd: string): Commands[string] {
289
+ return {
290
+ summary: 'render a URL in-process without binding a port',
291
+ run: async (args) => {
292
+ const { values, positionals: [url] } = parse(args, 'prunelle render [options] <url>: builds or reuses a test build and prints the response with no port bound', {
293
+ method: { type: 'string', short: 'X', description: 'HTTP method (default: GET, or POST when --body is passed)' },
294
+ body: { type: 'string', short: 'd', description: 'request body string' },
295
+ header: { type: 'string', short: 'H', multiple: true, description: 'request header as "Name: Value" (repeatable)' },
296
+ cookie: { type: 'string', short: 'b', multiple: true, description: 'request cookie as "name=value" (repeatable)' },
297
+ include: { type: 'boolean', short: 'i', description: 'include HTTP status line and response headers before the body' },
298
+ fresh: { type: 'boolean', description: 'force a fresh build instead of reusing the cached render build' },
299
+ root: ROOT,
300
+ }, { positionals: ['<url>'], exits: [[0, 'response status is 2xx or 3xx'], [1, 'response status is 4xx/5xx or the render failed'], USAGE_EXIT] })
301
+ const { renderUrl, writeRenderResult } = await import('./render')
302
+ const result = await failing('render', () => renderUrl({
303
+ root: rootOf(cwd, values.root, undefined),
304
+ url: url!,
305
+ method: values.method,
306
+ body: values.body,
307
+ headers: values.header,
308
+ cookies: values.cookie,
309
+ includeHeaders: values.include === true,
310
+ fresh: values.fresh === true,
311
+ }))
312
+ writeRenderResult(result, values.include === true)
313
+ return result.ok ? 0 : 1
314
+ },
315
+ }
316
+ }
317
+
318
+ function mcpCommand(cwd: string): Commands[string] {
319
+ return {
320
+ summary: 'run the Prunelle stdio MCP server for coding agents',
321
+ run: async (args) => {
322
+ const { values, positionals: [dir] } = parse(args, 'prunelle mcp [options] [dir]: runs a stdio MCP server exposing check, inspect, build analysis, render, docs, dev errors, and llms.txt resources', {
323
+ 'allow-write': { type: 'boolean', description: 'allow MCP tools that write build output to disk' },
324
+ 'root': ROOT,
325
+ }, { positionals: ['[dir]'], exits: [OK, [1, 'the MCP server exited with an error'], USAGE_EXIT] })
326
+ const { runPrunelleMcpStdio } = await import('./mcp')
327
+ return await failing('mcp', () => runPrunelleMcpStdio({
328
+ root: rootOf(cwd, values.root, dir),
329
+ allowWrite: values['allow-write'] === true,
330
+ }))
331
+ },
332
+ }
333
+ }
334
+
335
+ export function builtinCommands(cwd: string): Commands {
336
+ return {
337
+ new: newCommand(cwd),
338
+ check: checkCommand(cwd),
339
+ inspect: inspectCommand(cwd),
340
+ docs: docsCommand(cwd),
341
+ render: renderCommand(cwd),
342
+ mcp: mcpCommand(cwd),
343
+ dev: devCommand(cwd),
344
+ build: buildCommand(cwd),
345
+ eco: ecoCommand(cwd),
346
+ start: startCommand(cwd),
347
+ upgrade: upgradeCommand(cwd),
348
+ }
349
+ }
350
+
351
+ // The `--root <dir>` (or `--root=<dir>`) of a plugin command's arguments, and the arguments without it.
352
+ export function takeRoot(args: readonly string[], cwd: string): { root: string, rest: string[] } {
353
+ const rest: string[] = []
354
+ let root: string | undefined
355
+ for (let i = 0; i < args.length; i++) {
356
+ const arg = args[i]!
357
+ if (arg === '--root') {
358
+ const next = args[i + 1]
359
+ if (next === undefined || next.startsWith('-'))
360
+ return badUsage('--root requires a directory path')
361
+ root = next
362
+ i++
363
+ }
364
+ else if (arg.startsWith('--root=')) {
365
+ root = arg.slice('--root='.length)
366
+ }
367
+ else {
368
+ rest.push(arg)
369
+ }
370
+ }
371
+ return { root: resolve(cwd, root ?? '.'), rest }
372
+ }