0
# Command-Line Interface
1
2
Main CLI class that handles command parsing, environment setup, and command execution for all Taro operations.
3
4
## Capabilities
5
6
### CLI Class
7
8
Main command-line interface handler that processes arguments and executes Taro commands.
9
10
```typescript { .api }
11
/**
12
* Main CLI class for handling Taro command-line operations
13
*/
14
class CLI {
15
appPath: string;
16
17
/**
18
* Initialize CLI with optional application path
19
* @param appPath - Optional path to the application directory, defaults to process.cwd()
20
*/
21
constructor(appPath?: string);
22
23
/**
24
* Execute CLI with command-line arguments
25
* @returns Promise that resolves when command execution completes
26
*/
27
run(): Promise<void>;
28
29
/**
30
* Parse command-line arguments and route to appropriate handlers
31
* @returns Promise that resolves when argument parsing completes
32
*/
33
parseArgs(): Promise<void>;
34
}
35
```
36
37
**Usage Examples:**
38
39
```typescript
40
import { CLI } from "@tarojs/cli";
41
42
// Basic CLI usage
43
const cli = new CLI();
44
await cli.run();
45
46
// CLI with custom app path
47
const cli = new CLI('/path/to/my-app');
48
await cli.run();
49
50
// Direct argument parsing
51
const cli = new CLI();
52
await cli.parseArgs();
53
```
54
55
### Command-Line Options
56
57
The CLI supports numerous command-line options for different operations:
58
59
```typescript { .api }
60
interface CLIOptions {
61
// Global options
62
version?: boolean;
63
help?: boolean;
64
'disable-global-config'?: boolean;
65
66
// Build options
67
type?: string; // Platform type (weapp, h5, etc.)
68
watch?: boolean; // Enable watch mode
69
env?: string; // Environment (development, production)
70
mode?: string; // Build mode
71
port?: number; // Development server port
72
73
// React Native specific
74
'reset-cache'?: boolean;
75
'public-path'?: string;
76
'bundle-output'?: string;
77
'sourcemap-output'?: string;
78
'sourcemap-use-absolute-path'?: boolean;
79
'sourcemap-sources-root'?: string;
80
'assets-dest'?: string;
81
82
// Project creation options
83
name?: string;
84
description?: string;
85
typescript?: boolean;
86
framework?: FrameworkType;
87
compiler?: CompilerTypes;
88
npm?: NpmType;
89
'template-source'?: string;
90
template?: string;
91
css?: CSSType;
92
clone?: boolean;
93
autoInstall?: boolean;
94
95
// Build feature flags
96
build?: boolean;
97
check?: boolean;
98
'inject-global-style'?: boolean;
99
'new-blended'?: boolean;
100
blended?: boolean;
101
qr?: boolean;
102
}
103
```
104
105
### Available Commands
106
107
The CLI provides the following built-in commands:
108
109
```typescript { .api }
110
type TaroCommand =
111
| 'init' // Initialize new project
112
| 'build' // Build project for target platform
113
| 'create' // Create page/component
114
| 'config' // Manage configuration
115
| 'global-config' // Manage global configuration
116
| 'doctor' // Run project diagnostics
117
| 'info' // Display environment information
118
| 'update' // Update Taro packages
119
| 'inspect' // Inspect webpack configuration
120
| 'help'; // Display help information
121
```
122
123
**Command Usage Examples:**
124
125
```bash
126
# Project initialization
127
taro init my-project --framework react --typescript
128
129
# Building for different platforms
130
taro build --type weapp --env production
131
taro build --type h5 --watch --port 3000
132
133
# Creating new pages
134
taro create --name HomePage --typescript
135
136
# Project diagnostics
137
taro doctor
138
139
# Configuration management
140
taro config set registry https://registry.npm.taobao.org
141
taro global-config
142
143
# Environment information
144
taro info
145
146
# Package updates
147
taro update
148
```
149
150
### Environment Variables
151
152
The CLI recognizes and sets several environment variables:
153
154
```typescript { .api }
155
interface TaroEnvironment {
156
NODE_ENV?: 'development' | 'production' | 'test';
157
TARO_ENV?: string; // Target platform
158
TARO_APP_ID?: string; // Application ID for mini programs
159
}
160
```
161
162
### Platform Detection
163
164
The CLI automatically detects and configures platform-specific settings:
165
166
```typescript { .api }
167
type SupportedPlatform =
168
| 'weapp' // WeChat Mini Program
169
| 'ascf' // Ascend Platform
170
| 'alipay' // Alipay Mini Program
171
| 'swan' // Baidu Smart Program
172
| 'tt' // ByteDance Mini Program
173
| 'qq' // QQ Mini Program
174
| 'jd' // JD Mini Program
175
| 'h5' // HTML5 Web
176
| 'rn' // React Native
177
| 'harmony-hybrid' // Harmony Hybrid
178
| 'plugin'; // Mini Program Plugin
179
```
180
181
### Error Handling
182
183
The CLI provides comprehensive error handling and user feedback:
184
185
```typescript { .api }
186
interface CLIError {
187
code: string;
188
message: string;
189
details?: any;
190
}
191
192
// Common error scenarios
193
type CLIErrorType =
194
| 'INVALID_COMMAND'
195
| 'MISSING_CONFIG'
196
| 'BUILD_FAILED'
197
| 'TEMPLATE_NOT_FOUND'
198
| 'PLATFORM_NOT_SUPPORTED'
199
| 'VERSION_MISMATCH';
200
```