@barney-media/crap-typescript

中文文档
2.0.0发布于 2026/10/19Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 3.8万

用于TypeScript项目CRAP计量分析的CLI.

中文使用文档
文档目录5 节
  1. 安装
  2. 使用量
  3. 选项
  4. 退出代码
  5. 许可证

@barney-media/crap-typescript

在TypeScript项目中用于CRAP(Change Risk Anti-Pattern)计量分析的CLI.

安装

npm install --save-dev @barney-media/crap-typescript

使用量

npx crap-typescript
npx crap-typescript --changed
npx crap-typescript --format json --output reports/crap.json
npx crap-typescript --agent --junit-report reports/crap-junit.xml
npx crap-typescript --threshold 6
npx crap-typescript --exclude 'packages/api/generated/**'
npx crap-typescript --exclude-path-regex '^src/proto/'
npx crap-typescript --exclude-generated-marker '@custom-generated'
npx crap-typescript src/sample.ts
npx crap-typescript packages/api packages/web

选项

--help                       Print usage to stdout
(no args)                    Analyze all TypeScript files under any nested src/ tree
--changed                    Analyze changed TypeScript files under src/
--exclude <glob>             Exclude source paths by normalized relative glob; repeatable
--exclude-path-regex <regex> Exclude source paths by normalized relative regex; repeatable
--exclude-generated-marker <marker> Exclude leading generated-header markers; repeatable
--use-default-exclusions[=true|false] Enable generated-code defaults (`true` by default)
--package-manager <tool>     Force auto, npm, pnpm, yarn, or bun
--test-runner <runner>       Force auto, vitest, or jest
--format <format>            Emit toon, json, text, junit, or none (default: toon)
--agent                      Default primary output to toon, failures-only, omit-redundancy
--failures-only[=true|false] Emit failed methods only in the primary report
--omit-redundancy[=true|false] Omit redundant per-method status in the primary report
--output <path>              Write the primary report to a file instead of stdout
--junit-report <path>        Also write a full JUnit XML report for CI test-report UIs
--threshold <number>         Override the CRAP threshold (`6.0` by default)
<file ...>                   Analyze explicit TypeScript files
<directory ...>              Analyze TypeScript files under each directory's nested src/ tree

默认 toon 格式是紧凑和代理可读的。 初步报告包括运行级别 status 财务报告和审定财务报表 threshold; 方法行使用 status, (中文(简体) ), crap, (中文(简体) ), cc, (中文(简体) ), cov, (中文(简体) ), covKind, (中文(简体) ), method, (中文(简体) ), src, (中文(简体) ), lineStart,以及 lineEnd,与 src 设置到项目相对源文件路径. --format none 压制主 stdout 输出; 当使用时 --output,它创建了空的主报告文件. --failures-only, (中文(简体) ), --failures-only=true,以及 --failures-only=false 控制主报告是否只包含失败的方法条目. --omit-redundancy, (中文(简体) ), --omit-redundancy=true,以及 --omit-redundancy=false 控制主报告方法行是否省略 status; 与 --format junit,它们省略了自定义测试框 status 属性,同时保留 JUnit 失败和跳出元素。 JUnit侧车报告总是包括全套方法集和方法状态,不论是否 --agent, (中文(简体) ), --failures-only,或 --omit-redundancy。JUnit XML是用于 GitLab 测试标签的形状,有一个 <testsuites> 包装、 测试 classname编号 :file 源路径, 带有度量的试样名称, time="0"级别 system-out 加上包含 CRAP 分数、阈值、覆盖类型、源路径和行距的失败/滑动文本. --agent 是一个复合快捷键,而不是格式:它默认主输出为 --format toon, (中文(简体) ), --failures-only=true,以及 --omit-redundancy=true明确 --format, (中文(简体) ), --failures-only=false,或 --omit-redundancy=false 选项可以覆盖这些默认值.

基线可分析性排除总是跳过声明,测试/样本文件, __tests__/, (中文(简体) ), dist/, (中文(简体) ), coverage/,以及 node_modules/生成代码默认值,然后排除生成的目录片段,精确 gen 目录片段,常见生成的文件名后缀,原出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自出自 除非 --use-default-exclusions=false; 禁用默认值不会禁用基线可分析性过滤器.

完整的初级报告和JUnit侧车在排除文件时包括排除来源的审计计数。 默认代理主输出省略了这些细节.

由内置执行人运行的命令,包括覆盖命令和 --changed 发现,300秒后暂停 设定 CRAP_TYPESCRIPT_COMMAND_TIMEOUT_MS 改为非负毫秒值,以覆盖这一默认值; 0 禁用超时。 被捕捉到的出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出 用户阵容命令失败可能包括截断输出,而需要完整输出的内部命令,例如 git status --porcelain -z,如果超过上限则失败.

默认阈值是 6.0。以下数值 4.0 打印警告,因为它们可能太吵;上方的值 8.0 印出警告,因为即使硬门也太宽大了.

退出代码

  • 0 成功,阈值得到遵守
  • 1 无效的 CLI 使用或执行失败
  • 2 CRAP 阈值超过 ( > 已配置阈值)

见 主要文件 详细情况.

许可证

阿帕奇 - 2.0

版本与发布记录

@barney-media/crap-typescript 共发布 14 个版本,首次发布于 2026/04/08;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.0latest2026/10/1920 天前
1.0.02026/09/281 个月前
0.5.32026/09/261 个月前
0.5.22026/09/052 个月前
0.5.12026/08/302 个月前
0.5.02026/08/092 个月前

关于 @barney-media/crap-typescript 的常见问题

@barney-media/crap-typescript 是什么?

用于TypeScript项目CRAP计量分析的CLI.

如何安装 @barney-media/crap-typescript?

在项目目录运行 npm install @barney-media/crap-typescript,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 crap-typescript,可以用 npx crap-typescript 直接运行。

@barney-media/crap-typescript 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@barney-media/crap-typescript 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@barney-media/crap-typescript 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.13.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@barney-media/crap-typescript 可以商用吗?

@barney-media/crap-typescript 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@barney-media/crap-typescript 的下载量有多少?

根据 npm 官方下载统计,@barney-media/crap-typescript 在 2026/10/08 – 2026/11/06 这 30 天内被下载 37,701 次,日均约 1,257 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@barney-media/crap-typescript 还在维护吗?

最新版本 2.0.0 发布于 2026/10/19(20 天前);至今共发布 14 个版本,其中过去 12 个月发布了 14 个;首次发布于 2026/04/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。