CLI 参考

本页内容

EmDash CLI 提供用于管理 EmDash CMS 实例的命令——数据库设置、类型生成、内容 CRUD、schema 管理、媒体等。

安装

CLI 包含在 emdash 包中。使用以下命令安装:

npm install emdash

使用 npx emdash 运行命令或将脚本添加到 package.json。二进制文件也可以用简写 em 来调用。

使用包脚本启动站点,例如 pnpm dev。包脚本启动 Astro;EmDash 集成生成 emdash-env.d.ts,运行时在首次请求时运行待执行的迁移,并在数据库为空且设置未完成时应用内置的种子。

身份验证

使用共享远程客户端的命令按以下顺序解析身份验证:

  1. --token 标志 — 命令行上的显式令牌
  2. EMDASH_TOKEN 环境变量
  3. 存储的凭证 来自 ~/.config/emdash/auth.json(由 emdash login 保存)
  4. 开发绕过 — 如果 URL 是 localhost 且没有可用令牌,自动通过开发绕过端点进行身份验证

这些命令接受 --url(来自 EMDASH_URL,回退到 http://localhost:4321)和 --token 标志。身份验证命令有自己的连接选项。当目标是本地开发服务器时,不需要令牌。

通用标志

这些标志可用于使用共享远程客户端的命令:

标志简写描述默认值
--url-uEmDash 实例 URLEMDASH_URLhttp://localhost:4321
--token-t身份验证令牌来自环境变量/存储的凭证
--header "Name: Value"-H自定义请求头;可重复使用来自 EMDASH_HEADERS/存储的凭证
--json以 JSON 格式输出(用于管道)从 TTY 自动检测

输出

对于使用共享远程客户端的命令,当输出是 TTY 时使用 consola 美化打印,当通过管道或设置了 --json 时变为原始 JSON。emdash migrate 仅在使用其显式 --json 选项时输出 JSON。

命令

emdash migrate

检查或应用由 Astro 构建发出的核心迁移集。

npx emdash migrate [options]
npx emdash migrate --check [options]
npx emdash migrate --status --json [options]

默认情况下,该命令发现项目根目录并读取 .emdash/migrations.json。它验证清单与项目安装的 EmDash 包是否匹配,解析适配器的项目本地执行器,并在执行任何 SQL 之前打印不可变目标。

选项

选项描述
--check不应用任何内容;如果有待执行或未知的迁移记录则以非零退出
--status报告确切状态而不应用;成功报告后以零退出
--json以稳定的迁移报告 JSON 格式输出
--manifest <path>读取非标准清单路径
--from-config显式评估受信任的 Astro 配置而不是清单
--config <path>--from-config 一起使用的 Astro 配置路径
--expected-target-fingerprint <sha256>非交互式应用时必需的守卫
--database <path>覆盖 SQLite 路径
--database-url-env <name>覆盖 PostgreSQL 连接变量名
--d1 <uuid-or-name>显式选择 D1 数据库
--account-id <id>显式选择 Cloudflare 账户
--wrangler-config <path>从显式的 Wrangler 配置读取 D1 绑定元数据
--wrangler-env <name>选择环境;需要 --wrangler-config

交互式人类可读应用会请求确认。非交互式应用和每次使用 --json 的应用都需要为目标打印的确切指纹。没有 down--dry-run;使用 --check 来确定是否需要工作。

退出码

代码含义
0成功,包括成功的 --status 报告
1验证、配置、目标、迁移或清理错误
2--check 发现待执行的已知迁移
3--check 发现未知的已应用记录(优先于待执行)
4缺少确认、被拒绝或目标指纹不匹配
130在有界执行器清理后被中断

参见管理核心数据库迁移了解部署顺序、目标凭证和 D1 序列化。

emdash dev(已弃用)

此旧版命令在启动 Astro 之前初始化并迁移本地 SQLite 数据库。该行为不使用站点配置的数据库适配器,且与 Cloudflare D1 开发不兼容。现有调用现在在执行任何数据库工作之前会打印弃用警告。

emdash types

从运行中的 EmDash 实例的 schema 生成 TypeScript 类型。

npx emdash types [options]

选项

选项简写描述默认值
--url-uEmDash 实例 URLhttp://localhost:4321
--token-t身份验证令牌来自环境变量/存储的凭证
--output-o类型输出路径.emdash/types.ts
--cwd工作目录当前目录

示例

# 从本地开发服务器生成类型
npx emdash types

# 从远程实例生成
npx emdash types --url https://my-site.pages.dev

# 自定义输出路径
npx emdash types --output src/types/emdash.ts

行为

  1. 从实例获取 schema
  2. 生成 TypeScript 类型定义
  3. 将类型写入输出文件
  4. 在旁边写入 schema.json 以供参考

emdash login

使用 OAuth 设备流登录 EmDash 实例。

npx emdash login [options]

选项

选项简写描述默认值
--url-uEmDash 实例 URLhttp://localhost:4321

行为

  1. 从实例发现身份验证端点
  2. 如果是 localhost 且未配置身份验证,自动使用开发绕过
  3. 否则启动 OAuth 设备流——显示代码并打开浏览器
  4. 轮询授权,然后将凭证保存到 ~/.config/emdash/auth.json

保存的凭证将被所有后续针对同一实例的命令自动使用。

emdash logout

注销并移除存储的凭证。

npx emdash logout [options]

选项

选项简写描述默认值
--url-uEmDash 实例 URLhttp://localhost:4321

emdash whoami

显示当前已认证的用户。

npx emdash whoami [options]

选项

选项简写描述默认值
--url-uEmDash 实例 URLhttp://localhost:4321
--token-t身份验证令牌来自环境变量/存储的凭证
--json以 JSON 格式输出

显示邮箱、名称、角色、身份验证方式和实例 URL。

emdash content

管理内容项。所有子命令通过 EmDashClient 使用远程 API。

content list <collection>

npx emdash content list posts
npx emdash content list posts --status published --limit 10
选项描述
--status按状态过滤
--limit最大项目数
--cursor分页游标

content get <collection> <id>

npx emdash content get posts 01ABC123
npx emdash content get posts 01ABC123 --raw
选项描述
--raw返回原始 Portable Text(跳过 markdown 转换)

响应包含 _rev 令牌。将其传递给 content update 以确认您在覆盖之前已看到当前状态。

content create <collection>

npx emdash content create posts --data '{"title": "Hello"}'
npx emdash content create posts --file post.json --slug hello-world
cat post.json | npx emdash content create posts --stdin
选项描述
--data包含内容数据的 JSON 字符串
--file从 JSON 文件读取数据
--stdin从标准输入读取数据
--slug内容 slug
--locale内容语言区域
--translation-of要将此项链接为其翻译的内容项 ID
--draft保持为草稿而不是自动发布

通过 --data--file--stdin 三者之一提供数据。除非设置了 --draft,否则新项目自动发布。

content update <collection> <id>

您必须提供来自先前 get_rev 令牌以证明您已看到当前状态。这可以防止覆盖您未看到的更改。以下步骤读取项目,然后使用该令牌更新它:

# 1. 读取项目,记录 _rev
npx emdash content get posts 01ABC123

# 2. 使用步骤 1 中的 _rev 更新
npx emdash content update posts 01ABC123 \
  --rev MToyMDI2LTAyLTE0... \
  --data '{"title": "Updated"}'
选项描述
--rev来自 get 的修订令牌(必需)
--data包含内容数据的 JSON 字符串
--file从 JSON 文件读取数据

如果项目在您 get 后已更改,服务器返回 409 Conflict——重新读取并重试。

content delete <collection> <id>

npx emdash content delete posts 01ABC123

软删除内容项(移至回收站)。

content publish <collection> <id>

npx emdash content publish posts 01ABC123

content unpublish <collection> <id>

npx emdash content unpublish posts 01ABC123

content schedule <collection> <id>

npx emdash content schedule posts 01ABC123 --at 2026-03-01T09:00:00Z
选项描述
--atISO 8601 日期时间(必需)

content restore <collection> <id>

npx emdash content restore posts 01ABC123

恢复已回收的内容项。

emdash schema

管理集合和字段。

schema list

npx emdash schema list

列出所有集合。

schema get <collection>

npx emdash schema get posts

显示集合及其所有字段。

schema create <collection>

npx emdash schema create articles --label Articles
npx emdash schema create articles --label Articles --label-singular Article --description "Blog articles"
选项描述
--label集合标签(必需)
--label-singular单数标签
--description集合描述

schema delete <collection>

npx emdash schema delete articles
npx emdash schema delete articles --force
选项描述
--force跳过确认

除非设置了 --force,否则提示确认。

schema add-field <collection> <field>

npx emdash schema add-field posts body --type portableText --label "Body Content"
npx emdash schema add-field posts featured --type boolean --required
选项描述
--type字段类型:string、text、number、integer、boolean、datetime、image、reference、portableText、json(必需)
--label字段标签(默认为字段 slug)
--required该字段是否必需

schema remove-field <collection> <field>

npx emdash schema remove-field posts featured

emdash media

管理媒体项。

media list

npx emdash media list
npx emdash media list --mime image/png --limit 20
选项描述
--mime按 MIME 类型过滤
--limit项目数量
--cursor分页游标

media upload <file>

npx emdash media upload ./photo.jpg
npx emdash media upload ./photo.jpg --alt "A sunset" --caption "Taken in Bristol"
选项描述
--alt替代文本
--caption标题文本

media get <id>

npx emdash media get 01MEDIA123

media delete <id>

npx emdash media delete 01MEDIA123

media repair-usage

修复一个集合或所有内容集合的内容媒体使用索引。在导入或直接数据库写入后当使用覆盖率已过时或不可信时使用。

npx emdash media repair-usage --collection posts
npx emdash media repair-usage --all
npx emdash media repair-usage --all --json
选项简写描述
--collection-c修复一个内容集合
--all修复所有内容集合

传入 --collection--all 之一。远程修复需要管理员用户和具有 admin 范围的身份验证令牌。

全内容修复同步运行,在大型站点上可能很慢或开销大。当只需要修复一个集合时优先使用 --collection

结构化的 completepartialstale 修复结果以 0 退出;结构化的 failed 结果以 1 退出。自动化和定时任务应使用 --json 并解析 statusfailedSourceCountskippedSourceCount 和每个集合的摘要,而不是将退出 0 视为完整覆盖。

跨内容全文搜索。

npx emdash search "hello world"
npx emdash search "hello" --collection posts --limit 5
选项简写描述
--collection-c按集合过滤
--limit-l最大结果数

emdash taxonomy

管理分类法和术语。

taxonomy list

npx emdash taxonomy list

taxonomy terms <name>

npx emdash taxonomy terms categories
npx emdash taxonomy terms tags --limit 50
选项简写描述
--limit-l最大术语数
--cursor分页游标

taxonomy add-term <taxonomy>

npx emdash taxonomy add-term categories --name "Tech" --slug tech
npx emdash taxonomy add-term categories --name "Frontend" --parent 01PARENT123
选项描述
--name术语标签(必需)
--slug术语 slug(默认为 slugify 后的名称)
--parent父术语 ID(用于层级分类法)

emdash menu

管理导航菜单。

npx emdash menu list
npx emdash menu get primary

返回菜单及其所有项目。

emdash export-seed

将数据库 schema 和内容导出为种子文件。直接操作本地 SQLite 文件。

npx emdash export-seed [options] > seed.json

选项

选项简写描述默认值
--database-d数据库文件路径./data.db
--cwd工作目录当前目录
--with-content包含内容(全部或逗号分隔的集合)
--no-pretty禁用 JSON 格式化false

输出格式

导出的种子文件包含:

  • 设置:站点标题、标语、社交链接
  • 集合:所有集合定义及其字段
  • 分类法:分类法定义和术语
  • 菜单:导航菜单及其项目
  • 小部件区域:小部件区域和小部件
  • 内容(如果请求):带有 $media 引用和 $ref: 语法的条目以便移植

emdash secrets generate

为部署生成 EMDASH_ENCRYPTION_KEY。该密钥用于加密静态存储的插件密钥。

npx emdash secrets generate

将新密钥打印到标准输出。将其通过管道传入您的密钥存储,或使用 --write 直接写入本地 .env 文件。同一 .env 文件由 Node 读取,在本地开发中也由 Wrangler 和 Cloudflare Vite 插件读取:

npx emdash secrets generate --write .env

--write 在没有 --force 的情况下拒绝覆盖现有条目。在具有现有加密数据的部署中替换密钥将使这些密钥不可读,因此这种保护是有意为之的。

emdash secrets fingerprint <key>

打印密钥的 8 字符指纹(kid),而不暴露其值。这在 CI 中用于验证是否部署了正确的密钥。以下命令打印密钥的指纹:

npx emdash secrets fingerprint emdash_enc_v1_...

生成的文件

.emdash/types.ts

emdash types 命令为每个集合生成 TypeScript 接口:

// Generated by EmDash CLI
// Do not edit manually - run `emdash types` to regenerate

import type { PortableTextBlock } from "emdash";

export interface Post {
	id: string;
	title: string;
	content: PortableTextBlock[];
	publishedAt: Date | null;
}

.emdash/schema.json

该命令还写入原始 schema 导出以供工具使用:

{
  "version": "a1b2c3d4",
  "collections": [
    {
      "slug": "posts",
      "label": "Posts",
      "fields": [...]
    }
  ]
}

环境变量

变量描述
EMDASH_DATABASE_URL覆盖数据库 URL
EMDASH_TOKEN用于远程操作的身份验证令牌
EMDASH_URL使用共享远程客户端的命令的默认 URL
EMDASH_HEADERS用于共享远程客户端和 login 的换行符分隔的自定义请求头
EMDASH_ENCRYPTION_KEY用于加密静态存储的插件密钥的密钥。由运维人员提供——永远不存储在数据库中。使用 emdash secrets generate 生成。
EMDASH_PREVIEW_SECRET可选的预览 HMAC 密钥覆盖。未设置时,EmDash 会在 options 表中生成并持久化一个。
EMDASH_IP_SALT可选的评论者 IP 哈希盐覆盖。未设置时,EmDash 会在 options 表中生成并持久化一个。
EMDASH_AUTH_SECRET旧版。如果设置,用作 IP 盐来源,因此现有安装在升级后保持稳定的评论者 IP 哈希。新安装不应设置此项。

包脚本

为方便起见,将常用命令添加为 package.json 脚本:

{
	"scripts": {
		"dev": "astro dev",
		"types": "emdash types",
		"export-seed": "emdash export-seed",
		"db:reset": "rm -f data.db"
	}
}

退出码

代码描述
0成功
1错误(配置、网络、数据库)