create-thexjs-app 0.1.4 → 1.1.0

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 (138) hide show
  1. package/README.md +56 -985
  2. package/dist/index.js +265 -196
  3. package/package.json +5 -1
  4. package/templates/{basic → addons/auth}/src/api/auth/login.ts +1 -3
  5. package/templates/{basic → addons/auth}/src/lib/auth.ts +13 -9
  6. package/templates/{basic → addons/auth}/src/lib/migrations/001_create_sessions.sql +1 -3
  7. package/templates/addons/auth/src/pages/dashboard/index.tsx +39 -0
  8. package/templates/addons/auth/src/pages/login.tsx +72 -0
  9. package/templates/addons/content/content/hello-world.md +13 -0
  10. package/templates/addons/content/src/pages/blog.tsx +29 -0
  11. package/templates/{basic → addons/shadcn}/components.json +1 -1
  12. package/templates/addons/shadcn/src/components/ui/badge.tsx +17 -0
  13. package/templates/addons/shadcn/src/components/ui/button.tsx +36 -0
  14. package/templates/addons/shadcn/src/components/ui/card.tsx +33 -0
  15. package/templates/addons/shadcn/src/components/ui/input.tsx +18 -0
  16. package/templates/addons/tailwind/src/styles/globals.css +32 -0
  17. package/templates/base/src/pages/index.tsx +29 -0
  18. package/templates/base/src/styles/globals.css +22 -0
  19. package/templates/base/tsconfig.json +24 -0
  20. package/templates/basic/content/blog/hello.md +0 -8
  21. package/templates/basic/public/favicon.ico +0 -0
  22. package/templates/basic/src/actions/dashboard/greet.ts +0 -3
  23. package/templates/basic/src/api/placeholder/comments.ts +0 -17
  24. package/templates/basic/src/api/placeholder/posts.ts +0 -20
  25. package/templates/basic/src/api/placeholder/todos.ts +0 -21
  26. package/templates/basic/src/api/placeholder/users.ts +0 -12
  27. package/templates/basic/src/components/Head.tsx +0 -21
  28. package/templates/basic/src/components/Image.tsx +0 -27
  29. package/templates/basic/src/components/Link.tsx +0 -10
  30. package/templates/basic/src/components/ui/badge.tsx +0 -49
  31. package/templates/basic/src/components/ui/button.tsx +0 -58
  32. package/templates/basic/src/components/ui/card.tsx +0 -88
  33. package/templates/basic/src/components/ui/input.tsx +0 -20
  34. package/templates/basic/src/layouts/main.tsx +0 -38
  35. package/templates/basic/src/lib/placeholder-data.ts +0 -176
  36. package/templates/basic/src/pages/_404.tsx +0 -17
  37. package/templates/basic/src/pages/about.tsx +0 -22
  38. package/templates/basic/src/pages/blog.tsx +0 -41
  39. package/templates/basic/src/pages/dashboard/index.tsx +0 -84
  40. package/templates/basic/src/pages/index.tsx +0 -54
  41. package/templates/basic/src/pages/login.tsx +0 -92
  42. package/templates/basic/src/pages/placeholder.tsx +0 -203
  43. package/templates/basic/src/pages/posts/[id].tsx +0 -17
  44. package/templates/basic/src/styles/globals.css +0 -90
  45. package/templates/basic/src/x-routes.ts +0 -29
  46. package/templates/basic/tsconfig.json +0 -11
  47. package/templates/basic/x.config.ts +0 -10
  48. package/templates/blog/components.json +0 -20
  49. package/templates/blog/content/posts/getting-started.md +0 -38
  50. package/templates/blog/content/posts/hello-world.md +0 -21
  51. package/templates/blog/content/posts/tailwind-v4.md +0 -31
  52. package/templates/blog/public/favicon.ico +0 -0
  53. package/templates/blog/src/components/ui/avatar.tsx +0 -91
  54. package/templates/blog/src/components/ui/badge.tsx +0 -49
  55. package/templates/blog/src/components/ui/button.tsx +0 -58
  56. package/templates/blog/src/components/ui/card.tsx +0 -88
  57. package/templates/blog/src/components/ui/input.tsx +0 -20
  58. package/templates/blog/src/layouts/blog.tsx +0 -46
  59. package/templates/blog/src/layouts/main.tsx +0 -32
  60. package/templates/blog/src/lib/utils.ts +0 -5
  61. package/templates/blog/src/pages/about.tsx +0 -36
  62. package/templates/blog/src/pages/blog/[slug].tsx +0 -56
  63. package/templates/blog/src/pages/blog/index.tsx +0 -70
  64. package/templates/blog/src/pages/index.tsx +0 -73
  65. package/templates/blog/src/styles/globals.css +0 -90
  66. package/templates/blog/src/x-routes.ts +0 -20
  67. package/templates/blog/tsconfig.json +0 -5
  68. package/templates/blog/x.config.ts +0 -6
  69. package/templates/default/components.json +0 -19
  70. package/templates/default/src/layouts/main.tsx +0 -28
  71. package/templates/default/src/pages/index.tsx +0 -66
  72. package/templates/default/src/styles/globals.css +0 -55
  73. package/templates/default/src/x-routes.ts +0 -17
  74. package/templates/default/tsconfig.json +0 -5
  75. package/templates/default/x.config.ts +0 -6
  76. package/templates/landing/components.json +0 -20
  77. package/templates/landing/public/favicon.ico +0 -0
  78. package/templates/landing/src/components/code-block.tsx +0 -118
  79. package/templates/landing/src/components/ui/avatar.tsx +0 -91
  80. package/templates/landing/src/components/ui/badge.tsx +0 -49
  81. package/templates/landing/src/components/ui/button.tsx +0 -58
  82. package/templates/landing/src/components/ui/card.tsx +0 -88
  83. package/templates/landing/src/components/ui/input.tsx +0 -20
  84. package/templates/landing/src/layouts/main.tsx +0 -78
  85. package/templates/landing/src/lib/syntax.tsx +0 -366
  86. package/templates/landing/src/lib/utils.ts +0 -5
  87. package/templates/landing/src/pages/docs/_layout.tsx +0 -139
  88. package/templates/landing/src/pages/docs/api-routes.tsx +0 -113
  89. package/templates/landing/src/pages/docs/build-deploy.tsx +0 -124
  90. package/templates/landing/src/pages/docs/configuration.tsx +0 -121
  91. package/templates/landing/src/pages/docs/content-collections.tsx +0 -130
  92. package/templates/landing/src/pages/docs/data-layer.tsx +0 -142
  93. package/templates/landing/src/pages/docs/getting-started.tsx +0 -129
  94. package/templates/landing/src/pages/docs/index.tsx +0 -187
  95. package/templates/landing/src/pages/docs/installation.tsx +0 -143
  96. package/templates/landing/src/pages/docs/introduction.tsx +0 -114
  97. package/templates/landing/src/pages/docs/layouts.tsx +0 -114
  98. package/templates/landing/src/pages/docs/middleware.tsx +0 -124
  99. package/templates/landing/src/pages/docs/packages/cli.tsx +0 -133
  100. package/templates/landing/src/pages/docs/packages/core.tsx +0 -176
  101. package/templates/landing/src/pages/docs/packages/env.tsx +0 -128
  102. package/templates/landing/src/pages/docs/pages.tsx +0 -135
  103. package/templates/landing/src/pages/docs/routing.tsx +0 -123
  104. package/templates/landing/src/pages/docs/server-functions.tsx +0 -177
  105. package/templates/landing/src/pages/features.tsx +0 -129
  106. package/templates/landing/src/pages/index.tsx +0 -159
  107. package/templates/landing/src/styles/globals.css +0 -136
  108. package/templates/landing/src/x-routes.ts +0 -31
  109. package/templates/landing/tsconfig.json +0 -10
  110. package/templates/landing/x.config.ts +0 -2
  111. package/templates/saas/components.json +0 -20
  112. package/templates/saas/public/favicon.ico +0 -0
  113. package/templates/saas/src/actions/dashboard.ts +0 -3
  114. package/templates/saas/src/api/auth/login.ts +0 -31
  115. package/templates/saas/src/components/ui/avatar.tsx +0 -91
  116. package/templates/saas/src/components/ui/badge.tsx +0 -49
  117. package/templates/saas/src/components/ui/button.tsx +0 -58
  118. package/templates/saas/src/components/ui/card.tsx +0 -88
  119. package/templates/saas/src/components/ui/input.tsx +0 -20
  120. package/templates/saas/src/layouts/dashboard.tsx +0 -67
  121. package/templates/saas/src/layouts/main.tsx +0 -38
  122. package/templates/saas/src/lib/auth.ts +0 -91
  123. package/templates/saas/src/lib/migrations/001_create_sessions.sql +0 -9
  124. package/templates/saas/src/lib/utils.ts +0 -5
  125. package/templates/saas/src/pages/dashboard/_middleware.ts +0 -9
  126. package/templates/saas/src/pages/dashboard/analytics.tsx +0 -68
  127. package/templates/saas/src/pages/dashboard/index.tsx +0 -86
  128. package/templates/saas/src/pages/dashboard/settings.tsx +0 -111
  129. package/templates/saas/src/pages/index.tsx +0 -110
  130. package/templates/saas/src/pages/login.tsx +0 -140
  131. package/templates/saas/src/pages/pricing.tsx +0 -121
  132. package/templates/saas/src/styles/globals.css +0 -90
  133. package/templates/saas/src/x-routes.ts +0 -22
  134. package/templates/saas/tsconfig.json +0 -5
  135. package/templates/saas/x.config.ts +0 -7
  136. /package/templates/{basic → addons/auth}/src/api/auth/logout.ts +0 -0
  137. /package/templates/{basic → addons/auth}/src/pages/dashboard/_middleware.ts +0 -0
  138. /package/templates/{basic → addons/shadcn}/src/lib/utils.ts +0 -0
