@contrast/cli

中文文档
1.72.1发布于 2026/11/05SEE LICENSE IN LICENSEES Module近 30 天下载 1.4万

A collection of agent related CLI utilities

原始使用文档
文档目录5 节
  1. Running
  2. Commands
  3. config-diagnostics
  4. system-diagnostics
  5. rewrite

@contrast/cli

Command-line utilities for the Contrast Node.js agent. @contrast/agent depends on this package, so the commands are available wherever the agent is installed.

Running

The package has several commands, so name the command with npx -p (plain npx @contrast/cli doesn't work):

npx -p @contrast/cli <command> [options]

Run the commands from the application's directory, with the same environment variables the application uses. They discover configuration exactly as the agent does (contrast_security.yaml and CONTRAST__* variables), so a different working directory or environment gives different results. They need a Node.js version the agent supports.

Commands

config-diagnostics

Usage: npx -p @contrast/cli config-diagnostics [options] <entrypoint>

Arguments:
  entrypoint             The entrypoint JavaScript or ESM file for the application

Options:
  -q, --quiet            suppress logging to stdout
  -o, --output <string>  output file for the generated JSON
                         (default: ./contrast_effective_config.json)
  -V, --version          output the version number
  -h, --help             display help for command

Prints the agent's effective configuration as JSON, showing each option's value and where it came from (environment variable, configuration file, Contrast UI, or default). Credentials are redacted.

Unless api.enable is false, it connects to Contrast (TeamServer) the same way the agent does at startup, so the output includes remote settings from the Contrast UI. The Status field says whether that connection succeeded and, if not, why.

The JSON goes to stdout and to the output file. --quiet only suppresses stdout. The file is still written.

system-diagnostics

Usage: npx -p @contrast/cli system-diagnostics [options]

Options:
  -q, --quiet            suppress logging to stdout
  -o, --output <string>  output file for the generated JSON
                         (default: ./contrast_system_info.json)
  -V, --version          output the version number
  -h, --help             display help for command

Prints information about the host or container the agent runs on, as JSON. This is the same system information the agent collects. It doesn't contact Contrast.

The JSON goes to stdout and to the output file. --quiet only suppresses stdout.

rewrite

Usage: npx -p @contrast/cli rewrite [options] <entrypoint>

Arguments:
  entrypoint     The entrypoint for the application

Options:
  -a, --assess   rewrite in assess mode
  -p, --protect  rewrite in protect mode
  -V, --version  output the version number
  -h, --help     display help for command

Rewrites the application's source files ahead of time and caches the results, so the agent doesn't have to rewrite them when the application starts. Starting from the entrypoint, it follows every static import, require(), and import() with a string argument, and rewrites each file it reaches.

The mode must match how the application will run:

  • --protect rewrites for Protect.
  • --assess rewrites for Assess. It is also the default when assess.enable is true in the configuration.
  • With neither flag and Assess not enabled, it rewrites for Protect.

Results are cached in <agent.node.rewrite.cache.path>/<application name>/<agent version>/<mode>/. The cache path defaults to .contrast/ in the working directory. The agent only uses the cache when the application name, agent version, and mode all match, so rerun rewrite after upgrading the agent.

The command fails with Rewriting disabled. if agent.node.rewrite.enable or agent.node.rewrite.cache.enable is false.

版本与发布记录

@contrast/cli 共发布 110 个版本,首次发布于 2023/01/12;过去 12 个月发布了 23 个版本。

版本发布日期距今
1.72.1latest2026/11/056 天前
1.72.02026/10/3011 天前
1.71.02026/10/2318 天前
1.70.02026/10/1724 天前
1.69.02026/09/261 个月前
1.68.02026/08/192 个月前

关于 @contrast/cli 的常见问题

@contrast/cli 是什么?

A collection of agent related CLI utilities

如何安装 @contrast/cli?

在项目目录运行 npm install @contrast/cli,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 rewrite、config-diagnostics、system-diagnostics,可以用 npx rewrite 直接运行。

@contrast/cli 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/contrast__cli 是否存在。

@contrast/cli 支持 ESM 还是 CommonJS?

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

@contrast/cli 需要什么版本的 Node.js?

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

@contrast/cli 可以商用吗?

@contrast/cli 使用 SEE LICENSE IN LICENSE 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@contrast/cli 的下载量有多少?

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

@contrast/cli 还在维护吗?

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