forte-cli 0.2.1

CLI for the Forte fullstack web framework
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# Forte CLI Implementation Plan

## Overview

Forte is a fullstack framework integrating a Rust backend + TypeScript/React frontend.
This document outlines the implementation plan for the Forte CLI tool.

## Current State

### Existing Components

| Project | Role | Status |
|---------|------|--------|
| `forte/` | SSR server orchestration | PoC complete |
| `forte-manual/` | Manually generated example app | PoC complete |
| `forte-rs-to-ts/` | Rust → TypeScript type generation | Complete |
| `forte-json/` | Streaming JSON serialization | Complete |
| `fn0/` | WASM/JS execution engine | Complete |
| `ski/` | JavaScript runtime (Deno core) | Complete |

### Code Generation Responsibilities

| Target | Owner | Execution Timing |
|--------|-------|-----------------|
| Backend routes (`route_generated.rs`) | `build.rs` | `cargo build` |
| Props types (`.props.ts`) | CLI → calls `forte-rs-to-ts` | `forte dev` |
| Frontend router (`routes.generated.ts`) | CLI | `forte dev` |
| Action types + client (`forte:actions`) | CLI | `forte dev` |

- Backend developer: Can work with just `cargo build`
- Frontend developer: Use `forte dev` to sync types + router

---

## CLI Command Structure

```
forte init <project-name>     # Create new project
forte add page <path>         # Add page
forte add action <name>       # Add action
forte dev                     # Dev server (watch + codegen)
forte build                   # Production build
```

---

## Step 1: Project Structure Refactoring

Currently, SSR server logic is in `forte/src/main.rs`.
Separate CLI and server.

### Post-refactoring Structure

```
forte/
├── Cargo.toml
├── src/
│   ├── main.rs              # CLI entry point
│   ├── cli/
│   │   ├── mod.rs
│   │   ├── init.rs          # forte init
│   │   ├── add.rs           # forte add
│   │   ├── dev.rs           # forte dev
│   │   └── build.rs         # forte build
│   ├── codegen/
│   │   ├── mod.rs
│   │   ├── backend_routes.rs    # route_generated.rs generation
│   │   └── frontend_routes.rs   # Frontend router generation
│   ├── server/
│   │   └── mod.rs           # SSR server logic from current main.rs
│   ├── watcher.rs           # File change detection
│   └── templates/           # Embedded templates
└── templates/               # Project/page template files
```

---

## Step 2: `forte dev` Implementation

Dev server + code generation + watch mode

### Execution Flow

```
forte dev
    ├─1. Initial code generation
    │   ├─ Run forte-rs-to-ts → generate .props.ts
    │   ├─ Generate backend routes → route_generated.rs
    │   └─ Generate frontend routes → routes.generated.ts
    ├─2. Initial build
    │   ├─ cargo build --target wasm32-wasip2 (backend)
    │   └─ npm run build (frontend)
    ├─3. Start server
    │   └─ http://localhost:3000
    └─4. Watch mode
        ├─ rs/src/pages/** change → backend rebuild + codegen
        └─ fe/src/pages/** change → frontend rebuild
```

### Code Generation Details

#### 2-1. Props Type Generation (using existing tool)

Call `forte-rs-to-ts`:
```rust
Command::new("forte-rs-to-ts")
    .arg(&project_rs_dir)
    .status()
```

#### 2-2. Backend Route Generation

Move current `rs/build.rs` logic to CLI.
`build.rs` simply calls CLI, or CLI generates directly.

Generated file: `rs/src/route_generated.rs`

#### 2-3. Frontend Router Generation

Auto-generate the hardcoded routing in current `fe/src/server.ts`.

Generated file: `fe/src/routes.generated.ts`

```typescript
// Auto-generated by forte CLI

import type { ComponentType } from 'react';

export interface Route {
    pattern: RegExp;
    params: string[];
    load: () => Promise<{ default: ComponentType<any> }>;
}

export const routes: Route[] = [
    {
        pattern: /^\/$/,
        params: [],
        load: () => import('./pages/index/page'),
    },
    {
        pattern: /^\/product\/([^/]+)$/,
        params: ['id'],
        load: () => import('./pages/product/[id]/page'),
    },
];
```

`server.ts` imports this for routing:

```typescript
import { routes } from './routes.generated';

export async function handler(request: Request): Promise<Response> {
    const url = new URL(request.url);

    for (const route of routes) {
        const match = url.pathname.match(route.pattern);
        if (match) {
            const params = Object.fromEntries(
                route.params.map((name, i) => [name, match[i + 1]])
            );
            const { default: Page } = await route.load();
            // ... render
        }
    }
}
```

---

## Step 3: `forte init` Implementation

### Command

```bash
forte init my-app
cd my-app
```

### Generated Structure