package/README.md CHANGED
@@ -1,54 +1,34 @@
1
1
  <p align="center">
2
- <a href="#">
3
- <img src="https://github.com/abdelkabirouadoukou/x/raw/main/examples/landing/public/favicon.ico" alt="x framework logo" width="120" height="120">
4
- </a>
2
+ <img src="https://github.com/abdelkabirouadoukou/x/raw/main/examples/landing/public/favicon.ico" alt="x framework logo" width="96" height="96">
5
3
  </p>
6
4
 
7
- <p align="center">
8
-
9
- <strong>The ultra-fast, full-stack React framework built natively for Bun.</strong>
10
- Static sites, SSR, file-based API routes, and server functions — running in a single process.
11
-
12
- </p>
5
+ <h1 align="center">create-thexjs-app</h1>
13
6
 
14
7
  <p align="center">
15
- <a href="#quick-start"><img src="https://img.shields.io/badge/Bun-1.0+-black?style=flat-square&logo=bun" alt="Bun Ready"></a>
16
- <a href="#features"><img src="https://img.shields.io/badge/React-19-blue?style=flat-square&logo=react" alt="React 19"></a>
17
- <a href="#features"><img src="https://img.shields.io/badge/TypeScript-Strict-blue?style=flat-square&logo=typescript" alt="TypeScript"></a>
18
- <a href="./TASKS.md"><img src="https://img.shields.io/badge/Status-Active_Development-brightgreen?style=flat-square" alt="Status"></a>
8
+ Scaffold a new <strong>x</strong> app with interactive feature selection — a
9
+ single universal template, styled like <code>create-next-app</code>.
19
10
  </p>
20
11
 
21
12
  <p align="center">
