0
# CLI Integration
1
2
Command-line interface integration allowing programmatic access to CLI functionality with full argument processing and configuration file support.
3
4
## Capabilities
5
6
### Main CLI Function
7
8
Processes command-line arguments and executes clone detection with CLI-style behavior.
9
10
```typescript { .api }
11
/**
12
* CLI entry point function that processes command line arguments
13
* @param argv - Command line arguments array (typically process.argv)
14
* @param exitCallback - Optional callback for process exit with exit code
15
* @returns Promise resolving to array of detected clones
16
*/
17
function jscpd(
18
argv: string[],
19
exitCallback?: (code: number) => {}
20
): Promise<IClone[]>;
21
```
22
23
**Usage Examples:**
24
25
```typescript
26
import { jscpd } from "jscpd";
27
28
// Basic programmatic CLI usage
29
const clones = await jscpd([
30
"node", "jscpd", "./src"
31
]);
32
33
// With CLI options
34
const clones = await jscpd([
35
"node", "jscpd",
36
"./src", "./lib",
37
"--min-lines", "5",
38
"--min-tokens", "50",
39
"--format", "javascript,typescript",
40
"--reporters", "console,json",
41
"--output", "./reports",
42
"--threshold", "10"
43
]);
44
45
// With exit callback for CI integration
46
const clones = await jscpd([
47
"node", "jscpd", "./src",
48
"--threshold", "5"
49
], (exitCode) => {
50
console.log(`JSCPD finished with exit code: ${exitCode}`);
51
process.exit(exitCode);
52
});
53
```
54
55
### CLI Initialization
56
57
Creates and configures Commander.js CLI with all available options.
58
59
```typescript { .api }
60
/**
61
* Initializes Commander.js CLI with all jscpd options
62
* @param packageJson - Package metadata for version and description
63
* @param argv - Command line arguments array
64
* @returns Configured Commander instance
65
*/
66
function initCli(packageJson: any, argv: string[]): Command;
67
```
68
69
**Usage Example:**
70
71
```typescript
72
import { initCli } from "jscpd";
73
import { readJSONSync } from "fs-extra";
74
75
const packageJson = readJSONSync("./package.json");
76
const cli = initCli(packageJson, process.argv);
77
78
// Access parsed CLI options
79
console.log("Min lines:", cli.minLines);
80
console.log("Format:", cli.format);
81
console.log("Paths:", cli.args);
82
```
83
84
## CLI Options Reference
85
86
All CLI options available through the command-line interface:
87
88
### File Selection Options
89
90
```bash
91
# Specify paths to analyze
92
jscpd /path/to/source /another/path
93
94
# Use glob patterns
95
jscpd --pattern "src/**/*.js"
96
jscpd -p "**/*.{js,ts}"
97
98
# Specify file formats
99
jscpd --format javascript,typescript
100
jscpd -f "javascript,python"
101
102
# Custom format extensions
103
jscpd --formats-exts "javascript:es,es6;dart:dt"
104
```
105
106
### Detection Thresholds
107
108
```bash
109
# Minimum lines for duplication
110
jscpd --min-lines 5
111
jscpd -l 5
112
113
# Minimum tokens for duplication
114
jscpd --min-tokens 50
115
jscpd -k 50
116
117
# Maximum lines to analyze
118
jscpd --max-lines 1000
119
jscpd -x 1000
120
121
# Maximum file size
122
jscpd --max-size 1mb
123
jscpd -z 120kb
124
125
# Detection quality mode
126
jscpd --mode strict
127
jscpd -m mild
128
```
129
130
### Exclusion Options
131
132
```bash
133
# Ignore files by glob pattern
134
jscpd --ignore "**/*.test.js,**/node_modules/**"
135
jscpd -i "**/*.spec.ts"
136
137
# Ignore code blocks by regex
138
jscpd --ignore-pattern "DEBUG,TODO,FIXME"
139
140
# Respect .gitignore
141
jscpd --gitignore
142
jscpd -g
143
144
# Skip local folder duplicates
145
jscpd --skipLocal
146
```
147
148
### Output Options
149
150
```bash
151
# Specify reporters
152
jscpd --reporters console,html,json
153
jscpd -r time,console
154
155
# Output directory
156
jscpd --output ./reports
157
jscpd -o /tmp/jscpd-reports
158
159
# Use absolute paths
160
jscpd --absolute
161
jscpd -a
162
163
# Silent mode (no console output)
164
jscpd --silent
165
jscpd -s
166
167
# Verbose output
168
jscpd --verbose
169
jscpd -v
170
171
# Debug mode
172
jscpd --debug
173
jscpd -d
174
```
175
176
### Advanced Options
177
178
```bash
179
# Configuration file
180
jscpd --config .jscpd.json
181
jscpd -c custom-config.json
182
183
# Custom store for large codebases
184
jscpd --store leveldb
185
186
# Git blame integration
187
jscpd --blame
188
jscpd -b
189
190
# Threshold for exit code
191
jscpd --threshold 10
192
jscpd -t 5
193
194
# Custom exit code
195
jscpd --exitCode 2
196
197
# Case insensitive (experimental)
198
jscpd --ignoreCase
199
200
# Avoid symlinks
201
jscpd --noSymlinks
202
203
# List supported formats
204
jscpd --list
205
```
206
207
### Help and Information
208
209
```bash
210
# Show help
211
jscpd --help
212
jscpd -h
213
214
# Show version
215
jscpd --version
216
jscpd -V
217
218
# List all supported formats
219
jscpd --list
220
```
221
222
## Configuration Files
223
224
JSCPD supports multiple configuration sources with priority order:
225
226
1. **CLI arguments** (highest priority)
227
2. **Config file** (`.jscpd.json` or specified via `--config`)
228
3. **package.json** (in `jscpd` property)
229
4. **Default values** (lowest priority)
230
231
### Config File Format
232
233
```json
234
{
235
"minLines": 5,
236
"minTokens": 50,
237
"threshold": 10,
238
"reporters": ["console", "html"],
239
"ignore": [
240
"**/*.test.js",
241
"**/node_modules/**"
242
],
243
"output": "./reports",
244
"format": [
245
"javascript",
246
"typescript"
247
],
248
"absolute": true,
249
"gitignore": true
250
}
251
```
252
253
### Package.json Configuration
254
255
```json
256
{
257
"name": "my-project",
258
"jscpd": {
259
"threshold": 5,
260
"reporters": ["html"],
261
"ignore": ["**/*.test.js"]
262
}
263
}
264
```
265
266
## Exit Codes
267
268
- **0**: No duplications found or duplications below threshold
269
- **1**: General error (invalid options, file not found, etc.)
270
- **Custom exit code**: When duplications exceed threshold (configurable via `--exitCode`)
271
272
## Integration Examples
273
274
### CI/CD Integration
275
276
```bash
277
#!/bin/bash
278
# CI script example
279
set -e
280
281
echo "Running jscpd analysis..."
282
jscpd ./src --threshold 5 --reporters console,json --output ./reports
283
284
if [ $? -eq 0 ]; then
285
echo "✅ Code duplication check passed"
286
else
287
echo "❌ Code duplication exceeded threshold"
288
exit 1
289
fi
290
```
291
292
### NPM Scripts
293
294
```json
295
{
296
"scripts": {
297
"jscpd": "jscpd ./src",
298
"jscpd:ci": "jscpd ./src --threshold 5 --reporters json --output ./reports",
299
"jscpd:html": "jscpd ./src --reporters html --output ./reports && open ./reports/html/index.html"
300
}
301
}
302
```