```
my-app/
├── Forte.toml
├── rs/
│   ├── Cargo.toml
│   ├── .cargo/config.toml
│   └── src/
│       ├── lib.rs
│       └── pages/
│           └── index/
│               └── mod.rs
└── fe/
    ├── package.json
    ├── tsconfig.json
    ├── rolldown.config.ts
    └── src/
        ├── server.ts
        └── pages/
            └── index/
                └── page.tsx
```

### Embedded Templates

Embed template files with `include_str!` or `rust-embed`:

```rust
static CARGO_TOML_TEMPLATE: &str = include_str!("../templates/rs/Cargo.toml.tmpl");
static PAGE_MOD_TEMPLATE: &str = include_str!("../templates/rs/page.mod.rs.tmpl");
// ...
```

### Variable Substitution

Simple template variable substitution:
- `{{project_name}}` → project name
- `{{page_name}}` → page name

---

## Step 4: `forte add page` Implementation

### Command

```bash
forte add page product/[id]
forte add page user/settings
```

### Behavior

1. Parse path
   - `product/[id]` → static: `product`, dynamic: `id`

2. Generate backend file
   - `rs/src/pages/product/[id]/mod.rs`

3. Generate frontend file
   - `fe/src/pages/product/[id]/page.tsx`

4. Regenerate code
   - Update `route_generated.rs`
   - Update `routes.generated.ts`
   - Generate `.props.ts`

### Templates

**rs/src/pages/{path}/mod.rs:**
```rust
use serde::Serialize;

#[derive(Serialize)]
pub enum Props {
    Ok {
        // TODO: Add your props here
    },
}

// Infallible - errors are handled via Props variants
pub async fn handler(
    _headers: std::collections::HashMap<String, String>,
    {{#params}}
    {{name}}: {{type}},
    {{/params}}
) -> Props {
    Props::Ok {}
}
```

**fe/src/pages/{path}/page.tsx:**
```tsx
import type { Props } from './.props';

export default function Page(props: Props) {
    if (props.t !== 'Ok') {
        return <div>Error</div>;
    }

    return (
        <div>
            <h1>{{page_name}}</h1>
            {/* TODO: Implement your page */}
        </div>
    );
}
```

---

## Step 5: `forte build` Implementation

### Command

```bash
forte build
```

### Output

```
dist/
├── backend.wasm         # or backend.cwasm (pre-compiled)
├── frontend/
│   └── server.js        # SSR bundle
└── static/              # Static assets
```

### Build Steps

1. **Code generation** (same as dev)

2. **Backend build**
   ```bash
   cargo build --release --target wasm32-wasip2
   ```

3. **CWASM pre-compilation** (optional)
   ```bash
   wasmtime compile backend.wasm -o backend.cwasm
   ```

4. **Frontend build**
   ```bash
   npm run build
   ```

5. **Copy static assets**
   - `fe/public/``dist/static/`
   - Asset hashing (optional)

---

## Step 6: Watch Mode + Hot Swap

### Dependencies

```toml
notify = "6"  # File system watching
```

### Watch Targets

| Path | Action on Change |
|------|-----------------|
| `rs/src/pages/**/*.rs` | Props codegen + backend rebuild + WASM hot swap |
| `rs/src/actions/**/*.rs` | Action codegen + backend rebuild + WASM hot swap |
| `fe/src/pages/**/*.tsx` | Frontend rebuild + JS hot swap |
| `fe/public/**` | Static asset change notification |
| `Forte.toml` | Full restart |

### Hot Swap (no server restart)

**Backend WASM:**
```
File change → cargo build → fn0 cache invalidation → new WASM loaded on next request
```

**SSR JS:**
```
File change → npm run build → fn0 cache invalidation → new JS loaded on next request
```

**Client (Phase 1: LiveReload):**
```
File change → reload signal to browser
```

### Debouncing

Delay rebuild on rapid consecutive changes:
```rust
let debounce_duration = Duration::from_millis(100);
```

---

## E2E Test Strategy

### Principles

- **All commands verified through E2E tests**
- No manual testing, automate with code
- Use project created by `forte init` for testing other commands

### Test Structure

```
forte/
├── tests/
│   └── e2e/
│       ├── mod.rs
│       ├── init_test.rs      # forte init tests
│       ├── dev_test.rs       # forte dev tests
│       ├── add_test.rs       # forte add tests
│       └── build_test.rs     # forte build tests
```

### Test Dependencies

```toml
[dev-dependencies]
assert_cmd = "2"      # CLI execution and verification
predicates = "3"      # Output verification
tempfile = "3"        # Temporary directories
reqwest = { version = "0.12", features = ["blocking"] }  # HTTP requests
```

### Test Scenarios