22
- <a href="#getting-started">Getting Started</a> •
23
- <a href="#routing">Routing</a> •
24
- <a href="#pages--loaders">Pages</a> •
25
- <a href="#layouts">Layouts</a> •
26
- <a href="#api-routes">API Routes</a> •
27
- <a href="#server-functions">Server Functions</a> •
28
- <a href="#content-collections">Content</a> •
29
- <a href="#middleware">Middleware</a> •
30
- <a href="#data-layer">Data</a> •
31
- <a href="#build--deploy">Deploy</a>
13
+ <a href="#usage">Usage</a> •
14
+ <a href="#features">Features</a> •
15
+ <a href="#cli-flags">CLI flags</a> •
16
+ <a href="#development">Development</a>
32
17
  </p>
33
18
 
34
- ## Layout
19
+ ## Usage
35
20
 
36
- ```
37
- packages/core framework runtime: file-based router, SSR renderer, middleware
38
- packages/cli x dev / x build / x start
39
- packages/env type-safe environment variable validation
40
- examples/basic basic app exercising pages, API routes, server functions
41
- examples/landing full marketing site with docs, blog (x's own docs site)
42
- examples/blog blog example with content collections
43
- ```
44
-
45
- ## Getting Started
21
+ Requires [Bun](https://bun.sh). Run the scaffolder with:
46
22
 
47
- Create a new x project, learn the project structure, and build your first page.
23
+ ```bash
24
+ bun create thexjs-app@latest
25
+ ```
48
26
 
49
- ### Installation
27
+ You'll be guided through interactive prompts:
50
28
 
51
- The fastest way to start is with the project scaffolder. Make sure you have [Bun](https://bun.sh) installed, then run:
29
+ - **Project name** — e.g. `my-app`
30
+ - **Features** — choose from Tailwind CSS, shadcn/ui, Auth, and Content collections
31
+ - **Install & git** — confirm dependency install and `git init`
52
32
 
53
33
  ```bash
54
34
  bun create thexjs-app@latest my-app
@@ -56,973 +36,64 @@ cd my-app
56
36
  bun run dev
57
37
  ```
58
38
 
59
- When prompted for a template, press Enter to accept **default** (recommended) — a single home page to start from.
60
-
61
- Your app will be running at `http://localhost:3000`.
62
-
63
- ### Manual setup
64
-
65
- If you prefer to set up manually, create a directory and add x:
66
-
67
- ```bash
68
- mkdir my-app && cd my-app
69
- bun init -y
70
- bun add @thexjs/core
71
- cat << EOF > x.config.ts
72
- import { defineConfig } from "@thexjs/core";
73
- export default defineConfig({
74
- pagesDir: "src/pages",
75
- });
76
- EOF
77
- ```
78
-
79
- ### Project structure
80
-
81
- A typical x project looks like this:
82
-
83
- ```
84
- my-app/
85
- x.config.ts
86
- src/
87
- pages/ # File-based routes
88
- index.tsx
89
- about.tsx
90
- _404.tsx
91
- blog/
92
- [slug].tsx
93
- layouts/ # Nested layouts
94
- main.tsx
95
- api/ # API routes
96
- hello.ts
97
- actions/ # Server functions
98
- greet.ts
99
- content/ # Markdown content
100
- posts/
101
- hello-world.md
102
- ```
103
-
104
- ### Your first page
105
-
106
- Create `src/pages/index.tsx` with a simple component:
107
-
108
- ```tsx
109
- export default function Home() {
110
- return (
111
- <div>
112
- <h1 className="text-4xl font-bold">Hello x!</h1>
113
- <p className="text-muted-foreground">Welcome to your new app.</p>
114
- </div>
115
- );
116
- }
117
- ```
118
-
119
- ### Running the dev server
120
-
121
- Start the development server with hot reload:
122
-
123
- ```bash
124
- x dev
125
- [x] resolving routes...
126
- [x] found 3 routes in 12ms
127
- [x] dev server running at http://localhost:3000
128
- ```
129
-
130
- The dev server watches your `src/` directory and automatically reloads when files change.
131
-
132
- ---
133
-
134
- ## Routing
135
-
136
- x uses the file system as your route table. Drop a file in `src/pages/`, get a route.
137
-
138
- ### How it works
139
-
140
- Every `.tsx` file in your pages directory becomes a route. The file path determines the URL pattern.
141
-
142
- ```
143
- pages/index.tsx -> /
144
- pages/about.tsx -> /about
145
- pages/contact.tsx -> /contact
146
- pages/blog/index.tsx -> /blog
147
- pages/blog/[slug].tsx -> /blog/:slug
148
- pages/dashboard/
149
- settings.tsx -> /dashboard/settings
150
- profile.tsx -> /dashboard/profile
151
- pages/_404.tsx -> catch-all 404
152
- ```
153
-
154
- ### Static routes
155
-
156
- Simple files map to exact URL paths. `pages/about.tsx` becomes `/about`.
157
-
158
- ```tsx
159
- export default function About() {
160
- return <h1 className="text-3xl font-bold">About us</h1>;
161
- }
162
- ```
163
-
164
- ### Dynamic segments
165
-
166
- Wrap a filename in square brackets to create a dynamic segment. The value is available via the `params` object in loaders.
167
-
168
- ```tsx
169
- import type { RouteProps, LoaderArgs } from "@thexjs/core";
170
-
171
- export async function loader({ params }: LoaderArgs) {
172
- const post = await getPost(params.slug);
173
- return { title: post.title, content: post.content };
174
- }
175
-
176
- export default function BlogPost({ loaderData }: RouteProps<typeof loader>) {
177
- return (
178
- <article>
179
- <h1 className="text-3xl font-bold">{loaderData.title}</h1>
180
- <div>{loaderData.content}</div>
181
- </article>
182
- );
183
- }
184
- ```
185
-
186
- Multiple dynamic segments work too: `pages/product/[category]/[id].tsx` → `/product/:category/:id`.
187
-
188
- ### Nested routes with folders
189
-
190
- Organize routes in folders for nested URL structures. Each folder can have its own `index.tsx`.
191
-
192
- ```
193
- pages/dashboard/
194
- index.tsx -> /dashboard
195
- settings.tsx -> /dashboard/settings
196
- profile.tsx -> /dashboard/profile
197
- billing/
198
- index.tsx -> /dashboard/billing
199
- history.tsx -> /dashboard/billing/history
200
- ```
201
-
202
- ### Catch-all 404 page
203
-
204
- Create `pages/_404.tsx` to show a custom not-found page for unmatched routes.
205
-
206
- ```tsx
207
- export default function NotFound() {
208
- return (
209
- <div className="text-center py-20">
210
- <h1 className="text-6xl font-bold text-muted-foreground">404</h1>
211
- <p className="mt-4 text-lg text-muted-foreground">Page not found</p>
212
- <a href="/" className="mt-6 inline-block text-primary hover:underline">
213
- Go home
214
- </a>
215
- </div>
216
- );
217
- }
218
- ```
219
-
220
- ---
221
-
222
- ## Pages & Loaders
223
-
224
- x supports two page modes — static prerendering and server-side rendering — both powered by loaders.
225
-
226
- ### Page modes
227
-
228
- By default, pages are server-rendered (SSR). Export `mode = "static"` to prerender at build time.
229
-
230
- ### Static pages
231
-
232
- Static pages are rendered at build time and exported as HTML. Use this for marketing pages, blog posts, or any content that doesn't need per-request rendering.
233
-
234
- ```tsx
235
- import type { RouteProps } from "@thexjs/core";
236
-
237
- export const mode = "static";
238
-
239
- export default function About({}: RouteProps) {
240
- return (
241
- <div className="max-w-2xl mx-auto py-12">
242
- <h1 className="text-4xl font-bold">About</h1>
243
- <p className="mt-4 text-muted-foreground">
244
- This page is prerendered at build time.
245
- </p>
246
- </div>
247
- );
248
- }
249
- ```
250
-
251
- ### Server pages with loaders
252
-
253
- Server pages (the default) run a `loader` function on every request. The loader can fetch data, query a database, or call an external API.
254
-
255
- ```tsx
256
- import type { RouteProps, LoaderArgs } from "@thexjs/core";
257
-
258
- export async function loader({ request }: LoaderArgs) {
259
- const res = await fetch("https://api.example.com/products");
260
- const products = await res.json();
261
- return { products };
262
- }
263
-
264
- export default function Products({ loaderData }: RouteProps<typeof loader>) {
265
- return (
266
- <div>
267
- <h1 className="text-3xl font-bold">Products</h1>
268
- <ul className="mt-6 space-y-4">
269
- {loaderData.products.map((p: any) => (
270
- <li key={p.id} className="rounded-xl border border-border bg-card p-4">
271
- <h2 className="font-semibold">{p.name}</h2>
272
- <p className="text-sm text-muted-foreground">{p.price}</p>
273
- </li>
274
- ))}
275
- </ul>
276
- </div>
277
- );
278
- }
279
- ```
280
-
281
- ### Loader with dynamic params
282
-
283
- Combined with dynamic routing, loaders receive `params` parsed from the URL path.
284
-
285
- ```tsx
286
- import type { RouteProps, LoaderArgs } from "@thexjs/core";
287
-
288
- export async function loader({ params }: LoaderArgs) {
289
- const product = await db.query(
290
- "SELECT * FROM products WHERE id = ?", [params.id]
291
- );
292
- if (!product) throw new Response(null, { status: 404 });
293
- return { product };
294
- }
295
-
296
- export default function ProductDetail({ loaderData }: RouteProps<typeof loader>) {
297
- const { product } = loaderData;
298
- return (
299
- <div>
300
- <h1 className="text-3xl font-bold">{product.name}</h1>
301
- <p className="mt-2 text-muted-foreground">{product.description}</p>
302
- <p className="mt-4 text-2xl font-bold text-primary">${product.price}</p>
303
- </div>
304
- );
305
- }
306
- ```
307
-
308
- ### RouteProps type
309
-
310
- The `RouteProps` type provides typed access to `loaderData`, `params`, and `request`. Pass your loader function as the type parameter for full type safety.
311
-
312
- ```tsx
313
- import type { RouteProps } from "@thexjs/core";
314
-
315
- export default function Page({ loaderData, params, request }: RouteProps<typeof loader>) {
316
- // loaderData has the return type of loader()
317
- // params has the dynamic segment types
318
- // request is the standard Request object
319
- }
320
- ```
321
-
322
- ---
323
-
324
- ## Layouts
325
-
326
- Layouts wrap your pages with shared UI. x supports nested layouts via a dedicated layouts directory and the `_layout.tsx` convention.
327
-
328
- ### Layouts directory
329
-
330
- Configure a layouts directory in `x.config.ts`. Layouts follow the same file-tree hierarchy as pages.
331
-
332
- ```tsx
333
- import { defineConfig } from "@thexjs/core";
334
-
335
- export default defineConfig({
336
- pagesDir: "src/pages",
337
- layoutsDir: "src/layouts",
338
- });
339
- ```
340
-
341
- ### Root layout
342
-
343
- The root layout wraps every page in your app. Create `src/layouts/main.tsx` to add a header, footer, or global styling.
344
-
345
- ```tsx
346
- import type { ReactNode } from "react";
347
-
348
- export default function RootLayout({ children }: { children: ReactNode }) {
349
- return (
350
- <div className="flex min-h-screen flex-col bg-background">
351
- <header className="border-b border-border p-4">
352
- <a href="/" className="text-lg font-bold">My App</a>
353
- </header>
354
- <main className="flex-1">{children}</main>
355
- <footer className="border-t border-border p-4 text-center text-sm text-muted-foreground">
356
- &copy; 2026 My App
357
- </footer>
358
- </div>
359
- );
360
- }
361
- ```
362
-
363
- ### Nested layouts with _layout.tsx
364
-
365
- Place a `_layout.tsx` file inside a pages folder to create a nested layout. All pages in that folder (and subfolders) inherit it.
366
-
367
- ```
368
- pages/
369
- _layout.tsx -> root layout
370
- index.tsx
371
- blog/
372
- _layout.tsx -> nested layout for /blog/*
373
- index.tsx
374
- [slug].tsx
375
- ```
376
-
377
- A nested layout can add a sidebar, breadcrumbs, or section-specific navigation.
378
-
379
- ```tsx
380
- import type { ReactNode } from "react";
381
-
382
- export default function BlogLayout({ children }: { children: ReactNode }) {
383
- return (
384
- <div className="flex gap-8">
385
- <aside className="w-64 shrink-0">
386
- <nav className="space-y-2">
387
- <a href="/blog" className="block font-semibold">All posts</a>
388
- <a href="/blog/category/react" className="block text-muted-foreground hover:text-foreground">React</a>
389
- <a href="/blog/category/bun" className="block text-muted-foreground hover:text-foreground">Bun</a>
390
- </nav>
391
- </aside>
392
- <div className="min-w-0 flex-1">{children}</div>
393
- </div>
394
- );
395
- }
396
- ```
397
-
398
- ### Layout chain
399
-
400
- Layouts nest hierarchically. A page under `pages/blog/[slug].tsx` would be wrapped by `blog/_layout.tsx` and then the root layout. The chain is resolved automatically based on the page's file path.
401
-
402
- ---
403
-
404
- ## API Routes
405
-
406
- Build REST endpoints alongside your frontend pages. API routes live in `src/api/` and share the same process as your pages.
407
-
408
- ### File-based API routing
409
-
410
- Like pages, API routes use the file system. A file at `src/api/hello.ts` becomes `/api/hello`.
411
-
412
- ```tsx
413
- import type { ApiHandler } from "@thexjs/core";
414
-
415
- export const GET: ApiHandler = ({ request }) => {
416
- return Response.json({ message: "Hello from x!" });
417
- };
418
- ```
419
-
420
- ### Request & response
421
-
422
- Each exported HTTP method receives the request and returns a standard `Response` object. Dynamic segments work the same as pages: `api/users/[id].ts` → `/api/users/:id`.
423
-
424
- ```tsx
425
- import type { ApiHandler } from "@thexjs/core";
426
-
427
- export const GET: ApiHandler = async ({ request }) => {
428
- const users = await db.query("SELECT * FROM users");
429
- return Response.json(users);
430
- };
431
-
432
- export const POST: ApiHandler = async ({ request }) => {
433
- const body = await request.json();
434
- const result = await db.query(
435
- "INSERT INTO users (name, email) VALUES (?, ?) RETURNING *",
436
- [body.name, body.email]
437
- );
438
- return Response.json(result, { status: 201 });
439
- };
440
- ```
441
-
442
- ### POST endpoint example
443
-
444
- ```tsx
445
- import type { ApiHandler } from "@thexjs/core";
446
-
447
- export const POST: ApiHandler = async ({ request }) => {
448
- const form = await request.formData();
449
- const email = form.get("email");
450
- const message = form.get("message");
451
-
452
- if (!email || !message) {
453
- return Response.json(
454
- { error: "Email and message are required" },
455
- { status: 400 }
456
- );
457
- }
458
-
459
- await sendEmail({ email, message });
460
- return Response.json({ success: true });
461
- };
462
- ```
463
-
464
- ### API route tree
465
-
466
- API routes support the same file-tree conventions as pages — nested folders, dynamic segments, and index files.
467
-
468
- ```
469
- src/api/
470
- hello.ts -> GET /api/hello
471
- users.ts -> GET, POST /api/users
472
- users/
473
- [id].ts -> GET, PUT, DELETE /api/users/:id
474
- auth/
475
- login.ts -> POST /api/auth/login
476
- register.ts -> POST /api/auth/register
477
- ```
478
-
479
- ### Process sharing
480
-
481
- API routes run in the same Bun process as your pages and server functions. This means you can share database connections, in-memory caches, and configuration without any network overhead.
482
-
483
- ---
484
-
485
- ## Server Functions
486
-
487
- Call server-side functions directly from the browser without writing REST endpoints. Server functions live in `src/actions/` and are invoked via fetch.
488
-
489
- ### Defining server functions
490
-
491
- Create a file in `src/actions/` and export named async functions.
492
-
493
- ```ts
494
- export async function greet(name: string) {
495
- return `Hello, ${name}! The server time is ${new Date().toISOString()}.`;
496
- }
497
-
498
- export async function sendEmail({ to, subject, body }: {
499
- to: string;
500
- subject: string;
501
- body: string;
502
- }) {
503
- return { sent: true, to };
504
- }
505
- ```
506
-
507
- ### Calling from the browser
508
-
509
- Server functions are called by sending a POST request to `/__x/actions/<filename>/<functionName>`. The arguments are sent as JSON in the request body.
510
-
511
- ```tsx
512
- "use client";
513
-
514
- import { useState } from "react";
515
-
516
- export default function GreetForm() {
517
- const [message, setMessage] = useState("");
518
-
519
- async function handleSubmit(e: React.FormEvent) {
520
- e.preventDefault();
521
- const form = new FormData(e.target as HTMLFormElement);
522
- const name = form.get("name");
523
-
524
- const res = await fetch("/__x/actions/greet/greet", {
525
- method: "POST",
526
- headers: { "Content-Type": "application/json" },
527
- body: JSON.stringify([name]),
528
- });
529
-
530
- const data = await res.text();
531
- setMessage(data);
532
- }
533
-
534
- return (
535
- <form onSubmit={handleSubmit} className="space-y-4">
536
- <input
537
- name="name"
538
- placeholder="Enter your name"
539
- className="rounded-xl border border-border bg-card px-4 py-2"
540
- />
541
- <button type="submit" className="rounded-xl bg-primary px-4 py-2 text-primary-foreground">
542
- Greet me
543
- </button>
544
- {message && <p className="text-muted-foreground">{message}</p>}
545
- </form>
546
- );
547
- }
548
- ```
549
-
550
- ### Server functions from loaders
551
-
552
- You can also import and call server functions directly in loaders — no HTTP needed since they share the same process.
553
-
554
- ```tsx
555
- import type { RouteProps, LoaderArgs } from "@thexjs/core";
556
- import { getDashboardData } from "../actions/dashboard";
557
-
558
- export async function loader({ request }: LoaderArgs) {
559
- const data = await getDashboardData();
560
- return { data };
561
- }
562
-
563
- export default function Dashboard({ loaderData }: RouteProps<typeof loader>) {
564
- return <div>...</div>;
565
- }
566
- ```
567
-
568
- ---
569
-
570
- ## Content Collections
571
-
572
- Write content in Markdown with frontmatter, and x automatically turns it into pages. Perfect for blogs, documentation, and any content-driven site.
573
-
574
- ### Configuration
575
-
576
- Point the content directory in `x.config.ts` to a folder with your markdown files.
577
-
578
- ```tsx
579
- import { defineConfig } from "@thexjs/core";
580
-
581
- export default defineConfig({
582
- contentDir: "content",
583
- });
584
- ```
585
-
586
- ### Markdown with frontmatter
587
-
588
- Each markdown file starts with frontmatter (YAML between `---` delimiters) followed by markdown content.
589
-
590
- ```markdown
591
- ---
592
- title: Hello World
593
- date: 2026-03-15
594
- tags: [getting-started, tutorial]
595
- author: Jane Doe
596
- ---
597
-
598
- ## Welcome to x!
599
-
600
- This is your first post using x's content collection system.
601
-
602
- You can write **markdown** with all the usual syntax:
603
-
604
- - Lists
605
- - **Bold** and *italic* text
606
- - `inline code` and code blocks
607
-
608
- ```ts
609
- const greeting = "Hello from x!";
610
- console.log(greeting);
611
- ```
612
- ```
613
-
614
- ### Reading content in a loader
615
-
616
- Use `scanContent` to discover files and `renderMarkdown` to convert markdown to HTML in your loaders.
617
-
618
- ```tsx
619
- import type { RouteProps, LoaderArgs } from "@thexjs/core";
620
- import { scanContent, renderMarkdown } from "@thexjs/core";
621
-
622
- export async function loader({ params }: LoaderArgs) {
623
- const posts = await scanContent("posts");
624
- const post = posts.find((p) => p.slug === params.slug);
625
- if (!post) throw new Response(null, { status: 404 });
626
- const html = await renderMarkdown(post.body);
627
- return { post: { ...post, html } };
628
- }
629
-
630
- export default function BlogPost({ loaderData }: RouteProps<typeof loader>) {
631
- return (
632
- <article className="prose max-w-none">
633
- <h1 className="text-4xl font-bold">{loaderData.post.title}</h1>
634
- <p className="text-sm text-muted-foreground">
635
- {loaderData.post.date} &mdash; {loaderData.post.author}
636
- </p>
637
- <div className="mt-6 flex gap-2">
638
- {loaderData.post.tags?.map((tag: string) => (
639
- <span key={tag} className="rounded-full bg-primary/10 px-3 py-1 text-xs text-primary">
640
- {tag}
641
- </span>
642
- ))}
643
- </div>
644
- <div
645
- className="mt-8 leading-relaxed"
646
- dangerouslySetInnerHTML={{ __html: loaderData.post.html }}
647
- />
648
- </article>
649
- );
650
- }
651
- ```
652
-
653
- ### scanContent API
654
-
655
- `scanContent(directory)` scans a subdirectory of your content folder and returns an array of content entries. Each entry includes `slug`, `frontmatter`, and `content`.
656
-
657
- ### renderMarkdown API
658
-
659
- `renderMarkdown(markdownString)` converts markdown to an HTML string. It supports syntax highlighting via Shiki and handles all standard markdown features.
660
-
661
- ---
662
-
663
- ## Middleware
664
-
665
- Route-level middleware lets you intercept requests before they reach your page or API handler. Use it for authentication, redirects, logging, and validation.
666
-
667
- ### The _middleware.ts convention
668
-
669
- Place a `_middleware.ts` file in any route directory. It runs for all routes in that directory and its subdirectories.
670
-
671
- ```
672
- pages/
673
- _middleware.ts -> runs for all routes
674
- index.tsx
675
- dashboard/
676
- _middleware.ts -> runs only for /dashboard/*
677
- settings.tsx
678
- profile.tsx
679
- admin/
680
- _middleware.ts -> auth check for /admin/*
681
- index.tsx
682
- ```
683
-
684
- ### Middleware context
685
-
686
- A middleware function receives an object with `params` (dynamic route params), `request` (the original Request), and a `next` function to continue the chain.
687
-
688
- ```ts
689
- import type { MiddlewareContext, MiddlewareNext } from "@thexjs/core";
690
-
691
- export async function middleware(ctx: MiddlewareContext, next: MiddlewareNext) {
692
- console.log(`[${ctx.request.method}] ${ctx.request.url}`);
693
- return next(ctx);
694
- }
695
- ```
696
-
697
- ### Auth middleware example
698
-
699
- ```ts
700
- import type { MiddlewareContext, MiddlewareNext } from "@thexjs/core";
701
-
702
- export async function middleware(ctx: MiddlewareContext, next: MiddlewareNext) {
703
- const session = ctx.request.cookies.get("session");
704
-
705
- if (!session) {
706
- return new Response(null, {
707
- status: 302,
708
- headers: { Location: "/login" },
709
- });
710
- }
711
-
712
- const user = await validateSession(session);
713
- if (!user) {
714
- return new Response(null, {
715
- status: 302,
716
- headers: { Location: "/login" },
717
- });
718
- }
719
-
720
- ctx.params.user = user;
721
- return next(ctx);
722
- }
723
- ```
724
-
725
- ### MiddlewareNext
726
-
727
- Call `next(ctx)` to pass control to the next middleware or the route handler. You can modify `ctx.params` to enrich the request context for downstream handlers.
728
-
729
- ### Redirect patterns
730
-
731
- ```ts
732
- // Redirect to login
733
- return new Response(null, {
734
- status: 302,
735
- headers: { Location: "/login?redirect=" + ctx.request.url },
736
- });
737
-
738
- // Redirect back after successful auth
739
- const url = new URL(ctx.request.url);
740
- const redirectTo = url.searchParams.get("redirect") || "/";
741
- return new Response(null, {
742
- status: 302,
743
- headers: { Location: redirectTo },
744
- });
745
- ```
746
-
747
- ---
39
+ Your app runs at `http://localhost:3000` with hot reload.
748
40
 
749
- ## Data Layer
41
+ ## Features
750
42
 
751
- x provides built-in SQLite and PostgreSQL integrations. Connect to a database, run migrations, and query data directly from loaders and server functions.
43
+ The scaffolder ships a single lean base template. Every feature is optional and
44
+ selected at prompt time (or via CLI flags).
752
45
 
753
- ### SQLite
46
+ | Feature | Adds |
47
+ | ----------------- | ----------------------------------------------------------------------------------------------------- |
48
+ | **Tailwind CSS** | Utility-first styling + the x dev/build Tailwind pipeline |
49
+ | **shadcn/ui** | Accessible React components in `src/components/ui` (requires Tailwind) |
50
+ | **Auth** | SQLite-backed demo sessions, `/login` and a protected `/dashboard` with middleware |
51
+ | **Content** | Markdown/MDX with frontmatter in `content/`, auto-routed and indexed on `/blog` |
754
52
 
755
- Use `connectSQLite` to connect to a local SQLite database file. SQLite requires zero configuration and is perfect for development and single-server deployments.
53
+ Enabling **shadcn/ui** automatically enables **Tailwind CSS**.
756
54
 
757
- ```ts
758
- import { connectSQLite, runSQLiteMigrations } from "@thexjs/core";
55
+ A generated project always includes a complete `.gitignore`, a self-contained
56
+ `tsconfig.json`, and (by default) an initialized git repository.
759
57
 
760
- const db = connectSQLite("data/app.db");
58
+ ## CLI flags
761
59
 
762
- await runSQLiteMigrations(db, [
763
- {
764
- version: 1,
765
- sql: `
766
- CREATE TABLE IF NOT EXISTS users (
767
- id INTEGER PRIMARY KEY AUTOINCREMENT,
768
- name TEXT NOT NULL,
769
- email TEXT UNIQUE NOT NULL,
770
- created_at TEXT DEFAULT (datetime('now'))
771
- )
772
- `,
773
- },
774
- {
775
- version: 2,
776
- sql: `
777
- CREATE TABLE IF NOT EXISTS posts (
778
- id INTEGER PRIMARY KEY AUTOINCREMENT,
779
- user_id INTEGER REFERENCES users(id),
780
- title TEXT NOT NULL,
781
- body TEXT,
782
- created_at TEXT DEFAULT (datetime('now'))
783
- )
784
- `,
785
- },
786
- ]);
787
-
788
- export { db };
789
- ```
790
-
791
- ### Querying SQLite
792
-
793
- The database object supports prepared statements with `query` and `execute` methods.
794
-
795
- ```tsx
796
- import type { RouteProps, LoaderArgs } from "@thexjs/core";
797
- import { db } from "../lib/db";
798
-
799
- export async function loader({}: LoaderArgs) {
800
- const users = db.query(
801
- "SELECT id, name, email FROM users ORDER BY created_at DESC"
802
- ).all();
803
- return { users };
804
- }
805
-
806
- export default function Users({ loaderData }: RouteProps<typeof loader>) {
807
- return (
808
- <div>
809
- <h1 className="text-3xl font-bold">Users</h1>
810
- <ul className="mt-6 space-y-3">
811
- {loaderData.users.map((u: any) => (
812
- <li key={u.id} className="rounded-xl border border-border bg-card p-4">
813
- <p className="font-semibold">{u.name}</p>
814
- <p className="text-sm text-muted-foreground">{u.email}</p>
815
- </li>
816
- ))}
817
- </ul>
818
- </div>
819
- );
820
- }
821
- ```
822
-
823
- ### PostgreSQL
824
-
825
- For production deployments, use `connectPostgres` with a connection string. PostgreSQL provides concurrent access, connection pooling, and is suitable for multi-server deployments.
826
-
827
- ```ts
828
- import { connectPostgres, runPostgresMigrations } from "@thexjs/core";
829
-
830
- const db = connectPostgres({
831
- connectionString: process.env.DATABASE_URL,
832
- max: 20, // connection pool size
833
- });
834
-
835
- await runPostgresMigrations(db, [
836
- {
837
- version: 1,
838
- sql: `
839
- CREATE TABLE IF NOT EXISTS users (
840
- id SERIAL PRIMARY KEY,
841
- name VARCHAR(255) NOT NULL,
842
- email VARCHAR(255) UNIQUE NOT NULL,
843
- created_at TIMESTAMPTZ DEFAULT NOW()
844
- )
845
- `,
846
- },
847
- ]);
848
-
849
- export { db };
850
- ```
851
-
852
- ### Migration API
853
-
854
- Both `runSQLiteMigrations` and `runPostgresMigrations` take an array of migration objects. Each migration has a `version` number (incrementing) and `sql` string. Migrations are tracked and only run once.
855
-
856
- ---
857
-
858
- ## Build & Deploy
859
-
860
- x produces optimized production builds with static HTML export, a server entry point, and content collection rendering — all in a single command.
861
-
862
- ### Build command
863
-
864
- Run `x build` to produce a production build. The build output goes to a `.x/` directory.
865
-
866
- ```bash
867
- x build
868
- [x] resolving routes...
869
- [x] found 12 routes
870
- [x] building static pages...
871
- [x] building server bundle...
872
- [x] rendering content collections...
873
- [x] build complete in 1.2s
874
- ```
875
-
876
- ### Output structure
877
-
878
- The `.x/` directory contains everything needed to deploy — static files, server bundle, and assets.
879
-
880
- ```
881
- .x/
882
- client/ # Client-side assets
883
- assets/
884
- *.js # Bundled JS
885
- *.css # Extracted CSS
886
- server/ # Server entry point
887
- index.js # Bun server bundle
888
- static/ # Prerendered HTML pages
889
- index.html
890
- about/index.html
891
- blog/
892
- hello-world/index.html
893
- x.json # Build manifest
894
- ```
895
-
896
- ### Static page export
897
-
898
- Pages with `mode = "static"` are exported as HTML files in `.x/static/`. Each page is fully rendered at build time, including its loader output.
899
-
900
- ### Server entry
901
-
902
- Server-rendered pages are bundled into `.x/server/index.js`. This file contains all server routes, API handlers, server functions, and middleware.
903
-
904
- ### Production server
905
-
906
- Use `x start` to run the production server. It serves static files from `.x/static/` and handles dynamic routes via the server bundle.
60
+ Skips the interactive prompts — useful for scripting and CI:
907
61
 
908
62
  ```bash
909
- x start
910
- [x] production server running at http://localhost:3000
63
+ bun create thexjs-app@latest my-app --tailwind --auth --content --shadcn
911
64
  ```
912
65
 
913
- ### Docker deployment
914
-
915
- Deploy with a minimal Docker image using the official Bun runtime. The build output is self-contained.
916
-
917
- ```dockerfile
918
- FROM oven/bun:1 AS build
919
- WORKDIR /app
920
- COPY package.json bun.lock .
921
- RUN bun install
922
- COPY . .
923
- RUN x build
924
-
925
- FROM oven/bun:1-slim
926
- WORKDIR /app
927
- COPY --from=build /app/.x .x
928
- EXPOSE 3000
929
- CMD ["x", "start"]
930
- ```
931
-
932
- ---
933
-
934
- ## Configuration
935
-
936
- Configure x via `x.config.ts` at your project root. Use `defineConfig` from `@thexjs/core` for type-safe configuration.
937
-
938
- ### defineConfig
939
-
940
- All configuration options are optional. x provides sensible defaults so you can start with zero configuration and add settings as needed.
941
-
942
- ```ts
943
- import { defineConfig } from "@thexjs/core";
944
-
945
- export default defineConfig({
946
- // Page routes
947
- pagesDir: "src/pages",
948
-
949
- // Layout directory (for root layouts)
950
- layoutsDir: "src/layouts",
951
-
952
- // API routes
953
- apiDir: "src/api",
954
-
955
- // Server functions
956
- actionsDir: "src/actions",
957
-
958
- // Content collections (markdown)
959
- contentDir: "content",
960
-
961
- // Dev server port
962
- port: 3000,
963
-
964
- // Legacy routes directory
965
- routesDir: "src/routes",
966
- });
967
- ```
968
-
969
- ### All options reference
970
-
971
- | Option | Type | Default | Description |
972
- |--------------|----------|---------------|------------------------------------|
973
- | `pagesDir` | `string` | `"src/pages"` | File-based page routes |
974
- | `layoutsDir` | `string` | `"src/layouts"` | Root layout directory |
975
- | `apiDir` | `string` | `"src/api"` | File-based API routes |
976
- | `actionsDir` | `string` | `"src/actions"` | Server functions |
977
- | `contentDir` | `string` | `"content"` | Markdown content collections |
978
- | `port` | `number` | `3000` | Dev server port |
979
- | `routesDir` | `string` | `undefined` | Legacy routes directory |
980
-
981
- ### pagesDir
982
-
983
- The directory containing your page route files. Defaults to `src/pages`. Each `.tsx` file becomes a route based on its file path.
984
-
985
- ### layoutsDir
986
-
987
- The directory for root layout components. Layouts wrap pages and can be nested using the `_layout.tsx` convention inside page directories.
988
-
989
- ### apiDir
990
-
991
- The directory for API route files. Files here respond to HTTP methods (`GET`, `POST`, etc.) and are served under `/api/...`.
992
-
993
- ### actionsDir
994
-
995
- The directory for server functions. Exported async functions can be called from the browser via `fetch('/__x/actions/...')`.
996
-
997
- ### contentDir
998
-
999
- The directory for markdown content collections. Files with frontmatter are scanned and can be loaded via `scanContent` and `renderMarkdown`.
1000
-
1001
- ### port
1002
-
1003
- The port number for the dev server. Defaults to `3000`. Set to a different value if the default port is in use.
1004
-
1005
- ---
66
+ | Flag | Description |
67
+ | ----------------- | --------------------------------------------- |
68
+ | `<name>` | Project name (creates a directory named `slug`) |
69
+ | `--tailwind` | Include Tailwind CSS |
70
+ | `--shadcn` | Include shadcn/ui (implies `--tailwind`) |
71
+ | `--auth` | Include demo auth |
72
+ | `--content` | Include content collections |
73
+ | `--no-install` | Skip `bun install` |
74
+ | `--no-git` | Skip `git init` |
75
+ | `--dev` | Start the dev server after install |
1006
76
 
1007
77
  ## Development
1008
78
 
1009
79
  ```bash
1010
- bun install # install deps + links the x CLI
1011
- bun test # run the test suite
1012
- bun run typecheck
1013
- bun run lint
80
+ cd packages/create-thexjs-app
81
+ bun install
82
+ bun run build # build dist/index.js (tsup)
83
+ bun run build:watch # rebuild on change
1014
84
  ```
1015
85
 
1016
- ## Production
86
+ Run the built CLI locally:
1017
87
 
1018
88
  ```bash
1019
- x build # static export + server bundle -> .x/
1020
- x start # start the production server from .x/
89
+ node dist/index.js
1021
90
  ```
1022
91
 
1023
- ---
92
+ The generator reads the base template from `templates/base` and optional
93
+ feature addons from `templates/addons/<feature>`. Addons mirror the project
94
+ root and are merged on top of the base template, so addon files can override
95
+ base files (e.g. Tailwind's `src/styles/globals.css`).
1024
96
 
1025
- <p align="center">
1026
- <a href="./TASKS.md">Build Status</a> &nbsp;|&nbsp;
1027
- <a href="./LICENSE">MIT License</a>
1028
- </p>
97
+ ## License
98
+
99
+ MIT