### Quick Start cpd Usage Source: https://github.com/kucherenko/jscpd/blob/master/rust/README.md Basic command-line examples for running the cpd tool on directories and with various options. ```bash cpd . ``` ```bash cpd ./src ./lib ``` ```bash cpd . --blame --reporters console-full ``` ```bash cpd . --reporters json,html ``` ```bash cpd . --threshold 10 ``` ```bash cpd --list ``` -------------------------------- ### Install Dependencies Source: https://github.com/kucherenko/jscpd/blob/master/README.md Install project dependencies using pnpm. ```bash pnpm install ``` -------------------------------- ### Install @jscpd/tokenizer Source: https://github.com/kucherenko/jscpd/blob/master/packages/tokenizer/README.md Install the tokenizer package using npm. ```bash npm install @jscpd/tokenizer --save ``` -------------------------------- ### Install @jscpd/redis-store Source: https://github.com/kucherenko/jscpd/blob/master/packages/redis-store/README.md Install the redis-store package using npm. ```bash npm install @jscpd/redis-store --save ``` -------------------------------- ### Configuration File Example (.cpd.yaml) Source: https://github.com/kucherenko/jscpd/blob/master/fixtures/markdown/file2.md Example configuration file defining languages, file patterns, exclusions, reporters, and language extensions. ```yaml #.cpd.yaml languages: - javascript - coffeescript - typescript - php - python - jsx - haxe - yaml - css - ruby - go - swift - twig - java - clike # c++, c, objective-c source - csharp # c# source - htmlmixed # html mixed source like knockout.js templates files: - "test/**/*" exclude: - "**/*.min.js" - "**/*.mm.js" reporter: json languages-exts: coffeescript: - coffeee javascript: - es - es5 - es6 - es7 ``` -------------------------------- ### cpd configuration file example Source: https://github.com/kucherenko/jscpd/blob/master/rust/npm/README.md An example of a `.jscpd.json` configuration file, specifying detection parameters, formats, reporters, and output settings. ```json { "minTokens": 30, "minLines": 3, "format": ["javascript", "typescript", "python"], "ignorePattern": ["node_modules", "dist", "*.min.js"], "reporters": ["console", "json"], "output": "report", "threshold": 5, "blame": false } ``` -------------------------------- ### Install @jscpd/badge-reporter Source: https://github.com/kucherenko/jscpd/blob/master/packages/badge-reporter/README.md Install the badge reporter package using npm. ```bash npm install @jscpd/badge-reporter ``` -------------------------------- ### Install @jscpd/leveldb-store Source: https://github.com/kucherenko/jscpd/blob/master/packages/leveldb-store/README.md Install the package using npm. ```bash npm install @jscpd/leveldb-store --save ``` -------------------------------- ### Install @jscpd/html-reporter Source: https://github.com/kucherenko/jscpd/blob/master/packages/html-reporter/README.md Install the HTML reporter package using npm. ```bash npm install @jscpd/html-reporter ``` -------------------------------- ### Install SARIF Reporter Source: https://github.com/kucherenko/jscpd/blob/master/packages/sarif-reporter/README.md Install the SARIF reporter package using npm. ```bash npm install @jscpd/sarif-reporter ``` -------------------------------- ### Install cpd via npm Source: https://github.com/kucherenko/jscpd/blob/master/rust/README.md Install the cpd command-line tool using npm. Choose to install both jscpd and cpd, or only the cpd command. ```bash npm install -g jscpd ``` ```bash npm install -g cpd ``` -------------------------------- ### Install @jscpd/core Source: https://github.com/kucherenko/jscpd/blob/master/packages/core/README.md Install the @jscpd/core package using npm. ```bash npm install @jscpd/core --save ``` -------------------------------- ### jscpd Configuration File Example Source: https://github.com/kucherenko/jscpd/blob/master/docs/rust.md An example of a `.jscpd.json` configuration file, specifying paths, reporters, detection thresholds, and format filtering. ```json { "path": ["./src"], "reporters": ["console", "json"], "minLines": 5, "minTokens": 50, "threshold": 0, "format": ["javascript", "typescript"], "ignore": ["**/node_modules/**"], "gitignore": true, "mode": "mild" } ``` -------------------------------- ### Shell Script Example Source: https://github.com/kucherenko/jscpd/blob/master/packages/tokenizer/__tests__/fixtures/markdown/synonym-langs.md Basic shell commands for demonstration. ```shell echo "hello world" ls -la /tmp cd /home ``` -------------------------------- ### Install @jscpd/finder Source: https://github.com/kucherenko/jscpd/blob/master/packages/finder/README.md Install the @jscpd/finder package using npm. ```bash npm install @jscpd/finder --save ``` -------------------------------- ### Run in Development Mode Source: https://github.com/kucherenko/jscpd/blob/master/README.md Start the project in development mode using pnpm. ```bash pnpm dev ``` -------------------------------- ### Install jscpd Globally or Use npx Source: https://github.com/kucherenko/jscpd/blob/master/docs/typescript.md Install the jscpd command-line tool globally using npm or run it directly without installation using npx. ```bash # npm npm install -g jscpd # npx (no install required) npx jscpd /path/to/code ``` -------------------------------- ### Usage Example Source: https://github.com/kucherenko/jscpd/blob/master/packages/badge-reporter/README.md Run jscpd with the badge reporter and specify the source path. ```bash jscpd [...options] --reporters badge /path/to/source ``` -------------------------------- ### Basic cpd usage examples Source: https://github.com/kucherenko/jscpd/blob/master/rust/npm/README.md Demonstrates scanning the current directory, specific paths, and setting minimum tokens/lines for duplicate detection. ```bash # Scan current directory cpd . # Scan specific paths cpd ./src ./lib # Minimum tokens/lines for a clone cpd . --min-tokens 30 --min-lines 3 ``` -------------------------------- ### Start JSCPD Server with Persistent Storage Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Starts the JSCPD server with persistent storage using LevelDB, recommended for production environments. ```bash jscpd-server . --store leveldb ``` -------------------------------- ### Start JSCPD Server with Custom Host and Port Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Starts the JSCPD server with a custom host and port, scanning the current directory. ```bash jscpd-server . --host localhost --port 3000 ``` -------------------------------- ### Install cpd using npm Source: https://github.com/kucherenko/jscpd/blob/master/rust/npm/README.md Installs the jscpd package globally, making both 'jscpd' and 'cpd' commands available. Alternatively, install only the 'cpd' command. ```bash # installs both jscpd and cpd commands npm install -g jscpd # installs only the cpd command npm install -g cpd ``` -------------------------------- ### Start jscpd-server Source: https://github.com/kucherenko/jscpd/blob/master/docs/ai-ready.md Start the jscpd-server, optionally specifying port, host, or storage options. This makes jscpd accessible as an MCP tool. ```bash jscpd-server /path/to/project ``` -------------------------------- ### Install LevelDB Store Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Installs the necessary npm package for enabling persistent storage with LevelDB. ```bash # Install the LevelDB store package npm install @jscpd/leveldb-store ``` -------------------------------- ### Install and Run jscpd (TypeScript Engine) Source: https://github.com/kucherenko/jscpd/blob/master/README.md Install the TypeScript engine globally using npm and run jscpd on a directory. Alternatively, use npx to run without global installation. ```bash # TypeScript engine (Node.js, v4.x) npm install -g jscpd@4 jscpd /path/to/code # or use without installing npx jscpd@4 /path/to/code ``` -------------------------------- ### Install cpd via crates.io Source: https://github.com/kucherenko/jscpd/blob/master/rust/README.md Install the jscpd Rust crate, which provides both jscpd and cpd binaries, using cargo. ```bash cargo install jscpd ``` -------------------------------- ### Install jscpd-server Source: https://github.com/kucherenko/jscpd/blob/master/docs/ai-ready.md Install the jscpd-server globally using npm to expose jscpd's detection capabilities as MCP tools for AI assistants. ```bash npm install -g jscpd-server ``` -------------------------------- ### jscpd Configuration File Example Source: https://github.com/kucherenko/jscpd/blob/master/fixtures/markdown/file1.md An example YAML configuration file for jscpd, defining languages, files, exclusions, reporters, and language extensions. ```yaml #.cpd.yaml languages: - javascript - coffeescript - typescript - php - python - jsx - haxe - yaml - css - ruby - go - swift - twig - java - clike # c++, c, objective-c source - csharp # c# source - htmlmixed # html mixed source like knockout.js templates files: - "test/**/*" exclude: - "**/*.min.js" - "**/*.mm.js" reporter: json languages-exts: coffeescript: - coffee javascript: - es - es5 - es6 - es7 ``` -------------------------------- ### Install jscpd Globally Source: https://github.com/kucherenko/jscpd/blob/master/fixtures/markdown/file1.md Install the jscpd command-line tool globally using npm for system-wide access. ```bash npm install jscpd -g ``` -------------------------------- ### Usage Example for @jscpd/core Source: https://github.com/kucherenko/jscpd/blob/master/packages/core/README.md Demonstrates how to use the Detector class from @jscpd/core to detect duplicate code. This example shows initialization with custom options, a tokenizer, and a store, followed by detecting clones in a given code string. ```typescript import {Tokenizer} from '@jscpd/tokenizer'; import { Detector, MemoryStore, IOptions, IClone, IStore, ITokenizer } from '@jscpd/core'; const options: IOptions = { minLines: 5, maxLines: 500, } const tokenizer: ITokenizer = new Tokenizer(); // here you can use any store what implement IStore interface const store: IStore = new MemoryStore(); // list of validators, implemented IValidator interface, validate clones const validators = []; const detector = new Detector(tokenizer, store, validators, options); ( async () => { const format = 'javascript'; const code: string = '...string with code...'; const clones: IClone[] = await detector.detect('source_id', code, format); console.log(clones); })(); ``` -------------------------------- ### Install and Run jscpd (Rust Engine) Source: https://github.com/kucherenko/jscpd/blob/master/README.md Install the Rust engine globally using npm and run jscpd or cpd on a directory. The Rust engine is significantly faster. ```bash # Rust engine (v5.x, 24-37x faster) — both jscpd and cpd commands npm install -g jscpd@5 jscpd /path/to/code cpd /path/to/code ``` -------------------------------- ### Usage Example for @jscpd/finder Source: https://github.com/kucherenko/jscpd/blob/master/packages/finder/README.md Demonstrates how to use getFilesToDetect and InFilesDetector to find code clones. Ensure you have the necessary imports and initialize the Tokenizer, Store, and Detector with appropriate options. ```typescript import { Tokenizer } from '@jscpd/tokenizer'; import { MemoryStore, IOptions, IClone, IStore, ITokenizer, } from '@jscpd/core'; import { EntryWithContent, getFilesToDetect, InFilesDetector } from '@jscpd/finder'; const options: IOptions = { minLines: 5, maxLines: 500, path: ['list of folders and files to analyse for clones'], }; const tokenizer: ITokenizer = new Tokenizer(); // any store implementing IStore works here const store: IStore = new MemoryStore(); const files: EntryWithContent[] = getFilesToDetect(options); const detector = new InFilesDetector(tokenizer, store, options); (async () => { const clones: IClone[] = await detector.detect(files); })(); ``` -------------------------------- ### Install jscpd via Cargo Source: https://github.com/kucherenko/jscpd/blob/master/rust/crates/cpd/README.md Install the jscpd CLI tool using the Rust package manager, Cargo. ```bash cargo install jscpd ``` -------------------------------- ### Start JSCPD Server in Current Directory Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Starts the JSCPD server to scan the current directory for code duplications. ```bash jscpd-server ``` -------------------------------- ### Install cpd command only (Rust Engine) Source: https://github.com/kucherenko/jscpd/blob/master/README.md Install only the 'cpd' command, which utilizes the Rust engine, globally using npm. ```bash # Rust engine — cpd command only npm install -g cpd cpd /path/to/code ``` -------------------------------- ### Install jscpd (Rust-native) Source: https://github.com/kucherenko/jscpd/blob/master/README.md Install the Rust-native version of jscpd using Cargo, which exposes both 'jscpd' and 'cpd' commands. ```bash # Rust-native install (exposes both jscpd and cpd) cargo install jscpd ``` -------------------------------- ### AI Reporter Example Output Source: https://github.com/kucherenko/jscpd/blob/master/docs/ai-ready.md This is an example of the compact output format generated by the 'ai' reporter, suitable for piping into an LLM. ```text src/utils/ auth.ts:10-25 ~ helpers.ts:40-55 src/utils/auth.ts 30-45 ~ 80-95 src/ utils/auth.ts:10-25 ~ api/routes.ts:5-20 --- 23 clones · 4.2% duplication ``` -------------------------------- ### detectClones with persist store Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd/README.md This example illustrates using `detectClones` with a persistent store. It initializes a `MemoryStore` and passes it to `detectClones` for storing detection results across multiple calls. ```APIDOC ## detectClones with persist store ### Description This example demonstrates using `detectClones` with a persistent store, such as `MemoryStore`, to maintain detection state across multiple runs. ### Method `detectClones` ### Parameters - `options` (object): Configuration object for clone detection. - `path` (string[]): An array of paths to scan for duplicates. - `silent` (boolean): If true, suppresses output. - `store` (IMemoryStore): An instance of a store (e.g., `MemoryStore`) to persist detection data. ### Response - `Promise`: A promise that resolves when the detection and storing process is complete. ``` -------------------------------- ### jscpd v5 Drop-in Replacement Example Source: https://github.com/kucherenko/jscpd/blob/master/docs/rust.md Shows how to use jscpd v5 as a direct replacement for jscpd v4, using the same command and path arguments. ```bash # Drop-in replacement for jscpd v4 jscpd /path/to/source # or cpd /path/to/source ``` -------------------------------- ### Install jscpd and cpd commands with npm Source: https://github.com/kucherenko/jscpd/blob/master/rust/jscpd/README.md Installs both the jscpd and cpd commands globally using npm. This is the recommended method for Node.js users who need both command-line tools. ```bash npm install -g jscpd ``` -------------------------------- ### Initialize RedisStore with jscpd Detector Source: https://github.com/kucherenko/jscpd/blob/master/packages/redis-store/README.md Demonstrates how to initialize the RedisStore and use it with the jscpd Detector. This setup is suitable for environments with memory constraints. ```typescript import { Tokenizer } from '@jscpd/tokenizer'; import { Detector, IOptions, IClone, IStore, ITokenizer, } from '@jscpd/core'; import RedisStore from '@jscpd/redis-store'; const options: IOptions = { minLines: 5, maxLines: 500, }; const tokenizer: ITokenizer = new Tokenizer(); const store: IStore = new RedisStore(); const detector = new Detector(tokenizer, store, [], options); ``` -------------------------------- ### jscpd CLI with argv Source: https://github.com/kucherenko/jscpd/blob/master/docs/typescript.md Example of using the jscpd CLI with command-line arguments to detect clones. ```APIDOC ## `jscpd` with argv ### Description This example demonstrates how to invoke the jscpd command-line interface with arguments to perform clone detection. ### Method CLI Command ### Endpoint N/A ### Parameters - `path` (string[]): The path(s) to analyze for clones. - `mode` (string): The detection mode (e.g., 'weak'). - `silent` (boolean): Suppresses output. ### Request Example ```typescript import { IClone } from '@jscpd/core'; import { jscpd } from 'jscpd'; const clones: IClone[] = await jscpd(['', '', './fixtures', '-m', 'weak', '--silent']); ``` ### Response #### Success Response Returns an array of detected clones (`IClone[]`). #### Response Example ```json [ { "start": {"line": 10, "column": 0}, "end": {"line": 20, "column": 5}, "startInFile": "/path/to/file1.js", "endInFile": "/path/to/file2.js", "density": 0.85, "tokenLength": 150 } ] ``` ``` -------------------------------- ### jscpd v5 with Custom Flags Example Source: https://github.com/kucherenko/jscpd/blob/master/docs/rust.md Demonstrates using jscpd v5 with custom flags, mirroring the functionality and options available in jscpd v4. ```bash # Same flags as v4 cpd /path/to/source --min-tokens 30 --min-lines 3 --reporters console,json,html ``` -------------------------------- ### Format Detection Example Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd/README.md Specify the formats for which to detect duplications using the --format flag. ```bash $ jscpd --format "php,javascript,markup,css" /path/to/files ``` -------------------------------- ### Install only the cpd command with npm Source: https://github.com/kucherenko/jscpd/blob/master/rust/jscpd/README.md Installs only the cpd command globally using npm. Use this if you prefer the shorter command name and do not need the 'jscpd' command. ```bash npm install -g cpd ``` -------------------------------- ### Install jscpd v5 with npm Source: https://github.com/kucherenko/jscpd/blob/master/docs/rust.md Installs jscpd v5 globally using npm, making both 'jscpd' and 'cpd' commands available. This is a drop-in replacement for v4 commands. ```bash # npm — installs both jscpd and cpd commands (same binary as v4 command name) npm install -g jscpd@5 jscpd /path/to/code cpd /path/to/code # cpd alias also available # npm — installs only the cpd command (lighter) npm install -g cpd cpd /path/to/code ``` -------------------------------- ### Define Formats Extensions Example Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd/README.md Specify file extensions for different formats. This redefines the default configuration, so all desired formats must be defined manually. ```bash $ jscpd --formats-exts javascript:es,es6;dart:dt /path/to/code ``` -------------------------------- ### AI Reporter Output Example Source: https://github.com/kucherenko/jscpd/blob/master/skills/jscpd/SKILL.md Illustrates the compact output format of the AI reporter, showing detected clone pairs and overall duplication statistics. ```text Clones: src/ foo.ts:10-25 ~ bar.ts:42-57 src/utils/helpers.ts:100-120 ~ src/utils/other.ts:5-25 --- 3 clones · 4.2% duplication ``` -------------------------------- ### Get Project Statistics Request Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Example of how to request overall duplication statistics for the scanned codebase using curl. ```bash curl http://localhost:3000/api/stats ``` -------------------------------- ### Programmatic Usage in Rust Source: https://github.com/kucherenko/jscpd/blob/master/rust/README.md Example of using the cpd-finder crate programmatically in Rust to detect code clones with custom configuration. ```rust use cpd_finder::orchestrate::{RunConfig, run}; let config = RunConfig { paths: vec!["./src".into()], min_tokens: 50, ..Default::default() }; let result = run(&config).unwrap(); println!("Found {} clones", result.clones.len()); println!("Analyzed {} files", result.statistics.total.sources); ``` -------------------------------- ### Install dry-refactoring Workflow Skill Source: https://github.com/kucherenko/jscpd/blob/master/docs/ai-ready.md Add the dry-refactoring skill to your AI agent to enable a guided process for reading clone output, refactoring, and verifying duplication removal. ```bash npx skills add kucherenko/jscpd --skill dry-refactoring ``` -------------------------------- ### Server Not Initialized Error Response Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Example of a 400 Bad Request response when the server has not yet completed its initial scan. ```json { "error": "Error", "message": "Server not initialized. Please wait for initial scan to complete.", "statusCode": 400 } ``` -------------------------------- ### GitHub Actions Workflow for Code Duplication Check Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md This GitHub Actions workflow automatically checks for code duplications on every push or pull request. It installs jscpd, starts the server, and then iterates through changed files to check for duplicates. ```yaml name: Check Code Duplication on: [push, pull_request] jobs: check-duplication: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Setup Node.js uses: actions/setup-node@v2 with: node-version: '18' - name: Install jscpd run: npm install -g jscpd - name: Start jscpd server run: | jscpd server . --port 3000 & sleep 10 # Wait for server to initialize - name: Check new code run: | # Check files changed in this PR for file in $(git diff --name-only HEAD~1); do if [[ -f "$file" ]]; then # Extract file extension to determine format ext="${file##*.}" curl -X POST http://localhost:3000/api/check \ -H "Content-Type: application/json" \ -d "{\"code\": \"$(cat $file | jq -Rs .)\", \"format\": \"$ext\"}" \ | jq . fi done ``` -------------------------------- ### Health Check Request Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Example of how to check the server's health and initialization status using curl. ```bash curl http://localhost:3000/api/health ``` -------------------------------- ### jscpd async/await API Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd/README.md This example shows the async/await syntax for using the jscpd API. It asynchronously awaits the result of the `jscpd` function and logs the detected clones to the console. ```APIDOC ## jscpd async/await API ### Description This demonstrates the async/await usage of the jscpd API for clone detection. It awaits the result and logs the clones. ### Method `jscpd` ### Parameters - `['', '', __dirname + '/../fixtures', '-m', 'weak', '--silent']`: An array of strings representing command-line arguments, specifying the path to scan, detection mode, and silent output. ### Response - `IClone[]`: An array of `IClone` objects representing detected code clones. ``` -------------------------------- ### Error Response for Project Statistics (Not Ready) Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Example of an error response (503 Service Unavailable) when statistics are not yet available, indicating the server is still initializing. ```json { "error": "NotReady", "message": "Statistics not available yet. Server is still initializing.", "statusCode": 503 } ``` -------------------------------- ### Install jscpd Tool Reference Skill Source: https://github.com/kucherenko/jscpd/blob/master/docs/ai-ready.md Add the jscpd tool reference skill to your AI agent to teach it about jscpd's CLI options, AI reporter format, and configuration. ```bash npx skills add kucherenko/jscpd --skill jscpd ``` -------------------------------- ### Build Project Source: https://github.com/kucherenko/jscpd/blob/master/README.md Build the project using pnpm. ```bash pnpm build ``` -------------------------------- ### jscpd v5 Blame Output Example Source: https://github.com/kucherenko/jscpd/blob/master/docs/rust.md Illustrates the 'blame' output format when used with '--reporters console-full'. It shows a side-by-side comparison of authors for duplicated code lines. ```text 176 │ Andrii Kucherenko │ <= │ 196 │ Josh Soref │ ## TODO 177 │ Andrii Kucherenko │ <= │ 197 │ Josh Soref │ 180 │ Andrii Kucherenko │ == │ 200 │ Andrii Kucherenko │ ## License ``` -------------------------------- ### Success Response for API Information Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Example of a successful response (200 OK) providing the server's name, version, available endpoints, and a link to its documentation. ```json { "name": "jscpd-server", "version": "1.0.0", "endpoints": { "POST /api/check": "Check code snippet for duplications", "GET /api/stats": "Get overall project statistics", "GET /api/health": "Server health check" }, "documentation": "https://github.com/kucherenko/jscpd" } ``` -------------------------------- ### jscpd Configuration in package.json Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd/README.md Alternatively, configure jscpd within the 'jscpd' section of your package.json file. This example includes additional options like 'gitignore'. ```json { "...": "...", "jscpd": { "path": ["./src"], "threshold": 0.1, "reporters": ["html", "console", "badge"], "ignore": ["**/__snapshots__/**"], "absolute": true, "gitignore": true } "...": "..." } ``` -------------------------------- ### Go Hello World Program Source: https://github.com/kucherenko/jscpd/blob/master/packages/tokenizer/__tests__/fixtures/markdown/synonym-langs.md A simple 'Hello, World!' program in Go. ```go package main import "fmt" func main() { fmt.Println("hello world") } ``` -------------------------------- ### Build cpd from source Source: https://github.com/kucherenko/jscpd/blob/master/rust/README.md Clone the repository and build the release binaries for jscpd and cpd locally. ```bash git clone https://github.com/kucherenko/jscpd.git cd jscpd/rust cargo build --release # binaries at target/release/jscpd and target/release/cpd ``` -------------------------------- ### MCP Server Installation and Usage Source: https://github.com/kucherenko/jscpd/blob/master/docs/ai-ready.md The jscpd-server implements the Model Context Protocol (MCP), exposing jscpd's detection capabilities as tools that AI assistants can call directly from the editor. ```APIDOC ## MCP Server [jscpd-server](../apps/jscpd-server) implements the [Model Context Protocol (MCP)](https://modelcontextprotocol.io), exposing jscpd's detection capabilities as tools that AI assistants can call directly from the editor. Start the server once against your codebase, then let your AI assistant check any snippet for duplication on demand — no CLI invocation needed. ### Installation ```bash npm install -g jscpd-server ``` ### Usage Start the server: ```bash jscpd-server /path/to/project ``` Options: - `--port` — Port number (default: 3000) - `--host` — Host to bind (default: 0.0.0.0) - `--store leveldb` — Use LevelDB persistent storage - Plus all standard jscpd detection options ``` -------------------------------- ### Using a Configuration File Source: https://github.com/kucherenko/jscpd/blob/master/fixtures/markdown/file2.md Specify a configuration file for jscpd settings. ```bash jscpd --config test/.cpd.yaml ``` -------------------------------- ### Success Response for Project Statistics Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Example of a successful response (200 OK) containing detailed duplication statistics for the project, including total counts and format-specific breakdowns. ```json { "statistics": { "detectionDate": "2025-11-17T10:30:00.000Z", "total": { "lines": 10000, "tokens": 50000, "sources": 50, "duplicatedLines": 500, "duplicatedTokens": 2500, "clones": 10, "percentage": 5.0, "percentageTokens": 5.0, "newDuplicatedLines": 0, "newClones": 0 }, "formats": { "javascript": { "total": { "lines": 5000, "tokens": 25000, "sources": 30, "duplicatedLines": 300, "duplicatedTokens": 1500, "clones": 6, "percentage": 6.0, "percentageTokens": 6.0, "newDuplicatedLines": 0, "newClones": 0 }, "sources": { "src/file1.js": { "lines": 100, "tokens": 500, "sources": 1, "duplicatedLines": 10, "duplicatedTokens": 50, "clones": 1, "percentage": 10.0, "percentageTokens": 10.0, "newDuplicatedLines": 0, "newClones": 0 } } } } }, "timestamp": "2025-11-17T10:30:00.000Z" } ``` -------------------------------- ### Extract Module/Utility Refactoring Example Source: https://github.com/kucherenko/jscpd/blob/master/skills/dry-refactoring/SKILL.md Demonstrates the 'extract module/utility' refactoring strategy. This is used when duplicated code spans multiple files across different domains, suggesting a need for a shared utility file. ```typescript // Move shared logic to a shared utility file and import it ``` -------------------------------- ### Add Tests and Check Source: https://github.com/kucherenko/jscpd/blob/master/README.md Run tests and checks for your changes using pnpm. ```bash pnpm test ``` -------------------------------- ### Install dry-refactoring Skill Source: https://github.com/kucherenko/jscpd/blob/master/skills/jscpd/SKILL.md Command to install the 'dry-refactoring' skill, which assists in refactoring duplicated code detected by jscpd. ```bash npx skills add https://github.com/kucherenko/jscpd --skill dry-refactoring ``` -------------------------------- ### Start JSCPD Server on Specific Port Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Starts the JSCPD server on a specified port, scanning the current directory. ```bash jscpd-server . --port 8080 ``` -------------------------------- ### Basic CLI Usage for jscpd v5 Source: https://github.com/kucherenko/jscpd/blob/master/docs/rust.md Demonstrates the basic command-line usage for jscpd v5, showing that both 'jscpd' and 'cpd' commands function identically. ```bash # Both commands work the same way jscpd [OPTIONS] [PATH]... cpd [OPTIONS] [PATH]... ``` -------------------------------- ### Ignore Pattern Example Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd/README.md Use --ignore-pattern to exclude code blocks matching regular expression patterns. This example excludes import statements from the calculation. ```bash $ jscpd /path/to/source --ignore-pattern "import.*from\s*'.*'" ``` -------------------------------- ### List Supported Formats Source: https://github.com/kucherenko/jscpd/blob/master/docs/rust.md Execute `cpd --list` to view all supported programming language formats for clone detection. ```bash cpd --list ``` -------------------------------- ### Configure PromQL format Source: https://github.com/kucherenko/jscpd/blob/master/FORMATS.md Use this command to enable duplicate detection for PromQL queries, specifying the '.promql' extension. ```bash jscpd --formats-exts "promql:promql" ./src ``` -------------------------------- ### Basic Usage: Specify Path and Languages Source: https://github.com/kucherenko/jscpd/blob/master/fixtures/markdown/file2.md Use this command to scan a specific directory for JavaScript and CoffeeScript files. ```bash jscpd --path my_project/ --languages javascript,coffee ``` -------------------------------- ### Install jscpd v5 with Cargo Source: https://github.com/kucherenko/jscpd/blob/master/docs/rust.md Installs jscpd v5 using Rust's package manager, Cargo. This method also makes both 'jscpd' and 'cpd' commands available. ```bash # crates.io — Rust-native install (exposes both jscpd and cpd commands) cargo install jscpd jscpd /path/to/code cpd /path/to/code ``` -------------------------------- ### Build and Test Rust Project Source: https://github.com/kucherenko/jscpd/blob/master/rust/README.md Commands to build the Rust project in release mode and run its tests. ```bash cargo build --release ``` ```bash cargo test ``` -------------------------------- ### Configure Qore format Source: https://github.com/kucherenko/jscpd/blob/master/FORMATS.md Use this command to enable duplicate detection for Qore scripts, specifying the '.q' and '.qm' extensions. ```bash jscpd --formats-exts "qore:q,qm" ./src ``` -------------------------------- ### JSON Reporter Output Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd/README.md Example of the JSON output generated by jscpd when using a reporter. ```json { "duplicates": [{ "format": "javascript", "lines": 27, "fragment": "...code fragment... ", "tokens": 0, "firstFile": { "name": "tests/fixtures/javascript/file2.js", "start": 1, "end": 27, "startLoc": { "line": 1, "column": 1 }, "endLoc": { "line": 27, "column": 2 } }, "secondFile": { "name": "tests/fixtures/javascript/file1.js", "start": 1, "end": 24, "startLoc": { "line": 1, "column": 1 }, "endLoc": { "line": 24, "column": 2 } } }], "statistic": { "detectionDate": "2018-11-09T15:32:02.397Z", "formats": { "javascript": { "sources": { "/path/to/file": { "lines": 24, "sources": 1, "clones": 1, "duplicatedLines": 26, "percentage": 45.33, "newDuplicatedLines": 0, "newClones": 0 } }, "total": { "lines": 297, "sources": 1, "clones": 1, "duplicatedLines": 26, "percentage": 45.33, "newDuplicatedLines": 0, "newClones": 0 } } }, "total": { "lines": 297, "sources": 6, "clones": 5, "duplicatedLines": 26, "percentage": 45.33, "newDuplicatedLines": 0, "newClones": 0 } } } ``` -------------------------------- ### GET /api/statistics Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Retrieves the overall duplication statistics for the entire project that the server is monitoring. ```APIDOC ## GET /api/statistics ### Description Retrieves the overall duplication statistics for the entire project that the server is monitoring. ### Method GET ### Endpoint /api/statistics ### Response #### Success Response (200) - **totalDuplications** (number) - The total number of duplications found. - **duplicatedLines** (number) - The total number of lines that are duplicated. - **totalLines** (number) - The total number of lines scanned. - **percentageDuplicated** (number) - The percentage of lines that are duplicated. #### Response Example ```json { "totalDuplications": 5, "duplicatedLines": 50, "totalLines": 1000, "percentageDuplicated": 5 } ``` ### Error Handling - **StatsError** (500) - Error retrieving statistics. - **NotReady** (503) - Statistics not available yet (server still initializing). ``` -------------------------------- ### Configure ReasonML format Source: https://github.com/kucherenko/jscpd/blob/master/FORMATS.md Use this command to enable duplicate detection for ReasonML files, specifying the '.re' and '.rei' extensions. ```bash jscpd --formats-exts "reason:re,rei" ./src ``` -------------------------------- ### Skip Comments Example Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd/README.md Use the --skipComments flag to ignore comments during duplicate detection. ```bash $ jscpd --skipComments /path/to/source ``` -------------------------------- ### Configure WebAssembly Text format Source: https://github.com/kucherenko/jscpd/blob/master/FORMATS.md Use this command to enable duplicate detection for WebAssembly text format files, specifying the '.wat' extension. ```bash jscpd --formats-exts "wasm:wat" ./src ``` -------------------------------- ### Configure Xeora format Source: https://github.com/kucherenko/jscpd/blob/master/FORMATS.md Use this command to enable duplicate detection for Xeora template files, specifying the '.xeora' and '.xchtml' extensions. ```bash jscpd --formats-exts "xeora:xeora,xchtml" ./src ``` -------------------------------- ### API Information Request Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Example of how to retrieve information about the jscpd-server API and its endpoints using curl. ```bash curl http://localhost:3000/ ``` -------------------------------- ### detectClones with custom store Source: https://github.com/kucherenko/jscpd/blob/master/docs/typescript.md Demonstrates using `detectClones` with a custom store (e.g., `MemoryStore`) for managing clone data and enabling incremental detection. ```APIDOC ## `detectClones` with custom store ### Description This example shows how to use `detectClones` with a custom store implementation, such as `MemoryStore`, to manage detected clones. This is useful for incremental detection or custom storage needs. ### Method `detectClones(options: DetectClonesOptions, store: IStore): Promise` ### Endpoint N/A (API Function) ### Parameters #### Options Object - **path** (string[]): Required. The path(s) to analyze. - **silent** (boolean): Optional. Suppresses output. #### Store Object - **store** (IStore): An instance of a store implementation (e.g., `MemoryStore`, `LeveldbStore`). ### Request Example ```typescript import { detectClones } from 'jscpd'; import { IMapFrame, MemoryStore } from '@jscpd/core'; const store = new MemoryStore(); // Initial detection await detectClones({ path: ['./src'], }, store); // Incremental detection using the same store await detectClones({ path: ['./src'], silent: true, }, store); ``` ### Response #### Success Response Returns a promise that resolves to an array of `IClone` objects detected during the operation. #### Response Example ```json [ { "start": {"line": 10, "column": 0}, "end": {"line": 20, "column": 5}, "startInFile": "/path/to/file1.js", "endInFile": "/path/to/file2.js", "density": 0.85, "tokenLength": 150 } ] ``` ``` -------------------------------- ### Programmatic Usage in CoffeeScript Source: https://github.com/kucherenko/jscpd/blob/master/fixtures/markdown/file2.md Example of using jscpd programmatically within a CoffeeScript application to run checks. ```coffeescript # coffeescript jscpd = require('jscpd') result = jscpd::run path: 'my/project/folder' files: '**/*.js' exclude: ['**/*.min.js', '**/node_modules/**'] reporter: json ``` -------------------------------- ### Configure MediaWiki format Source: https://github.com/kucherenko/jscpd/blob/master/FORMATS.md Use this command to enable duplicate detection for MediaWiki markup files, specifying the '.wiki' extension. ```bash jscpd --formats-exts "wiki:wiki" ./src ``` -------------------------------- ### MCP Tools Source: https://github.com/kucherenko/jscpd/blob/master/docs/ai-ready.md The jscpd-server exposes MCP tools for checking duplications, getting statistics, and re-scanning the working directory. ```APIDOC ### MCP Tools Available MCP tools exposed via the `/mcp` endpoint: - `check_duplication` — Check a code snippet for duplications (inputs: `code`, `format`) - `get_statistics` — Get project stats (no inputs) - `check_current_directory` — Re-scan the working directory (no inputs) Snippet checking uses an ephemeral in-memory store per request for isolation — no cross-request contamination, automatic cleanup, concurrent-request safe. ``` -------------------------------- ### Silent Mode Example Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd/README.md Run jscpd in silent mode using the --silent flag to suppress console output. ```bash $ jscpd /path/to/source --silent ``` -------------------------------- ### Processing Error Response Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Example of a 400 Bad Request response indicating a general processing error during the code check. ```json { "error": "CheckError", "message": "Error details...", "statusCode": 400 } ``` -------------------------------- ### Configure Pure format Source: https://github.com/kucherenko/jscpd/blob/master/FORMATS.md Use this command to enable duplicate detection for Pure language files, specifying the '.pure' extension. ```bash jscpd --formats-exts "pure:pure" ./src ``` -------------------------------- ### Validation Error Response (Empty Code) Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Example of a 400 Bad Request response indicating that the 'code' field cannot be empty. ```json { "error": "ValidationError", "message": "Field \"code\" cannot be empty", "statusCode": 400 } ``` -------------------------------- ### Get Statistics from jscpd Server Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Retrieve statistics about code duplication from the jscpd server API. This is useful for monitoring and reporting. ```bash curl -s http://localhost:3000/api/stats | jq '.statistics.total' ``` -------------------------------- ### Get Project Statistics Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md A placeholder for a bash script intended to retrieve overall project duplication statistics from the jscpd server. ```bash #!/bin/bash ``` -------------------------------- ### Validation Error Response (Wrong Type) Source: https://github.com/kucherenko/jscpd/blob/master/apps/jscpd-server/README.md Example of a 400 Bad Request response indicating an incorrect data type for a field. ```json { "error": "ValidationError", "message": "Field \"code\" must be a string", "statusCode": 400 } ``` -------------------------------- ### Agent Skills Installation Source: https://github.com/kucherenko/jscpd/blob/master/docs/ai-ready.md jscpd provides AI agent skills to teach coding assistants how to use jscpd for detecting and refactoring duplications. ```APIDOC ## Agent Skills jscpd ships two AI agent skills that teach coding assistants how to use jscpd and refactor detected duplications. ### jscpd — Tool Reference Skill Covers all CLI options, the AI reporter output format, and configuration file syntax. ```bash npx skills add kucherenko/jscpd --skill jscpd ``` ### dry-refactoring — Refactoring Workflow Skill A guided process for reading clone output, choosing the right extraction strategy, applying the refactor, and verifying the clone is eliminated. ```bash npx skills add kucherenko/jscpd --skill dry-refactoring ``` After installation, ask your agent to "find and fix code duplication" and it will invoke jscpd with the right options and act on the results. ```