**`forte init` test:**
```rust
#[test]
fn test_init_creates_project_structure() {
    let temp = tempfile::tempdir().unwrap();

    Command::cargo_bin("forte").unwrap()
        .args(["init", "my-app"])
        .current_dir(&temp)
        .assert()
        .success();

    // Verify directory structure
    assert!(temp.path().join("my-app/Forte.toml").exists());
    assert!(temp.path().join("my-app/rs/Cargo.toml").exists());
    assert!(temp.path().join("my-app/fe/package.json").exists());
}
```

**`forte dev` test:**
```rust
#[tokio::test]
async fn test_dev_server_responds() {
    let temp = tempfile::tempdir().unwrap();

    // 1. Create project with forte init
    Command::cargo_bin("forte").unwrap()
        .args(["init", "test-app"])
        .current_dir(&temp)
        .assert()
        .success();

    // 2. Run forte dev in background
    let mut child = Command::cargo_bin("forte").unwrap()
        .args(["dev"])
        .current_dir(temp.path().join("test-app"))
        .spawn()
        .unwrap();

    // 3. Wait for server start then make HTTP request
    tokio::time::sleep(Duration::from_secs(10)).await;
    let resp = reqwest::get("http://127.0.0.1:3000").await.unwrap();
    assert!(resp.status().is_success());

    // 4. Cleanup
    child.kill().unwrap();
}
```

---

## Implementation Order

### Principles

- **Write test code first, then implement**
- Each step is considered complete when E2E tests pass

### Phase 1 (MVP)

| Order | Task | Test Verification |
|-------|------|------------------|
| 1 | Project structure refactoring ✅ | - |
| 2 | `forte init` | Directory structure, required file generation |
| 3 | `forte dev` E2E test | init → dev → HTTP 200 response |
| 4 | Frontend router auto-generation | routes.generated.ts file verification |
| 5 | `forte add page` | Page file generation, codegen execution |
| 6 | Watch mode + hot swap | File change → rebuild → response change |
| 7 | `forte add action` | Action file generation, type generation |
| 8 | Static asset serving | /static/* path response |
| 9 | `forte build` | dist/ directory generation, file verification |

### Phase 2 (Future)

| Task | Test Verification |
|------|------------------|
| Client HMR | WebSocket connection, module replacement |
| Production asset hashing | Hash-included filenames, manifest |
| Hydration support | Client JS execution verification |

---

## Dependencies

### CLI

```toml
[dependencies]
clap = { version = "4", features = ["derive"] }
notify = "6"
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
toml = "0.8"
```

### Existing Tools

- `forte-rs-to-ts`: Props type generation (called as external binary)
- `fn0`: WASM/JS execution (library dependency)
- `forte-json`: JSON serialization (used by backend)

---

## Forte.toml Configuration

```toml
[project]
name = "my-app"

[dev]
port = 3000

[build]
# Whether to pre-compile CWASM
precompile_wasm = false

[paths]
backend = "rs"
frontend = "fe"
```

---

## Decisions Made

### 1. Code Generation Responsibilities

| Target | Owner |
|--------|-------|
| Backend routes (`route_generated.rs`) | `build.rs` (kept) |
| Props/Action types, frontend router | CLI |

- Backend developer: Can work independently with just `cargo build`
- CLI calls `forte-rs-to-ts` for type generation

### 2. Hot Swap + HMR

**Phase 1 Implementation:**
- Backend WASM hot swap (no server restart)
- SSR JS hot swap (no server restart)
- Client: LiveReload (browser refresh)

**TODO (future must-have):**
- Client HMR (React Fast Refresh)
- Vite integration vs custom implementation → decide later

### 3. Action Implementation Approach

**Benchmarked against Astro Actions style**

Backend structure:
```
rs/src/
├── pages/
│   └── index/mod.rs
└── actions/
    └── user/
        └── login.rs      # → actions.user.login()
```

**actions/user/login.rs:**
```rust
use serde::{Serialize, Deserialize};

#[derive(Deserialize)]
pub struct Params {
    pub email: String,
    pub password: String,
}

#[derive(Serialize)]
pub enum Response {
    Ok { token: String },
    Error { message: String },
}

// Infallible - errors are handled via Response variants
pub async fn handler(request: Request) -> Response {
    Response::Ok { token: "...".to_string() }
}
```

**Frontend usage:**
```typescript
import { actions } from 'forte:actions';

const result = await actions.user.login({ email, password });
if (result.t === 'Ok') {
    console.log(result.v.token);
}
```

- Nested path support: `actions/user/login.rs``actions.user.login()`
- `forte:actions` import approach: Astro style (details to be decided during implementation)

### 4. Static Asset Handling

- Directory: `fe/public/`
- Development: `forte dev` serves directly
- Production build:
  - Asset hashing applied (cache invalidation)
  - CDN path support required