# @barney-media/crap-typescript 中文文档

规范地址：https://npmcn.com/package/%40barney-media/crap-typescript/

版本：2\.0\.0
npm 元数据更新时间：2026-10-18T18:34:22.058Z
中文译文更新时间：2026-11-07T17:08:57.428Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40barney-media/crap-typescript
原文对照：https://npmcn.com/package/%40barney-media/crap-typescript/?lang=original
项目主页：https://github.com/fabian-barney/crap-typescript
源码仓库：https://github.com/fabian-barney/crap-typescript

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：19 kB，11 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=22\.13\.0
- 运行依赖：1 个
- 首次发布：2026-04-08T14:07:33.381Z
- 最新版本发布：2026-10-18T18:34:21.735Z
- 近 30 天下载量：37701（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：14（近 12 个月 14 个）

## 中文简介

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

## 安装

```sh
npm install @barney-media/crap-typescript
```

也可以使用：`pnpm add @barney-media/crap-typescript`、`yarn add @barney-media/crap-typescript`、`bun add @barney-media/crap-typescript`

## README

# @barney-media/crap-typescript

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

## 安装

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

## 使用量

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

## 选项

```text
--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 阈值超过 ( > 已配置阈值)

见 [主要文件](https://github.com/fabian-barney/crap-typescript) 详细情况.

## 许可证

[阿帕奇 - 2.0](https://github.com/fabian-barney/crap-typescript/blob/main/LICENSE)

## 常见问题

### @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 和提交记录判断。

