0
# CLI Tools
1
2
Command-line utilities for parsing files, combining schemas, and generating code from the terminal.
3
4
## Capabilities
5
6
### Parser CLI
7
8
Parse Flow and TypeScript files and output their schemas to the console.
9
10
```bash { .api }
11
# Parse one or more files
12
node parser-cli.js file1.js file2.ts file3.tsx
13
14
# Using from node_modules
15
node node_modules/@react-native/codegen/lib/cli/parser/parser-cli.js MyComponent.js
16
```
17
18
**Usage Example:**
19
20
```bash
21
# Parse a Flow component file
22
node parser-cli.js ./src/MyButton.js
23
24
# Parse multiple TypeScript files
25
node parser-cli.js ./src/MyButton.tsx ./src/MyModal.tsx ./src/MyModule.ts
26
```
27
28
The parser CLI automatically detects file types based on extensions and outputs JSON schemas to stdout.
29
30
### Schema Combination CLI
31
32
Combine multiple pre-generated schema files with platform filtering.
33
34
```bash { .api }
35
# Combine schemas with platform filtering
36
node combine-schemas-cli.js -p <platform> -o <output> -s @<schema-query-file>
37
38
# Parameters:
39
# -p, --platform: Target platform (ios or android)
40
# -o, --output: Output JSON file path
41
# -s, --schema-query: File containing list of schema files (must start with @)
42
```
43
44
**Usage Example:**
45
46
```bash
47
# Create a query file listing schema files
48
echo "schema1.json schema2.json schema3.json" > query.txt
49
50
# Combine for iOS platform
51
node combine-schemas-cli.js -p ios -o ios-combined.json -s @query.txt
52
53
# Combine for Android platform
54
node combine-schemas-cli.js -p android -o android-combined.json -s @query.txt
55
```
56
57
The schema query file should contain space-separated paths to JSON schema files.
58
59
### JavaScript-to-Schema CLI
60
61
Convert JavaScript/TypeScript source files directly to a combined schema.
62
63
```bash { .api }
64
# Convert JS/TS files to schema
65
node combine-js-to-schema-cli.js <outfile> <file1> [<file2> ...]
66
67
# Options:
68
# -p, --platform: Platform filter for filenames (ios/android)
69
# -e, --exclude: Regular expression to exclude files
70
# -l, --libraryName: Library name for the generated schema
71
```
72
73
**Usage Example:**
74
75
```bash
76
# Basic conversion
77
node combine-js-to-schema-cli.js combined.json ./src/Component1.js ./src/Component2.tsx
78
79
# With platform filtering and library name
80
node combine-js-to-schema-cli.js schema.json ./src/*.js -p ios -l MyLibrary
81
82
# Exclude test files
83
node combine-js-to-schema-cli.js production.json ./src/**/*.js -e "test|spec" -l MyApp
84
```
85
86
### Code Generation CLI
87
88
Generate all possible native code outputs from a schema file.
89
90
```bash { .api }
91
# Generate all code outputs
92
node generate-all.js <schemaPath> <libraryName> <outputDirectory> <packageName> <assumeNonnull>
93
94
# Parameters:
95
# schemaPath: Path to the JSON schema file
96
# libraryName: Name of the library being generated
97
# outputDirectory: Directory where generated files will be written
98
# packageName: Package name for platform-specific code
99
# assumeNonnull: Whether to assume nonnull annotations (true/false)
100
```
101
102
**Usage Example:**
103
104
```bash
105
# Generate all outputs for a library
106
node generate-all.js ./schema.json MyLibrary ./generated com.example.mylibrary true
107
108
# The generated directory will contain:
109
# - iOS component files (Objective-C++/C++)
110
# - Android component files (Java/C++)
111
# - TurboModule implementations
112
# - Test files
113
# - Component descriptors
114
```
115
116
This generates code using all available generators:
117
- `descriptors` - Component descriptor files
118
- `events` - Event emitter code
119
- `props` - Props interface code
120
- `states` - Component state code
121
- `tests` - Test code generation
122
- `shadow-nodes` - Shadow node implementations
123
- `modulesAndroid` - Android TurboModule code
124
- `modulesCxx` - C++ TurboModule code
125
- `modulesIOS` - iOS TurboModule code
126
127
## CLI Workflow Examples
128
129
### Complete Development Workflow
130
131
```bash
132
# 1. Parse component files to understand their structure
133
node parser-cli.js src/MyComponent.js src/MyModule.ts
134
135
# 2. Combine source files into a schema
136
node combine-js-to-schema-cli.js app-schema.json src/**/*.{js,ts,tsx} -l MyApp
137
138
# 3. Generate platform-specific code
139
node generate-all.js app-schema.json MyApp ./ios/Generated com.example.myapp false
140
```
141
142
### Platform-Specific Build Pipeline
143
144
```bash
145
# Create iOS-specific schema
146
node combine-js-to-schema-cli.js ios-schema.json src/**/*.{js,ts,tsx} -p ios -l MyApp
147
148
# Create Android-specific schema
149
node combine-js-to-schema-cli.js android-schema.json src/**/*.{js,ts,tsx} -p android -l MyApp
150
151
# Generate platform-specific code
152
node generate-all.js ios-schema.json MyApp ./ios/Generated com.example.myapp true
153
node generate-all.js android-schema.json MyApp ./android/generated com.example.myapp true
154
```
155
156
### Integration with Build Systems
157
158
The CLI tools can be integrated into npm scripts or build systems:
159
160
```json
161
{
162
"scripts": {
163
"codegen": "node node_modules/@react-native/codegen/lib/cli/combine/combine-js-to-schema-cli.js schema.json src/**/*.{js,ts,tsx} -l MyApp",
164
"codegen:ios": "node node_modules/@react-native/codegen/lib/cli/generators/generate-all.js schema.json MyApp ./ios/Generated com.example.myapp true",
165
"codegen:android": "node node_modules/@react-native/codegen/lib/cli/generators/generate-all.js schema.json MyApp ./android/Generated com.example.myapp true"
166
}
167
}
168
```
169
170
## CLI Error Handling
171
172
All CLI tools provide detailed error messages for common issues:
173
174
- **File not found**: Clear indication of missing files with full paths
175
- **Parse errors**: Specific line and column information for syntax errors
176
- **Schema validation**: Detailed validation error messages with suggestions
177
- **Platform conflicts**: Clear explanation of platform exclusion conflicts
178
- **Generation failures**: Specific generator error messages with context
179
180
Exit codes:
181
- `0`: Success
182
- `1`: General error (file not found, invalid arguments)
183
- `2`: Parse error (invalid JavaScript/TypeScript syntax)
184
- `3`: Schema validation error
185
- `4`: Code generation error