@eraserlabs/diagrams-cli

中文文档
0.5.2发布于 2026/11/12MITES Module构建来源已验证近 30 天下载 5187

Command-line interface for Eraser Diagrams: render and validate diagram JSON through @eraserlabs/diagrams.

原始使用文档
文档目录7 节
  1. Introduction
  2. Usage
  3. render options
  4. Global options
  5. Chromium
  6. Configuration file
  7. Output and exit codes

@eraserlabs/diagrams-cli

Introduction

@eraserlabs/diagrams-cli is the command-line interface for Eraser Diagrams. It renders diagram JSON to PNG or HTML and validates documents without a browser, using @eraserlabs/diagrams.

Usage

npm install -D @eraserlabs/diagrams-cli
npx eraser-diagrams render diagram.json -o diagram.png

Requires Node.js 22.23.4+, 24.22.1+, or 26.12.1+ and a Chromium-family browser on the machine (not bundled).

eraser-diagrams render <input...>     Render diagram JSON to PNG or HTML (needs Chromium)
eraser-diagrams validate <input...>   Validate diagram JSON without a browser
eraser-diagrams registry              Print the tag registry as JSON
eraser-diagrams schema <tag>          Print the JSON Schema of one tag
eraser-diagrams init                  Write eraser-diagrams.config.json in the current directory

<input> is a file path or - for stdin. Several inputs share one warm browser. Accepted documents are { "elements": [...] } or the split { "entities": [...], "connections": [...] }. A bare JSON array fails with E_ENVELOPE. A { "definition": { "elements": [...] } } wrapper is lifted with a note on stderr.

render options

FlagDefaultEffect
-o, --out <path><input>.<format> in the output directory (diagram.<format> for stdin)Output file for a single input. -o - writes the bytes to stdout.
--out-dir <dir>current directoryWhere outputs go; created if missing.
-f, --format png|htmlpngOutput format. HTML references each font's source (inline: true embeds file faces).
--scale <n>1PNG pixel density (deviceScaleFactor; 2 for retina). Ignored for HTML.
--transparentoffPreserve PNG background transparency. Ignored for HTML; authored fills remain visible.
--pages <n>1Warm Chromium page pool; more pages render more inputs concurrently.
--chromium-path <path>see belowChromium executable.
--fonts <path>stock fontsFonts config JSON file.
--icon-base-url <url>Eraser's public catalogIcon host: <url>/<name>.svg.
--icon-cache-dir <dir>noneOn-disk icon cache.
--unknown-icon placeholder|errorplaceholderUnknown icon names: placeholder glyph + warning, or a hard error.
--jsonoffMachine-readable report on stdout (nothing else goes to stdout).
--fail-on-warningoffExit 1 when any warning is reported (including degraded fonts).
-q, --quietoffNo per-input status lines; failures still print.
--debugoffStage timings and config/Chromium provenance on stderr.

For a PNG with a transparent background:

eraser-diagrams render diagram.json --transparent --scale 2 -o diagram.png

validate accepts --json, --fail-on-warning, --quiet, --debug.

Global options

FlagEffect
-c, --config <path>Config file to use.
--no-configIgnore config files.
--print-configPrint the effective configuration (after merging) and exit.
-h, --help, -v, --versionHelp / versions.

Chromium

Precedence: --chromium-path > CHROMIUM_PATH > chromiumPath in the config file > auto-detect. Auto-detect probes the usual Chrome and Chromium install locations and reports which one it picked. eraser-diagrams init writes the detected path into a config file so it stays pinned.

Configuration file

eraser-diagrams.config.json is found by walking up from the current directory (stopping at the first directory that contains .git), or given with --config or ERASER_DIAGRAMS_CONFIG. Precedence is flag > environment > config file > default. Relative paths in the file resolve against the file's own directory. Unknown keys are rejected.

