|
| 1 | +# Usage with Brownie package |
| 2 | + |
| 3 | +The `brownie codegen` CLI command generates Brownie state management library native store types from TypeScript schema. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```bash |
| 8 | +brownie codegen # Generate for all configured platforms |
| 9 | +brownie codegen -p swift # Generate Swift only |
| 10 | +brownie codegen --platform kotlin # Generate Kotlin only |
| 11 | +brownie codegen --help # Show help for Brownie state management codegen |
| 12 | +brownie --version # Show version |
| 13 | +``` |
| 14 | + |
| 15 | +## Configuration |
| 16 | + |
| 17 | +Add to your app's `package.json`: |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "brownie": { |
| 22 | + "swift": "./ios/Generated/", |
| 23 | + "kotlin": "./android/app/src/main/java/com/example/", |
| 24 | + "kotlinPackageName": "com.example" |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +| Field | Required | Description | |
| 30 | +| ------------------- | -------- | ----------------------------------------- | |
| 31 | +| `swift` | No\* | Output directory for Swift files | |
| 32 | +| `kotlin` | No\* | Output directory for Kotlin files | |
| 33 | +| `kotlinPackageName` | No | Kotlin package name (extracted from path) | |
| 34 | + |
| 35 | +\*At least one of `swift` or `kotlin` is required. |
| 36 | + |
| 37 | +## Store Definition |
| 38 | + |
| 39 | +Stores are auto-discovered from `*.brownie.ts` files. Define your store shape using module augmentation: |
| 40 | + |
| 41 | +```ts |
| 42 | +// BrownfieldStore.brownie.ts |
| 43 | +import type { BrownieStore } from '@callstack/brownie'; |
| 44 | + |
| 45 | +interface BrownfieldStore extends BrownieStore { |
| 46 | + counter: number; |
| 47 | + user: string; |
| 48 | + isLoading: boolean; |
| 49 | +} |
| 50 | + |
| 51 | +declare module '@callstack/brownie' { |
| 52 | + interface BrownieStores { |
| 53 | + BrownfieldStore: BrownfieldStore; |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +Multiple stores in same file: |
| 59 | + |
| 60 | +```ts |
| 61 | +// Stores.brownie.ts |
| 62 | +import type { BrownieStore } from '@callstack/brownie'; |
| 63 | + |
| 64 | +interface UserStore extends BrownieStore { |
| 65 | + name: string; |
| 66 | + email: string; |
| 67 | +} |
| 68 | + |
| 69 | +interface SettingsStore extends BrownieStore { |
| 70 | + theme: 'light' | 'dark'; |
| 71 | + notificationsEnabled: boolean; |
| 72 | +} |
| 73 | + |
| 74 | +declare module '@callstack/brownie' { |
| 75 | + interface BrownieStores { |
| 76 | + UserStore: UserStore; |
| 77 | + SettingsStore: SettingsStore; |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## Generated Output |
| 83 | + |
| 84 | +**Swift** (`Codable` struct with mutable properties): |
| 85 | + |
| 86 | +```swift |
| 87 | +struct BrownfieldStore: Codable { |
| 88 | + var counter: Double |
| 89 | + var isLoading: Bool |
| 90 | + var user: String |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +**Kotlin** (data class): |
| 95 | + |
| 96 | +```kotlin |
| 97 | +package com.example |
| 98 | + |
| 99 | +data class BrownfieldStore ( |
| 100 | + val counter: Double, |
| 101 | + val isLoading: Boolean, |
| 102 | + val user: String |
| 103 | +) |
| 104 | +``` |
| 105 | + |
| 106 | +### Store Discovery |
| 107 | + |
| 108 | +1. Recursively finds `*.brownie.ts` files (skips node_modules) |
| 109 | +2. Parses `declare module '@callstack/brownie'` blocks using ts-morph |
| 110 | +3. Extracts store names from `BrownieStores` interface properties |
| 111 | +4. Validates no duplicate store names exist |
| 112 | + |
| 113 | +### Codegen Pipeline |
| 114 | + |
| 115 | +``` |
| 116 | +*.brownie.ts files (auto-discovered) |
| 117 | + │ |
| 118 | + ▼ |
| 119 | +ts-morph (parse BrownieStores interface) |
| 120 | + │ |
| 121 | + ▼ |
| 122 | +quicktype-typescript-input (schemaForTypeScriptSources) |
| 123 | + │ |
| 124 | + ▼ |
| 125 | +JSON Schema |
| 126 | + │ |
| 127 | + ▼ |
| 128 | +quicktype-core |
| 129 | + │ |
| 130 | + ├──▶ Swift (lang: 'swift', mutable-properties: true) |
| 131 | + │ |
| 132 | + └──▶ Kotlin (lang: 'kotlin', framework: 'just-types') |
| 133 | +``` |
0 commit comments