@barney-media/crap-typescript

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

CLI for CRAP metric analysis in TypeScript projects.

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Options
  4. Exit Codes
  5. License

@barney-media/crap-typescript

CLI for CRAP (Change Risk Anti-Pattern) metric analysis in TypeScript projects.

Install

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

Usage

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

Options

--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

The default toon format is compact and agent-readable. Primary reports include run-level status and threshold; method rows use status, crap, cc, cov, covKind, method, src, lineStart, and lineEnd, with src set to the project-relative source file path. --format none suppresses primary stdout output; when used with --output, it creates an empty primary report file. --failures-only, --failures-only=true, and --failures-only=false control whether the primary report includes only failed method entries. --omit-redundancy, --omit-redundancy=true, and --omit-redundancy=false control whether primary report method rows omit status; with --format junit, they omit the custom testcase status property while preserving JUnit failure and skipped elements. JUnit sidecar reports always include the full method set and method status, regardless of --agent, --failures-only, or --omit-redundancy. JUnit XML is shaped for GitLab's Tests tab with a <testsuites> wrapper, testcase classname/file source paths, metric-bearing testcase names, time="0", and testcase-level system-out plus failure/skipped text containing CRAP score, threshold, coverage kind, source path, and line range. --agent is a composite shortcut, not a format: it defaults primary output to --format toon, --failures-only=true, and --omit-redundancy=true. Explicit --format, --failures-only=false, or --omit-redundancy=false options override those defaults.

Baseline analyzability exclusions always skip declarations, test/spec files, __tests__/, dist/, coverage/, and node_modules/. Generated-code defaults then exclude generated directory segments, exact gen directory segments, common generated filename suffixes, protobuf outputs, Angular generated artifacts, and leading generated-header markers before the first non-comment token. User exclusions compose with those defaults unless --use-default-exclusions=false; disabling defaults does not disable the baseline analyzability filter.

Full primary reports and JUnit sidecars include source-exclusion audit counts when files were excluded. Default agent primary output omits those details.

Commands run by the built-in executor, including coverage commands and --changed discovery, time out after 300 seconds by default. Set CRAP_TYPESCRIPT_COMMAND_TIMEOUT_MS to a non-negative millisecond value to override this default; 0 disables the timeout. Captured stdout and stderr are capped at 10 MiB per stream. User-facing command failures may include truncated output, while internal commands that require complete output, such as git status --porcelain -z, fail if the cap is exceeded.

The default threshold is 6.0. Values below 4.0 print a warning because they are likely too noisy; values above 8.0 print a warning because they are too lenient even for hard gates.

Exit Codes

  • 0 success, threshold respected
  • 1 invalid CLI usage or execution failure
  • 2 CRAP threshold exceeded (> configured threshold)

See the main documentation for full details.

License

Apache-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 是什么?

CLI for CRAP metric analysis in TypeScript projects.

如何安装 @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 和提交记录判断。