0
# Main API Functions
1
2
Core programmatic interface for the React Native CLI providing functions for running the CLI, loading project configurations, and integrating with development tools.
3
4
## Capabilities
5
6
### CLI Execution
7
8
Primary entry point for running the React Native CLI programmatically with optional platform-specific behavior.
9
10
```typescript { .api }
11
/**
12
* Main entry point to run the CLI with optional platform name
13
* @param platformName - Optional platform name for out-of-tree platforms (e.g., 'macos', 'windows')
14
* @returns Promise that resolves when CLI execution completes
15
* @throws CLIError when configuration loading fails or command execution encounters errors
16
*/
17
function run(platformName?: string): Promise<void>;
18
```
19
20
**Usage Examples:**
21
22
```typescript
23
import { run } from "@react-native-community/cli";
24
25
// Run CLI with default behavior
26
await run();
27
28
// Run CLI with out-of-tree platform support
29
await run("macos");
30
```
31
32
### Configuration Loading
33
34
Functions for loading React Native project configuration either synchronously or asynchronously.
35
36
```typescript { .api }
37
/**
38
* Synchronously loads React Native CLI configuration
39
* @param options - Optional configuration loading options
40
* @param options.projectRoot - Project root directory path (defaults to findProjectRoot())
41
* @param options.selectedPlatform - Platform to load configuration for (e.g., 'ios', 'android')
42
* @returns Config object with project settings, platforms, and commands
43
* @throws Error if configuration cannot be loaded or project not found
44
*/
45
function loadConfig(options?: LoadConfigOptions): Config;
46
47
/**
48
* Asynchronously loads React Native CLI configuration
49
* @param options - Optional configuration loading options
50
* @param options.projectRoot - Project root directory path (defaults to findProjectRoot())
51
* @param options.selectedPlatform - Platform to load configuration for (e.g., 'ios', 'android')
52
* @returns Promise resolving to Config object
53
* @throws CLIError if configuration loading fails
54
*/
55
function loadConfigAsync(options?: LoadConfigOptions): Promise<Config>;
56
```
57
58
**Usage Examples:**
59
60
```typescript
61
import { loadConfig, loadConfigAsync } from "@react-native-community/cli";
62
63
// Synchronous loading
64
try {
65
const config = loadConfig();
66
console.log("Project root:", config.root);
67
console.log("React Native path:", config.reactNativePath);
68
} catch (error) {
69
console.log("Not in a React Native project");
70
}
71
72
// Load with custom project root
73
const config = loadConfig({ projectRoot: "/path/to/project" });
74
75
// Asynchronous loading
76
const config = await loadConfigAsync();
77
console.log("Available platforms:", Object.keys(config.platforms));
78
79
// Platform-specific loading
80
const iosConfig = await loadConfigAsync({ selectedPlatform: "ios" });
81
console.log("iOS configuration:", iosConfig.platforms.ios);
82
```
83
84
### Development Server Middleware
85
86
Creates middleware for Metro development server integration with WebSocket support for React Native development tools.
87
88
```typescript { .api }
89
/**
90
* Creates development server middleware for Metro bundler
91
* @param options - Middleware configuration options
92
* @param options.host - Server host (optional, defaults to localhost)
93
* @param options.port - Server port number
94
* @param options.watchFolders - Array of directories to watch for changes
95
* @returns DevServerMiddleware object with endpoints and middleware
96
*/
97
function createDevServerMiddleware(options: MiddlewareOptions): DevServerMiddleware;
98
99
interface MiddlewareOptions {
100
host?: string;
101
port: number;
102
watchFolders: ReadonlyArray<string>;
103
}
104
105
interface DevServerMiddleware {
106
websocketEndpoints: {
107
'/message': any;
108
'/events': any;
109
};
110
messageSocketEndpoint: any;
111
eventsSocketEndpoint: any;
112
middleware: any;
113
}
114
```
115
116
**Usage Examples:**
117
118
```typescript
119
import { createDevServerMiddleware } from "@react-native-community/cli";
120
121
// Create development server middleware
122
const middleware = createDevServerMiddleware({
123
port: 8081,
124
watchFolders: ["./src", "./node_modules"],
125
});
126
127
// Use with Express server
128
app.use(middleware.middleware);
129
```
130
131
### Binary Path
132
133
Constant providing the path to the CLI binary entry point for programmatic access.
134
135
```typescript { .api }
136
/**
137
* Path to the CLI binary entry point
138
* Resolved path to the bin.js file for programmatic execution
139
*/
140
const bin: string;
141
```
142
143
**Usage Examples:**
144
145
```typescript
146
import { bin } from "@react-native-community/cli";
147
import { spawn } from "child_process";
148
149
// Execute CLI binary programmatically
150
const child = spawn("node", [bin, "info"], {
151
stdio: "inherit"
152
});
153
```
154
155
## Types
156
157
```typescript { .api }
158
interface Config {
159
root: string;
160
reactNativePath: string;
161
reactNativeVersion: string;
162
project: ProjectConfig;
163
dependencies: Record<string, DependencyConfig>;
164
platforms: Record<string, PlatformConfig<any, any, any, any>>;
165
assets: string[];
166
commands: Command[];
167
healthChecks: [];
168
}
169
170
interface LoadConfigOptions {
171
projectRoot?: string;
172
selectedPlatform?: string;
173
}
174
175
interface ProjectConfig {
176
android?: AndroidProjectConfig;
177
ios?: IOSProjectConfig;
178
[key: string]: any;
179
}
180
181
interface MiddlewareOptions {
182
host?: string;
183
port: number;
184
watchFolders: ReadonlyArray<string>;
185
}
186
187
interface DevServerMiddleware {
188
websocketEndpoints: {
189
'/message': WebSocketServer;
190
'/events': WebSocketServer;
191
};
192
messageSocketEndpoint: MessageSocketEndpoint;
193
eventsSocketEndpoint: EventsSocketEndpoint;
194
middleware: ConnectMiddleware;
195
}
196
```