0
# Command Line Interface
1
2
Complete CLI with option parsing for all server configuration options plus path specification and help text.
3
4
## Capabilities
5
6
### Main CLI Entry Point
7
8
Runs the LiveReload CLI application with argument parsing and server startup.
9
10
```javascript { .api }
11
/**
12
* Main CLI entry point that parses arguments and starts server
13
* Uses process.argv for argument parsing
14
*/
15
function run(): void;
16
```
17
18
**Usage Examples:**
19
20
```bash
21
# Basic usage - watch current directory
22
livereload
23
24
# Watch specific directory
25
livereload ./public
26
27
# Watch multiple directories
28
livereload "public, assets, templates"
29
30
# With options
31
livereload ./src -p 8080 -d
32
33
# Complex configuration
34
livereload ./public -e 'html,css,js' -x 'tmp,cache' -w 1000 -d
35
```
36
37
### CLI Argument Parser
38
39
Parses command line arguments and creates server configuration for testing purposes.
40
41
```javascript { .api }
42
/**
43
* Parse CLI arguments and create server (primarily for testing)
44
* @param testArgv - Array of command line arguments
45
* @returns Object with server instance and parsed path
46
*/
47
function createServerFromArgs(testArgv: string[]): {
48
server: Server;
49
path: string | string[];
50
};
51
```
52
53
**Usage Examples:**
54
55
```javascript
56
const { createServerFromArgs } = require('livereload/lib/command');
57
58
// For testing CLI argument parsing
59
const result = createServerFromArgs([
60
'./public',
61
'-p', '8080',
62
'-d'
63
]);
64
65
console.log(result.server.config.port); // 8080
66
console.log(result.path); // ['./public']
67
```
68
69
## CLI Options
70
71
### Basic Options
72
73
Essential CLI options for server configuration.
74
75
```bash
76
# Version information
77
livereload -v
78
livereload --version
79
80
# Port configuration
81
livereload -p 8080
82
livereload --port 8080
83
84
# Host binding
85
livereload -b 0.0.0.0
86
livereload --bind 0.0.0.0
87
88
# Debug mode
89
livereload -d
90
livereload --debug
91
```
92
93
### File Watching Options
94
95
Configure which files are monitored and how.
96
97
```bash
98
# Custom extensions (replaces defaults)
99
livereload -e 'html,css,js'
100
livereload --exts 'html,css,js'
101
102
# Additional extensions (appends to defaults)
103
livereload -ee 'scss,ts,jsx'
104
livereload --extraExts 'scss,ts,jsx'
105
106
# Specific files to reload
107
livereload -f 'index.html,config.js'
108
livereload --filesToReload 'index.html,config.js'
109
110
# Additional exclusion patterns
111
livereload -x 'tmp,cache,logs'
112
livereload --exclusions 'tmp,cache,logs'
113
114
# Use polling for file system changes
115
livereload -u
116
livereload --usepolling
117
118
# Add delay before browser notification
119
livereload -w 1000
120
livereload --wait 1000
121
```
122
123
### Advanced Options
124
125
CORS, CORP, and development URL configuration.
126
127
```bash
128
# Enable Cross-Origin Resource Policy
129
livereload -cp
130
livereload --corp
131
132
# Enable CORS for all origins
133
livereload -cs
134
livereload --cors
135
136
# Enable CORS for specific origin
137
livereload -cs 'https://example.com'
138
livereload --cors 'https://example.com'
139
140
# Set original development URL for proxying
141
livereload -op 'http://localhost:3000'
142
livereload --originalpath 'http://localhost:3000'
143
```
144
145
## Path Specification
146
147
### Single Path
148
149
Monitor a single directory or file.
150
151
```bash
152
# Current directory (default)
153
livereload
154
155
# Specific directory
156
livereload ./public
157
livereload /path/to/project/dist
158
159
# Relative paths
160
livereload ../shared/assets
161
```
162
163
### Multiple Paths
164
165
Monitor multiple directories simultaneously.
166
167
```bash
168
# Comma-separated paths
169
livereload "public, assets, templates"
170
livereload "./src, ./dist, ./config"
171
172
# Can be combined with options
173
livereload "src, dist" -p 8080 -d
174
```
175
176
### Path Resolution
177
178
All paths are resolved to absolute paths internally using `path.resolve()`.
179
180
## CLI Option Details
181
182
### Complete Option Reference
183
184
```javascript { .api }
185
/**
186
* CLI Options Configuration
187
*/
188
interface CLIOptions {
189
// Path argument (optional)
190
path?: string; // Directory to watch (default: '.')
191
192
// Basic options
193
version?: boolean; // -v, --version: Show version
194
port?: number; // -p, --port: Server port (default: 35729)
195
bind?: string; // -b, --bind: Host binding (default: 'localhost')
196
debug?: boolean; // -d, --debug: Enable debug output
197
198
// File watching options
199
exts?: string; // -e, --exts: Extensions (comma-separated)
200
extraExts?: string; // -ee, --extraExts: Additional extensions
201
filesToReload?: string; // -f, --filesToReload: Specific filenames
202
exclusions?: string; // -x, --exclusions: Exclusion patterns
203
usepolling?: boolean; // -u, --usepolling: Use polling mode
204
wait?: number; // -w, --wait: Delay in milliseconds
205
206
// Advanced options
207
corp?: boolean; // -cp, --corp: Enable CORP headers
208
cors?: string | boolean; // -cs, --cors: Enable CORS
209
originalpath?: string; // -op, --originalpath: Development URL
210
}
211
```
212
213
## Usage Examples
214
215
### Development Server
216
217
```bash
218
# Basic development setup
219
livereload ./public -d
220
221
# With custom port and CORS
222
livereload ./dist -p 8080 -cs -d
223
224
# Watch source files with build delay
225
livereload ./src -w 2000 -ee 'ts,scss' -d
226
```
227
228
### Production-like Setup
229
230
```bash
231
# Bind to all interfaces with CORP headers
232
livereload ./public -b 0.0.0.0 -cp
233
234
# With specific CORS origin
235
livereload ./dist -cs 'https://myapp.com' -p 35729
236
```
237
238
### Network/Docker Setup
239
240
```bash
241
# Use polling for network file systems
242
livereload /shared/project -u -d
243
244
# Docker container setup
245
livereload . -b 0.0.0.0 -u -w 1000 -d
246
```
247
248
### Complex Configuration
249
250
```bash
251
# Full-featured development environment
252
livereload ./public \
253
--port 8080 \
254
--bind 0.0.0.0 \
255
--debug \
256
--extraExts 'ts,tsx,scss,vue' \
257
--exclusions 'node_modules,tmp,cache' \
258
--filesToReload 'webpack.config.js,package.json' \
259
--usepolling \
260
--wait 500 \
261
--cors 'http://localhost:3000' \
262
--originalpath 'https://api.myapp.com'
263
```
264
265
## Error Handling
266
267
### Common CLI Errors
268
269
The CLI handles these error conditions:
270
271
```javascript
272
// Port already in use
273
if (err.code === "EADDRINUSE") {
274
console.log("The port LiveReload wants to use is used by something else.");
275
process.exit(1);
276
}
277
278
// Other server errors
279
process.on('uncaughtException', (err) => {
280
console.error('Server error:', err);
281
process.exit(1);
282
});
283
```
284
285
### Option Validation
286
287
Invalid options are handled by the `opts` library:
288
289
- Invalid port numbers are converted to NaN, then default to 35729
290
- Invalid regex patterns in exclusions may cause startup errors
291
- Comma-separated values are split and trimmed automatically
292
293
## Integration with Package Scripts
294
295
### npm scripts
296
297
```json
298
{
299
"scripts": {
300
"dev": "livereload ./public -d",
301
"dev:watch": "livereload ./src -ee 'ts,scss' -w 1000",
302
"dev:network": "livereload . -b 0.0.0.0 -u -d"
303
}
304
}
305
```
306
307
### Environment Variables
308
309
While not directly supported, you can use environment variables in npm scripts:
310
311
```json
312
{
313
"scripts": {
314
"dev": "livereload ./public -p ${PORT:-35729} -d"
315
}
316
}
317
```
318
319
## Exit Codes
320
321
- **0**: Normal termination
322
- **1**: Error condition (port in use, invalid arguments, etc.)
323
324
## Output Format
325
326
With debug mode enabled (`-d` or `--debug`), the CLI outputs:
327
328
```
329
Starting LiveReload v0.10.3 for /path/to/directory on localhost:35729.
330
LiveReload is waiting for a browser to connect...
331
Protocol version: 7
332
Exclusions: /\.git\//,/\.svn\//,/\.hg\//
333
Extensions: html,css,js,png,gif,jpg,php,php5,py,rb,erb,coffee
334
Polling: false
335
```