{
  "chromiumPath": "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
  "format": "png",
  "outDir": "./diagrams",
  "deviceScaleFactor": 2,
  "pages": 2,
  "icons": {
    "baseUrl": "https://storage.googleapis.com/eraser-public-assets/canvas-icons/",
    "cacheDir": "./.eraser/icons",
    "timeoutMs": 5000,
    "cacheTtlMs": 86400000,
    "onUnknown": "placeholder"
  },
  "fonts": "./fonts.json",
  "library": "./my-library.mjs",
  "overrides": "./my-overrides.mjs",
  "failOnWarning": false
}
KeyTypeMeaning
chromiumPathstringChromium executable.
format"png" | "html"Default --format.
outDirstringDefault --out-dir.
deviceScaleFactornumberDefault --scale.
transparentbooleanDefault --transparent (false).
pagesnumberDefault --pages.
icons.baseUrl, icons.cacheDir, icons.timeoutMs, icons.cacheTtlMsIcon loader settings (createEraserIconLoader).
icons.onUnknown"placeholder" | "error"Unknown icon policy.
fontsobject or stringA FontsConfig object, or the path to a JSON file holding one. path / cachePath inside it resolve against the file that declares them.
librarystringES module path exporting library (an AuthoredLibrary) and optionally normalizers. Replaces the stock library; see CUSTOMIZATION.md.
overridesstringES module path exporting overrides ({ templates: TemplateFile[] }): component overrides merged over the library by name.
failOnWarningbooleanDefault --fail-on-warning.

Everything except --format and the output path is fixed when the browser boots, so it applies to the whole batch.

Output and exit codes

Status goes to stderr, one line per input, followed by its issues:

ok    diagram.json  → out/diagram.png  412 ms  1 warning
  warning W_UNKNOWN_ICON /elements/1/icon (Icon#api) — Unknown icon "nodejs"; using a placeholder glyph.
FAIL  other.json  E_UNKNOWN_TAG
  error   E_UNKNOWN_TAG /elements/0 (Shpe) — Unknown tag "Shpe". Did you mean "Shape"?

--json prints one report on stdout instead:

{
  "ok": false,
  "degradedFonts": [],
  "results": [
    {
      "input": "diagram.json",
      "out": "out/diagram.png",
      "ok": true,
      "errors": [],
      "warnings": [],
      "ms": 412
    },
    {
      "input": "other.json",
      "ok": false,
      "errors": [
        {
          "code": "E_UNKNOWN_TAG",
          "severity": "error",
          "path": "/elements/0",
          "message": "Unknown tag \"Shpe\".",
          "suggestion": "Shape"
        }
      ],
      "warnings": []
    }
  ]
}
Exit codeMeaning
0Every input succeeded (warnings allowed unless --fail-on-warning).
1At least one input failed: unreadable file, invalid JSON, validation errors, or a failed render.
2The invocation itself failed: bad flag, bad config file, no Chromium, custom library rejected.

Ctrl-C closes Chromium before exiting.

版本与发布记录

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

版本发布日期距今
0.5.2latest2026/11/122 天前
0.5.12026/11/059 天前
0.5.02026/11/0311 天前
0.4.32026/10/2816 天前
0.4.22026/10/2817 天前
0.4.12026/10/2817 天前

关于 @eraserlabs/diagrams-cli 的常见问题

@eraserlabs/diagrams-cli 是什么?

Command-line interface for Eraser Diagrams: render and validate diagram JSON through @eraserlabs/diagrams.

如何安装 @eraserlabs/diagrams-cli?

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

@eraserlabs/diagrams-cli 支持 TypeScript 吗?

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

@eraserlabs/diagrams-cli 支持 ESM 还是 CommonJS?

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

@eraserlabs/diagrams-cli 需要什么版本的 Node.js?

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

@eraserlabs/diagrams-cli 可以商用吗?

@eraserlabs/diagrams-cli 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@eraserlabs/diagrams-cli 的下载量有多少?

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

@eraserlabs/diagrams-cli 还在维护吗?

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