0
# CLI Interface
1
2
Command-line interface providing comprehensive options for formatting, linting, and processing files. The CLI supports various processing modes including staged files, changed files, and stdin processing.
3
4
## Capabilities
5
6
### Basic Command Usage
7
8
The `prettier-standard` command processes files according to specified options and patterns.
9
10
```bash { .api }
11
# Basic syntax
12
prettier-standard [<glob>]
13
14
# Examples
15
prettier-standard # Process all files in current repository
16
prettier-standard '**/*.js' # Process all JavaScript files
17
prettier-standard 'src/**/*.{js,ts}' # Process JS and TS files in src directory
18
prettier-standard --help # Show help information
19
```
20
21
### Command Options
22
23
#### Format Options
24
25
```bash { .api }
26
--format # Format files without linting (default behavior without --lint)
27
--lint # Additionally lint code after formatting using ESLint Standard rules
28
--check # Check formatting without modifying files, exit 1 if changes needed
29
```
30
31
**Usage Examples:**
32
33
```bash
34
# Just format files
35
prettier-standard --format '**/*.js'
36
37
# Format and lint
38
prettier-standard --lint '**/*.js'
39
40
# Check if files need formatting (CI/CD usage)
41
prettier-standard --check '**/*.js'
42
if [ $? -eq 0 ]; then
43
echo "All files are properly formatted"
44
else
45
echo "Some files need formatting"
46
fi
47
```
48
49
#### Git Integration Options
50
51
```bash { .api }
52
--staged # Run only on Git staged files (useful for pre-commit hooks)
53
--changed # Run only on files changed since HEAD
54
--since <rev> # Run only on files changed since given revision/branch
55
--lines # Run only on changed lines within files (experimental)
56
```
57
58
**Usage Examples:**
59
60
```bash
61
# Process staged files (pre-commit hook)
62
prettier-standard --lint --staged
63
64
# Process files changed since HEAD
65
prettier-standard --changed --lint
66
67
# Process files changed since master branch
68
prettier-standard --since master --lint
69
70
# Process files changed since specific commit
71
prettier-standard --since abc123 --lint
72
73
# Process only changed lines (experimental)
74
prettier-standard --lines --changed
75
```
76
77
#### Stdin Processing Options
78
79
```bash { .api }
80
--stdin # Force reading input from stdin
81
--stdin-filepath <path> # Specify filename for stdin (affects config resolution)
82
--parser <parser> # Force parser for stdin input (default: babel)
83
```
84
85
**Usage Examples:**
86
87
```bash
88
# Format code from stdin
89
echo 'function hello(){return"world";}' | prettier-standard --stdin
90
91
# Format with explicit parser
92
echo '.foo { color: "red"; }' | prettier-standard --parser css --stdin
93
94
# Format stdin with config resolution based on fake filename
95
echo 'const x:number=5' | prettier-standard --stdin --stdin-filepath fake.ts
96
97
# Check stdin formatting
98
echo 'function hello () { return "world" }' | prettier-standard --stdin --check
99
```
100
101
### Package.json Integration
102
103
#### Scripts Integration
104
105
Common patterns for integrating with npm scripts:
106
107
```json
108
{
109
"scripts": {
110
"format": "prettier-standard --format",
111
"format:check": "prettier-standard --check",
112
"lint": "prettier-standard --lint",
113
"precommit": "prettier-standard --lint --staged"
114
}
115
}
116
```
117
118
#### Lint-staged Integration
119
120
Using with lint-staged for pre-commit hooks:
121
122
```json
123
{
124
"scripts": {
125
"precommit": "lint-staged"
126
},
127
"lint-staged": {
128
"*.{js,ts,jsx,tsx}": ["prettier-standard --lint"],
129
"*.{json,css,md}": ["prettier-standard --format"]
130
}
131
}
132
```
133
134
#### Husky Integration
135
136
Complete pre-commit setup with Husky:
137
138
```json
139
{
140
"scripts": {
141
"prepare": "husky install"
142
},
143
"lint-staged": {
144
"*": ["prettier-standard --lint"]
145
}
146
}
147
```
148
149
Create `.husky/pre-commit`:
150
```bash
151
#!/bin/sh
152
. "$(dirname "$0")/_/husky.sh"
153
154
npx lint-staged
155
```
156
157
### Exit Codes
158
159
The CLI uses standard exit codes for different outcomes:
160
161
```bash { .api }
162
# Exit code 0: Success
163
prettier-standard --check '**/*.js' # All files properly formatted
164
prettier-standard --lint '**/*.js' # No formatting changes and no lint errors
165
166
# Exit code 1: Formatting or linting issues
167
prettier-standard --check '**/*.js' # Some files need formatting
168
prettier-standard --lint '**/*.js' # Lint errors found
169
170
# Exit code 2: Execution errors
171
prettier-standard --invalid-option # Invalid command line options
172
prettier-standard 'invalid-glob[' # Invalid glob pattern
173
```
174
175
**CI/CD Usage:**
176
177
```bash
178
# Fail build if formatting is needed
179
prettier-standard --check '**/*.js' || exit 1
180
181
# Fail build if linting errors exist
182
prettier-standard --lint '**/*.js' || exit 1
183
184
# Combined check
185
prettier-standard --check --lint '**/*.js' && echo "Code quality checks passed"
186
```
187
188
### Configuration Files
189
190
#### Prettier Configuration
191
192
The CLI respects `.prettierrc` files for formatting options:
193
194
```json
195
{
196
"semi": true,
197
"singleQuote": false,
198
"tabWidth": 4
199
}
200
```
201
202
**Supported formats:**
203
- `.prettierrc` (JSON)
204
- `.prettierrc.json`
205
- `.prettierrc.js`
206
- `prettier.config.js`
207
- `package.json` (prettier field)
208
209
#### ESLint Configuration
210
211
When using `--lint`, ESLint configuration is read from:
212
213
```json
214
{
215
"rules": {
216
"eqeqeq": "off",
217
"no-console": "warn"
218
},
219
"env": {
220
"node": true,
221
"browser": true
222
}
223
}
224
```
225
226
**Supported formats:**
227
- `.eslintrc.json`
228
- `.eslintrc.js`
229
- `.eslintrc` (JSON)
230
- `package.json` (eslintConfig field)
231
232
#### Ignore Files
233
234
**`.prettierignore`** - Files to exclude from formatting:
235
```
236
dist/
237
build/
238
**/*.min.js
239
coverage/
240
node_modules/
241
```
242
243
**`.eslintignore`** - Files to format but exclude from linting:
244
```
245
dist/
246
build/
247
**/*.min.js
248
coverage/
249
```
250
251
### Editor Integration
252
253
#### Vim Integration
254
255
Using with ALE plugin:
256
257
```vim
258
Plug 'w0rp/ale'
259
let g:ale_fixers = {'javascript': ['prettier_standard']}
260
let g:ale_linters = {'javascript': ['']}
261
let g:ale_fix_on_save = 1
262
```
263
264
#### Sublime Text 3 Integration
265
266
1. Install prettier-standard globally: `npm install -g prettier-standard`
267
2. Find installation path: `which prettier-standard`
268
3. Install SublimeJsPrettier plugin
269
4. Configure in Sublime settings:
270
271
```json
272
{
273
"prettier_cli_path": "/usr/local/bin/prettier-standard"
274
}
275
```
276
277
5. Use Command Palette: `JsPrettier: Format Code`
278
279
#### VS Code Integration
280
281
Create `.vscode/settings.json`:
282
283
```json
284
{
285
"editor.formatOnSave": true,
286
"editor.defaultFormatter": "esbenp.prettier-vscode",
287
"prettier.prettierPath": "./node_modules/prettier-standard"
288
}
289
```
290
291
### Performance Tips
292
293
#### Glob Pattern Optimization
294
295
```bash
296
# Efficient: Specific patterns
297
prettier-standard 'src/**/*.{js,ts}'
298
299
# Less efficient: Overly broad patterns
300
prettier-standard '**/*'
301
302
# Most efficient: Process only changed files
303
prettier-standard --changed --lint
304
```
305
306
#### Large Repository Handling
307
308
```bash
309
# For large repos, use changed files
310
prettier-standard --since master --lint
311
312
# For staged files in pre-commit
313
prettier-standard --staged --lint
314
315
# Process specific directories
316
prettier-standard 'apps/frontend/src/**/*.js' --lint
317
```
318
319
### Error Messages and Troubleshooting
320
321
#### Common Error Messages
322
323
```bash
324
# Invalid patterns
325
Error: Unable to expand glob pattern: Invalid glob pattern
326
327
# Git repository required
328
Error: No git repository detected...
329
330
# Syntax errors in files
331
SyntaxError: Unexpected token
332
333
# Module not found (internal error)
334
Error: Module not found: eslint-config-standard
335
```
336
337
#### Debugging Options
338
339
While there are no explicit debug flags, you can use verbose output:
340
341
```bash
342
# Check which files would be processed
343
prettier-standard --check '**/*.js' 2>&1 | grep -E '\.(js|ts|jsx|tsx)$'
344
345
# Test with a single file first
346
prettier-standard --check 'path/to/single/file.js'
347
348
# Use explicit patterns instead of defaults
349
prettier-standard 'src/**/*.js' 'test/**/*.js' --lint
350
```
351
352
#### Common Issues and Solutions
353
354
**Issue**: Files in node_modules being processed
355
```bash
356
# Solution: Use explicit patterns or check .prettierignore
357
prettier-standard 'src/**/*.js' 'lib/**/*.js'
358
```
359
360
**Issue**: TypeScript files not being processed
361
```bash
362
# Solution: Include TypeScript patterns
363
prettier-standard '**/*.{js,ts,jsx,tsx}' --lint
364
```
365
366
**Issue**: Pre-commit hook failing
367
```bash
368
# Solution: Ensure staged files are committed after formatting
369
git add .
370
prettier-standard --staged --lint
371
```