kucherenko/jscpd

GitHub: kucherenko/jscpd

基于 Rabin-Karp 算法的跨语言源代码复制粘贴检测工具,帮助团队识别和消除代码重复以降低技术债务和安全风险。

Stars: 5614 | Forks: 234

# jscpd ![支持乌克兰](https://badgen.net/badge/support/UKRAINE/?color=0057B8&labelColor=FFD700) [![npm](https://img.shields.io/npm/v/jscpd.svg?style=flat-square)](https://www.npmjs.com/package/jscpd) ![jscpd](https://static.pigsec.cn/wp-content/uploads/repos/2026/05/c8c6440cab021230.svg) [![license](https://img.shields.io/github/license/kucherenko/jscpd.svg?style=flat-square)](https://github.com/kucherenko/jscpd/blob/master/LICENSE) [![npm](https://img.shields.io/npm/dw/jscpd.svg?style=flat-square)](https://www.npmjs.com/package/jscpd) [![jscpd CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/05/b6cf984546021231.svg)](https://github.com/kucherenko/jscpd/actions/workflows/nodejs.yml) [![codecov](https://codecov.io/gh/kucherenko/jscpd/branch/master/graph/badge.svg)](https://codecov.io/gh/kucherenko/jscpd) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fkucherenko%2Fjscpd.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fkucherenko%2Fjscpd?ref=badge_shield) [![Open Collective 上的赞助者](https://opencollective.com/jscpd/backers/badge.svg)](#backers) [![Open Collective 上的赞助商](https://opencollective.com/jscpd/sponsors/badge.svg)](#sponsors) [![NPM](https://nodei.co/npm/jscpd.svg)](https://nodei.co/npm/jscpd/) 复制/粘贴是许多项目中常见的技术债务。jscpd 能够在 150 多种编程语言和数字文档格式中发现重复的代码块。 jscpd 工具使用 [Rabin-Karp](https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm) 算法来搜索重复内容。 ## jscpd 的包 | 名称 | 版本 | 描述 | |----------------------|----------|---------------| | [jscpd](apps/jscpd) | [![npm](https://img.shields.io/npm/v/jscpd.svg?style=flat-square)](https://www.npmjs.com/package/jscpd) | jscpd 的主包(包含 cli 和用于检测的 API) | | [jscpd-server](apps/jscpd-server) | [![npm](https://img.shields.io/npm/v/jscpd-server.svg?style=flat-square)](https://www.npmjs.com/package/jscpd-server) | jscpd 服务器应用程序 | | [@jscpd/core](packages/core) | [![npm](https://img.shields.io/npm/v/@jscpd/core.svg?style=flat-square)](https://www.npmjs.com/package/@jscpd/core) |核心检测算法,可用于在不同环境中检测重复代码,仅有一个依赖项 eventemitter3 | | [@jscpd/finder](packages/finder) | [![npm](https://img.shields.io/npm/v/@jscpd/finder.svg?style=flat-square)](https://www.npmjs.com/package/@jscpd/finder) | 文件中的重复检测器 | | [@jscpd/tokenizer](packages/tokenizer) | [![npm](https://img.shields.io/npm/v/@jscpd/tokenizer.svg?style=flat-square)](https://www.npmjs.com/package/@jscpd/tokenizer) | 用于对编程源代码进行 token 化的工具 | | [@jscpd/leveldb-store](packages/leveldb-store) | [![npm](https://img.shields.io/npm/v/@jscpd/leveldb-store.svg?style=flat-square)](https://www.npmjs.com/package/@jscpd/leveldb-store) | LevelDB 存储,用于大型代码库,比默认存储慢 | | [@jscpd/html-reporter](packages/html-reporter) | [![npm](https://img.shields.io/npm/v/@jscpd/html-reporter.svg?style=flat-square)](https://www.npmjs.com/package/@jscpd/html-reporter) | jscpd 的 Html 报告器 | | [@jscpd/badge-reporter](packages/badge-reporter) | [![npm](https://img.shields.io/npm/v/@jscpd/badge-reporter.svg?style=flat-square)](https://www.npmjs.com/package/@jscpd/badge-reporter) | jscpd 的徽章报告器 | ## AI 智能体技能 jscpd 包含一个用于检测和消除代码重复的[智能体技能](SKILL.md),可与 AI 编程助手(Claude、Copilot、Gemini、Cursor 等)配合使用。 ``` npx skills add kucherenko/jscpd ``` 安装后,该技能会指导你的智能体运行带有 `ai` 报告器的 jscpd,并对检测到的重复代码进行重构。 ## 安装 ``` $ npm install -g jscpd ``` ## 用法 ``` $ npx jscpd /path/to/source ``` 或 ``` $ jscpd /path/to/code ``` 或 ``` $ jscpd --pattern "src/**/*.js" ``` 关于 cli 的更多信息请点击[这里](apps/jscpd)。 ## JSCPD 服务器 JSCPD 服务器是一个独立的应用程序,提供了用于检测代码重复的 API。它可用于将重复检测功能集成到你的服务或工具中。 ### 安装 ``` $ npm install -g jscpd-server ``` ### 用法 启动服务器: ``` $ jscpd-server ``` 检查代码重复: ``` $ curl -X POST http://localhost:3000/api/check \ -H "Content-Type: application/json" \ -d '{ "code": "console.log(\"hello\");\nconsole.log(\"world\");", "format": "javascript" }' ``` 关于服务器的更多信息请点击[这里](apps/jscpd-server)。 ## 编程 API 要将复制/粘贴检测集成到你的应用程序中,可以使用编程 API: `jscpd` Promise API ``` import {IClone} from '@jscpd/core'; import {jscpd} from 'jscpd'; const clones: Promise = jscpd(process.argv); ``` `jscpd` async/await API ``` import {IClone} from '@jscpd/core'; import {jscpd} from 'jscpd'; (async () => { const clones: IClone[] = await jscpd(['', '', __dirname + '/../fixtures', '-m', 'weak', '--silent']); console.log(clones); })(); ``` `detectClones` API ``` import {detectClones} from "jscpd"; (async () => { const clones = await detectClones({ path: [ __dirname + '/../fixtures' ], silent: true }); console.log(clones); })() ``` 带有持久化存储的 `detectClones` ``` import {detectClones} from "jscpd"; import {IMapFrame, MemoryStore} from "@jscpd/core"; (async () => { const store = new MemoryStore(); await detectClones({ path: [ __dirname + '/../fixtures' ], }, store); await detectClones({ path: [ __dirname + '/../fixtures' ], silent: true }, store); })() ``` 如果需要深度定制检测过程,你可以使用 `@jscpd/core`、`@jscpd/finder` 和 `@jscpd/tokenizer` 构建你自己的工具。 ## 谁在使用 jscpd - [GitHub Super Linter](https://github.com/github/super-linter) 是多个 linter 的组合,可作为 GitHub Action 安装 - [Code-Inspector](https://www.code-inspector.com/) 是一个代码分析和技术债务管理服务。 - [Mega-Linter](https://nvuillam.github.io/mega-linter/) 是一个 100% 开源的 CI linter 聚合器(支持 GitHub Action 及其他 CI 工具),也可在本地运行 - [Codacy](http://docs.codacy.com/getting-started/supported-languages-and-tools/) 可以在开发过程中自动分析你的源代码并识别问题,帮助你更高效地开发软件并减少后续问题。 - [Natural](https://github.com/NaturalNode/natural) 是一个用于 nodejs 的通用自然语言工具。它为自然语言处理提供了广泛的功能。 - [OpenClaw](https://github.com/openclaw/openclaw) 是一个在自有设备上运行的个人 AI 助手,支持 20 多个消息渠道和多平台伴侣应用。 ## Star 历史 [![Star History Chart](https://api.star-history.com/chart?repos=kucherenko/jscpd&type=date&legend=top-left)](https://www.star-history.com/?repos=kucherenko%2Fjscpd&type=date&legend=top-left) ## 许可证 [MIT](LICENSE) © Andrey Kucherenko
标签:CLI, CMS安全, DNS解析, GNU通用公共许可证, JavaScript, MITM代理, Node.js, NPM包, OSV-Scalibr, Rabin-Karp算法, SOC Prime, TypeScript, WiFi技术, 代码克隆检测, 代码分析, 代码规范, 凭证管理, 安全插件, 开发工具, 开源项目, 技术债务, 数据管道, 暗色界面, 自动化攻击, 软件工程, 重复代码, 错误基检测, 静态代码分析