CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jscpd

Copy/paste detector for source code that supports 150+ programming languages and provides both CLI and programmatic APIs

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

cli-integration.mddocs/

CLI Integration

Command-line interface integration allowing programmatic access to CLI functionality with full argument processing and configuration file support.

Capabilities

Main CLI Function

Processes command-line arguments and executes clone detection with CLI-style behavior.

/**
 * CLI entry point function that processes command line arguments
 * @param argv - Command line arguments array (typically process.argv)
 * @param exitCallback - Optional callback for process exit with exit code
 * @returns Promise resolving to array of detected clones
 */
function jscpd(
  argv: string[], 
  exitCallback?: (code: number) => {}
): Promise<IClone[]>;

Usage Examples:

import { jscpd } from "jscpd";

// Basic programmatic CLI usage
const clones = await jscpd([
  "node", "jscpd", "./src"
]);

// With CLI options
const clones = await jscpd([
  "node", "jscpd", 
  "./src", "./lib",
  "--min-lines", "5",
  "--min-tokens", "50", 
  "--format", "javascript,typescript",
  "--reporters", "console,json",
  "--output", "./reports",
  "--threshold", "10"
]);

// With exit callback for CI integration
const clones = await jscpd([
  "node", "jscpd", "./src",
  "--threshold", "5"
], (exitCode) => {
  console.log(`JSCPD finished with exit code: ${exitCode}`);
  process.exit(exitCode);
});

CLI Initialization

Creates and configures Commander.js CLI with all available options.

/**
 * Initializes Commander.js CLI with all jscpd options
 * @param packageJson - Package metadata for version and description
 * @param argv - Command line arguments array
 * @returns Configured Commander instance
 */
function initCli(packageJson: any, argv: string[]): Command;

Usage Example:

import { initCli } from "jscpd";
import { readJSONSync } from "fs-extra";

const packageJson = readJSONSync("./package.json");
const cli = initCli(packageJson, process.argv);

// Access parsed CLI options
console.log("Min lines:", cli.minLines);
console.log("Format:", cli.format);
console.log("Paths:", cli.args);

CLI Options Reference

All CLI options available through the command-line interface:

File Selection Options

# Specify paths to analyze
jscpd /path/to/source /another/path

# Use glob patterns
jscpd --pattern "src/**/*.js"
jscpd -p "**/*.{js,ts}"

# Specify file formats
jscpd --format javascript,typescript
jscpd -f "javascript,python"

# Custom format extensions
jscpd --formats-exts "javascript:es,es6;dart:dt"

Detection Thresholds

# Minimum lines for duplication
jscpd --min-lines 5
jscpd -l 5

# Minimum tokens for duplication  
jscpd --min-tokens 50
jscpd -k 50

# Maximum lines to analyze
jscpd --max-lines 1000
jscpd -x 1000

# Maximum file size
jscpd --max-size 1mb
jscpd -z 120kb

# Detection quality mode
jscpd --mode strict
jscpd -m mild

Exclusion Options

# Ignore files by glob pattern
jscpd --ignore "**/*.test.js,**/node_modules/**"
jscpd -i "**/*.spec.ts"

# Ignore code blocks by regex
jscpd --ignore-pattern "DEBUG,TODO,FIXME"

# Respect .gitignore
jscpd --gitignore
jscpd -g

# Skip local folder duplicates
jscpd --skipLocal

Output Options

# Specify reporters
jscpd --reporters console,html,json
jscpd -r time,console

# Output directory
jscpd --output ./reports
jscpd -o /tmp/jscpd-reports

# Use absolute paths
jscpd --absolute
jscpd -a

# Silent mode (no console output)
jscpd --silent
jscpd -s

# Verbose output
jscpd --verbose
jscpd -v

# Debug mode
jscpd --debug
jscpd -d

Advanced Options

# Configuration file
jscpd --config .jscpd.json
jscpd -c custom-config.json

# Custom store for large codebases
jscpd --store leveldb

# Git blame integration
jscpd --blame
jscpd -b

# Threshold for exit code
jscpd --threshold 10
jscpd -t 5

# Custom exit code
jscpd --exitCode 2

# Case insensitive (experimental)
jscpd --ignoreCase

# Avoid symlinks
jscpd --noSymlinks

# List supported formats
jscpd --list

Help and Information

# Show help
jscpd --help
jscpd -h

# Show version
jscpd --version
jscpd -V

# List all supported formats
jscpd --list

Configuration Files

JSCPD supports multiple configuration sources with priority order:

  1. CLI arguments (highest priority)
  2. Config file (.jscpd.json or specified via --config)
  3. package.json (in jscpd property)
  4. Default values (lowest priority)

Config File Format

{
  "minLines": 5,
  "minTokens": 50,
  "threshold": 10,
  "reporters": ["console", "html"],
  "ignore": [
    "**/*.test.js",
    "**/node_modules/**"
  ],
  "output": "./reports",
  "format": [
    "javascript",
    "typescript"
  ],
  "absolute": true,
  "gitignore": true
}

Package.json Configuration

{
  "name": "my-project",
  "jscpd": {
    "threshold": 5,
    "reporters": ["html"],
    "ignore": ["**/*.test.js"]
  }
}

Exit Codes

  • 0: No duplications found or duplications below threshold
  • 1: General error (invalid options, file not found, etc.)
  • Custom exit code: When duplications exceed threshold (configurable via --exitCode)

Integration Examples

CI/CD Integration

#!/bin/bash
# CI script example
set -e

echo "Running jscpd analysis..."
jscpd ./src --threshold 5 --reporters console,json --output ./reports

if [ $? -eq 0 ]; then
  echo "✅ Code duplication check passed"
else
  echo "❌ Code duplication exceeded threshold"
  exit 1
fi

NPM Scripts

{
  "scripts": {
    "jscpd": "jscpd ./src",
    "jscpd:ci": "jscpd ./src --threshold 5 --reporters json --output ./reports", 
    "jscpd:html": "jscpd ./src --reporters html --output ./reports && open ./reports/html/index.html"
  }
}

docs

cli-integration.md

clone-detection.md

configuration.md

index.md

tile.json