0
# Code Formatting
1
2
Prettier integration with automatic configuration discovery and file-first configuration precedence for consistent code formatting.
3
4
## Capabilities
5
6
### lb-prettier Command
7
8
Runs Prettier with automatic configuration discovery and sensible defaults.
9
10
```typescript { .api }
11
/**
12
* Prettier formatter function with configuration discovery
13
* @param argv - Command line arguments including files and options
14
* @param options - Execution options for dry run and process control
15
* @returns ChildProcess when executed, string when dry run
16
*/
17
function prettier(argv: string[], options?: RunOptions): ChildProcess | string;
18
```
19
20
**CLI Usage:**
21
22
```bash
23
# Format files
24
lb-prettier "**/*.ts" "**/*.js"
25
26
# Check formatting without changes
27
lb-prettier --check "**/*.ts"
28
29
# List files that would be formatted
30
lb-prettier --list-different "**/*.ts"
31
32
# Write formatted files
33
lb-prettier --write "**/*.ts" "**/*.js"
34
35
# Format specific files
36
lb-prettier src/index.ts src/utils.ts
37
```
38
39
**Programmatic Usage:**
40
41
```typescript
42
import { prettier } from "@loopback/build";
43
44
// Format TypeScript files
45
const child = prettier(["--write", "**/*.ts"]);
46
47
// Dry run to see command
48
const command = prettier(["--check", "src/"], { dryRun: true });
49
console.log(command); // Shows the Prettier command that would be executed
50
51
// Format with custom working directory
52
prettier(["--write", "src/"], { cwd: "/path/to/project" });
53
```
54
55
### Configuration Discovery
56
57
Automatically discovers Prettier configuration files with file-first precedence.
58
59
**Configuration Search Order:**
60
1. Project-specific configuration (if `--config`, `--no-config`, or `--find-config-path` not provided)
61
2. `.prettierrc` in project root
62
3. Falls back to `@loopback/build/config/.prettierrc`
63
64
**Configuration Precedence:**
65
Uses `--config-precedence prefer-file` to prioritize project configuration files over CLI options.
66
67
### Default Configuration
68
69
Provides sensible defaults for LoopBack and TypeScript projects.
70
71
```typescript { .api }
72
// @loopback/build/config/.prettierrc contents:
73
interface PrettierConfig {
74
bracketSpacing: false; // {foo} instead of { foo }
75
singleQuote: true; // 'string' instead of "string"
76
printWidth: 80; // Line wrap at 80 characters
77
trailingComma: "all"; // Trailing commas everywhere
78
arrowParens: "avoid"; // x => x instead of (x) => x
79
}
80
```
81
82
### File Pattern Support
83
84
Supports glob patterns and specific file targeting.
85
86
```typescript { .api }
87
// Glob patterns for multiple files
88
// Specific file paths
89
// Supports all file types that Prettier handles
90
```
91
92
**Common File Patterns:**
93
94
```bash
95
# TypeScript and JavaScript
96
lb-prettier "**/*.{ts,js}"
97
98
# All supported formats
99
lb-prettier "**/*.{ts,js,json,md,yml,yaml}"
100
101
# Specific directories
102
lb-prettier "src/**/*.ts" "test/**/*.ts"
103
```
104
105
### Command Line Option Passthrough
106
107
All Prettier command line options are supported and passed through.
108
109
```typescript { .api }
110
interface PrettierOptions {
111
"--write": boolean; // Write formatted files to disk
112
"--check": boolean; // Check if files are formatted
113
"--list-different": boolean; // List files that differ from Prettier formatting
114
"--config": string; // Path to configuration file
115
"--no-config": boolean; // Do not look for configuration file
116
"--find-config-path": string; // Find and return path to configuration file
117
"--ignore-path": string; // Path to ignore file
118
"--no-ignore": boolean; // Do not look for ignore files
119
"--with-node-modules": boolean; // Process files in node_modules
120
"--parser": string; // Parser to use (auto-detected by default)
121
"--print-width": number; // Line wrap width
122
"--tab-width": number; // Tab width
123
"--use-tabs": boolean; // Use tabs instead of spaces
124
"--semi": boolean; // Print semicolons
125
"--single-quote": boolean; // Use single quotes
126
"--quote-props": string; // Quote object properties
127
"--jsx-single-quote": boolean; // Use single quotes in JSX
128
"--trailing-comma": string; // Trailing comma policy
129
"--bracket-spacing": boolean; // Print spaces inside object brackets
130
"--bracket-same-line": boolean; // Put closing bracket on same line
131
"--arrow-parens": string; // Arrow function parentheses policy
132
"--range-start": number; // Format from this character
133
"--range-end": number; // Format to this character
134
"--stdin-filepath": string; // File path for stdin input
135
"--end-of-line": string; // Line ending style
136
}
137
```
138
139
### Usage Examples
140
141
**Basic Formatting:**
142
143
```typescript
144
import { prettier } from "@loopback/build";
145
146
// Format all TypeScript files
147
prettier(["--write", "**/*.ts"], { dryRun: false });
148
149
// Check formatting without changes
150
prettier(["--check", "src/"], { dryRun: false });
151
```
152
153
**Package.json Integration:**
154
155
```json
156
{
157
"scripts": {
158
"prettier:cli": "lb-prettier \"**/*.ts\" \"**/*.js\"",
159
"prettier:check": "npm run prettier:cli -- --list-different",
160
"prettier:fix": "npm run prettier:cli -- --write",
161
"format": "npm run prettier:fix"
162
}
163
}
164
```
165
166
**Custom Configuration:**
167
168
```bash
169
# Use custom config file
170
lb-prettier --config .prettierrc.custom.json --write "**/*.ts"
171
172
# Ignore configuration files
173
lb-prettier --no-config --write --single-quote "**/*.ts"
174
175
# Custom ignore file
176
lb-prettier --ignore-path .prettierignore.custom --write "**/*.ts"
177
```
178
179
### Integration with Other Tools
180
181
Works seamlessly with ESLint and other development tools.
182
183
**ESLint Integration:**
184
- LoopBack ESLint config is compatible with default Prettier settings
185
- No conflicting rules between ESLint and Prettier formatting
186
187
**Editor Integration:**
188
- Configuration files are automatically detected by editors
189
- Supports format-on-save workflows
190
- Compatible with VS Code, WebStorm, and other editors
191
192
### Error Handling
193
194
Prettier errors are properly handled and reported.
195
196
```typescript { .api }
197
// Syntax errors in files are reported with file location
198
// Configuration errors show helpful error messages
199
// Process exits with non-zero code on formatting errors (when using --check)
200
// Invalid file patterns are reported clearly
201
```