|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: 2025 Torkild G. Kjevik |
| 3 | +SPDX-FileCopyrightText: 2025 ash_typescript contributors <https://github.com/ash-project/ash_typescript/graphs.contributors> |
| 4 | +
|
| 5 | +SPDX-License-Identifier: MIT |
| 6 | +--> |
| 7 | + |
| 8 | +# Mix Tasks Reference |
| 9 | + |
| 10 | +This document provides a comprehensive reference for all AshTypescript Mix tasks. |
| 11 | + |
| 12 | +## Installation Commands |
| 13 | + |
| 14 | +### `mix igniter.install ash_typescript` |
| 15 | + |
| 16 | +**Automated installer** that sets up everything you need to get started with AshTypescript. |
| 17 | + |
| 18 | +#### Usage |
| 19 | + |
| 20 | +```bash |
| 21 | +# Basic installation (RPC setup only) |
| 22 | +mix igniter.install ash_typescript |
| 23 | + |
| 24 | +# Full-stack React + TypeScript setup |
| 25 | +mix igniter.install ash_typescript --framework react |
| 26 | +``` |
| 27 | + |
| 28 | +#### What It Does |
| 29 | + |
| 30 | +The installer performs the following tasks: |
| 31 | + |
| 32 | +1. **Dependency Setup** |
| 33 | + - Adds AshTypescript to your `mix.exs` dependencies |
| 34 | + - Runs `mix deps.get` to install the package |
| 35 | + |
| 36 | +2. **Configuration** |
| 37 | + - Configures AshTypescript settings in `config/config.exs` |
| 38 | + - Sets default output paths and RPC endpoints |
| 39 | + |
| 40 | +3. **RPC Controller** |
| 41 | + - Creates RPC controller at `lib/*_web/controllers/ash_typescript_rpc_controller.ex` |
| 42 | + - Implements handlers for run and validate endpoints |
| 43 | + |
| 44 | +4. **Phoenix Router** |
| 45 | + - Adds RPC routes to your Phoenix router |
| 46 | + - Configures `/rpc/run` and `/rpc/validate` endpoints |
| 47 | + |
| 48 | +5. **React Setup** (with `--framework react`) |
| 49 | + - Sets up complete React + TypeScript environment |
| 50 | + - Configures esbuild or vite for frontend builds |
| 51 | + - Creates welcome page with getting started guide |
| 52 | + - Installs necessary npm packages |
| 53 | + |
| 54 | +#### Options |
| 55 | + |
| 56 | +| Option | Description | |
| 57 | +|--------|-------------| |
| 58 | +| `--framework react` | Set up React + TypeScript environment | |
| 59 | + |
| 60 | +#### When to Use |
| 61 | + |
| 62 | +- ✅ New projects starting with AshTypescript |
| 63 | +- ✅ Adding AshTypescript to existing Phoenix projects |
| 64 | +- ✅ Setting up frontend with React integration |
| 65 | +- ❌ Projects that already have AshTypescript installed |
| 66 | + |
| 67 | +**This is the recommended approach for initial setup.** |
| 68 | + |
| 69 | +## Code Generation Commands |
| 70 | + |
| 71 | +### `mix ash.codegen` |
| 72 | + |
| 73 | +**Preferred approach** for generating TypeScript types along with other Ash extensions in your project. |
| 74 | + |
| 75 | +#### Usage |
| 76 | + |
| 77 | +```bash |
| 78 | +# Generate types for all Ash extensions including AshTypescript |
| 79 | +mix ash.codegen --dev |
| 80 | + |
| 81 | +# With custom output location |
| 82 | +mix ash.codegen --dev --output "assets/js/ash_rpc.ts" |
| 83 | +``` |
| 84 | + |
| 85 | +#### What It Does |
| 86 | + |
| 87 | +Runs code generation for all Ash extensions in your project, including: |
| 88 | +- AshTypescript (TypeScript types and RPC clients) |
| 89 | +- AshPostgres (database migrations, if installed) |
| 90 | +- AshGraphql (GraphQL schemas, if installed) |
| 91 | +- Other Ash extensions |
| 92 | + |
| 93 | +#### Options |
| 94 | + |
| 95 | +| Option | Description | |
| 96 | +|--------|-------------| |
| 97 | +| `--dev` | Run codegen for development environment | |
| 98 | +| `--output FILE` | Custom output file path for TypeScript | |
| 99 | + |
| 100 | +#### When to Use |
| 101 | + |
| 102 | +- ✅ Projects with multiple Ash extensions |
| 103 | +- ✅ Want to run all codegen tasks together |
| 104 | +- ✅ Standard workflow for Ash projects |
| 105 | +- ❌ Need fine-grained control over AshTypescript only |
| 106 | + |
| 107 | +**This is the recommended approach for most projects.** |
| 108 | + |
| 109 | +### `mix ash_typescript.codegen` |
| 110 | + |
| 111 | +Generate TypeScript types, RPC clients, Zod schemas, and validation functions **only for AshTypescript**. |
| 112 | + |
| 113 | +#### Usage |
| 114 | + |
| 115 | +```bash |
| 116 | +# Basic generation (AshTypescript only) |
| 117 | +mix ash_typescript.codegen |
| 118 | + |
| 119 | +# Custom output location |
| 120 | +mix ash_typescript.codegen --output "frontend/src/api/ash.ts" |
| 121 | + |
| 122 | +# Custom RPC endpoints |
| 123 | +mix ash_typescript.codegen \ |
| 124 | + --run_endpoint "/api/rpc/run" \ |
| 125 | + --validate_endpoint "/api/rpc/validate" |
| 126 | + |
| 127 | +# Check if generated code is up to date (CI usage) |
| 128 | +mix ash_typescript.codegen --check |
| 129 | + |
| 130 | +# Preview generated code without writing to file |
| 131 | +mix ash_typescript.codegen --dry_run |
| 132 | +``` |
| 133 | + |
| 134 | +#### Options |
| 135 | + |
| 136 | +| Option | Type | Default | Description | |
| 137 | +|--------|------|---------|-------------| |
| 138 | +| `--output FILE` | `string` | `assets/js/ash_rpc.ts` | Output file path for generated TypeScript | |
| 139 | +| `--run_endpoint PATH` | `string` | `/rpc/run` | RPC run endpoint path | |
| 140 | +| `--validate_endpoint PATH` | `string` | `/rpc/validate` | RPC validate endpoint path | |
| 141 | +| `--check` | `boolean` | `false` | Check if generated code is up to date (exit 1 if not) | |
| 142 | +| `--dry_run` | `boolean` | `false` | Print generated code to stdout without writing file | |
| 143 | + |
| 144 | +#### Generated Content |
| 145 | + |
| 146 | +When run, this task generates: |
| 147 | + |
| 148 | +1. **TypeScript Interfaces** |
| 149 | + - Resource types with field metadata |
| 150 | + - Schema types for field selection |
| 151 | + - Result types for each action |
| 152 | + |
| 153 | +2. **RPC Client Functions** |
| 154 | + - HTTP-based RPC functions for each action |
| 155 | + - Channel-based RPC functions (if enabled) |
| 156 | + - Type-safe configuration objects |
| 157 | + |
| 158 | +3. **Filter Input Types** |
| 159 | + - Comprehensive filter operators |
| 160 | + - Type-safe query building |
| 161 | + - Nested relationship filtering |
| 162 | + |
| 163 | +4. **Zod Validation Schemas** (if enabled) |
| 164 | + - Runtime type validation |
| 165 | + - Schema for each resource |
| 166 | + - Nested validation support |
| 167 | + |
| 168 | +5. **Form Validation Functions** |
| 169 | + - Client-side validation helpers |
| 170 | + - Error message handling |
| 171 | + - Field-level validation |
| 172 | + |
| 173 | +6. **Typed Query Constants** |
| 174 | + - Pre-configured field selections |
| 175 | + - SSR-optimized types |
| 176 | + - Type-safe result extraction |
| 177 | + |
| 178 | +7. **Custom Type Imports** |
| 179 | + - Imports for custom types |
| 180 | + - Integration with external types |
| 181 | + - Type mapping support |
| 182 | + |
| 183 | +#### Examples |
| 184 | + |
| 185 | +**Basic Generation:** |
| 186 | +```bash |
| 187 | +mix ash_typescript.codegen |
| 188 | +``` |
| 189 | + |
| 190 | +**Custom Output Location:** |
| 191 | +```bash |
| 192 | +mix ash_typescript.codegen --output "frontend/src/api/ash.ts" |
| 193 | +``` |
| 194 | + |
| 195 | +**Custom RPC Endpoints:** |
| 196 | +```bash |
| 197 | +mix ash_typescript.codegen \ |
| 198 | + --run_endpoint "/api/rpc/run" \ |
| 199 | + --validate_endpoint "/api/rpc/validate" |
| 200 | +``` |
| 201 | + |
| 202 | +**CI Check:** |
| 203 | +```bash |
| 204 | +# In CI pipeline - fails if generated code is out of date |
| 205 | +mix ash_typescript.codegen --check |
| 206 | +``` |
| 207 | + |
| 208 | +**Preview Without Writing:** |
| 209 | +```bash |
| 210 | +# See what would be generated |
| 211 | +mix ash_typescript.codegen --dry_run | less |
| 212 | +``` |
| 213 | + |
| 214 | +#### When to Use |
| 215 | + |
| 216 | +- ✅ Want to run codegen specifically for AshTypescript |
| 217 | +- ✅ Need custom output paths or endpoints |
| 218 | +- ✅ Debugging generated TypeScript code |
| 219 | +- ✅ CI/CD pipelines with `--check` flag |
| 220 | +- ❌ Have other Ash extensions that need codegen (use `mix ash.codegen`) |
| 221 | + |
| 222 | +## Test Environment Code Generation |
| 223 | + |
| 224 | +For projects using test-only resources (common in library development), use the test environment: |
| 225 | + |
| 226 | +```bash |
| 227 | +# Generate types in test environment |
| 228 | +MIX_ENV=test mix ash_typescript.codegen |
| 229 | + |
| 230 | +# Or use the test.codegen alias (if defined) |
| 231 | +mix test.codegen |
| 232 | +``` |
| 233 | + |
| 234 | +### Setting Up Test Codegen Alias |
| 235 | + |
| 236 | +Add to your `mix.exs`: |
| 237 | + |
| 238 | +```elixir |
| 239 | +defp aliases do |
| 240 | + [ |
| 241 | + "test.codegen": ["cmd MIX_ENV=test mix ash_typescript.codegen"], |
| 242 | + # ... other aliases |
| 243 | + ] |
| 244 | +end |
| 245 | +``` |
| 246 | + |
| 247 | +## Workflow Integration |
| 248 | + |
| 249 | +### Development Workflow |
| 250 | + |
| 251 | +```bash |
| 252 | +# 1. Make changes to resources or domain configuration |
| 253 | +vim lib/my_app/resources/todo.ex |
| 254 | + |
| 255 | +# 2. Generate TypeScript types |
| 256 | +mix ash.codegen --dev |
| 257 | + |
| 258 | +# 3. Verify TypeScript compilation (in frontend directory) |
| 259 | +cd assets && npm run typecheck |
| 260 | + |
| 261 | +# 4. Run tests |
| 262 | +mix test |
| 263 | +``` |
| 264 | + |
| 265 | +### CI/CD Workflow |
| 266 | + |
| 267 | +```bash |
| 268 | +# In your CI pipeline (.github/workflows/ci.yml, etc.) |
| 269 | + |
| 270 | +# Check generated code is up to date |
| 271 | +mix ash_typescript.codegen --check |
| 272 | + |
| 273 | +# If out of date, CI fails with: |
| 274 | +# "Generated TypeScript code is out of date. Run: mix ash_typescript.codegen" |
| 275 | +``` |
| 276 | + |
| 277 | +**Example GitHub Actions:** |
| 278 | + |
| 279 | +```yaml |
| 280 | +- name: Check TypeScript codegen |
| 281 | + run: mix ash_typescript.codegen --check |
| 282 | + |
| 283 | +- name: Type check generated code |
| 284 | + run: | |
| 285 | + cd assets |
| 286 | + npm run typecheck |
| 287 | +``` |
| 288 | +
|
| 289 | +### Pre-commit Hook |
| 290 | +
|
| 291 | +Add to `.git/hooks/pre-commit`: |
| 292 | + |
| 293 | +```bash |
| 294 | +#!/bin/bash |
| 295 | +# Regenerate TypeScript on commit |
| 296 | +mix ash_typescript.codegen --check || { |
| 297 | + echo "TypeScript code out of date. Regenerating..." |
| 298 | + mix ash_typescript.codegen |
| 299 | + git add assets/js/ash_rpc.ts |
| 300 | +} |
| 301 | +``` |
| 302 | + |
| 303 | +## Troubleshooting |
| 304 | + |
| 305 | +### Common Issues |
| 306 | + |
| 307 | +#### "No domains found" |
| 308 | + |
| 309 | +**Problem:** Command runs but generates empty output or reports no domains. |
| 310 | + |
| 311 | +**Solution:** Ensure you're in the correct MIX_ENV: |
| 312 | + |
| 313 | +```bash |
| 314 | +# Wrong - uses dev environment |
| 315 | +mix ash_typescript.codegen |
| 316 | +
|
| 317 | +# Correct - uses test environment for test resources |
| 318 | +MIX_ENV=test mix ash_typescript.codegen |
| 319 | +``` |
| 320 | + |
| 321 | +#### Generated code doesn't compile |
| 322 | + |
| 323 | +**Problem:** TypeScript compilation fails after generation. |
| 324 | + |
| 325 | +**Solution:** Check for: |
| 326 | +1. Invalid field names (use field name mapping) |
| 327 | +2. Custom types not defined in imported modules |
| 328 | +3. Missing type mapping overrides for dependency types |
| 329 | + |
| 330 | +See [Configuration Reference](configuration.md) for field name mapping and type overrides. |
| 331 | + |
| 332 | +#### Changes not reflected |
| 333 | + |
| 334 | +**Problem:** Made changes to resources but generated TypeScript unchanged. |
| 335 | + |
| 336 | +**Solution:** |
| 337 | +1. Recompile Elixir code: `mix compile --force` |
| 338 | +2. Regenerate TypeScript: `mix ash_typescript.codegen` |
| 339 | +3. Verify output file path matches configuration |
| 340 | + |
| 341 | +#### Permission errors |
| 342 | + |
| 343 | +**Problem:** Cannot write to output file. |
| 344 | + |
| 345 | +**Solution:** Check file permissions and directory structure: |
| 346 | + |
| 347 | +```bash |
| 348 | +# Ensure directory exists |
| 349 | +mkdir -p assets/js |
| 350 | +
|
| 351 | +# Check permissions |
| 352 | +ls -la assets/js |
| 353 | +
|
| 354 | +# Fix if needed |
| 355 | +chmod 755 assets/js |
| 356 | +``` |
| 357 | + |
| 358 | +## See Also |
| 359 | + |
| 360 | +- [Configuration Reference](configuration.md) - Configure code generation |
| 361 | +- [Getting Started Tutorial](../tutorials/getting-started.md) - Initial setup guide |
| 362 | +- [Troubleshooting Reference](troubleshooting.md) - Common problems and solutions |
0 commit comments