create-thexjs-app 1.0.0 → 1.1.1

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