|
| 1 | +--- |
| 2 | +title: CLI 参考 |
| 3 | +seoTitle: "CLI 参考:每个 os 命令及其参数" |
| 4 | +description: 每个 `os` 命令、它做什么,以及最有用的参数。 |
| 5 | +editions: [community, cloud, enterprise] |
| 6 | +translation: |
| 7 | + source_sha: 3a77aa68e28e880be0de253974a5977e4412d168b16c73e544224ce1bdd82412 |
| 8 | + guide_rev: 1 |
| 9 | + mode: auto |
| 10 | +--- |
| 11 | + |
| 12 | +`@objectstack/cli` 包会安装一个单一的二进制文件 `os`(也可以用 `objectstack`)。 |
| 13 | +下面所有示例都使用更短的 `os`。 |
| 14 | + |
| 15 | +```bash |
| 16 | +npm i -g @objectstack/cli |
| 17 | +os --help |
| 18 | +``` |
| 19 | + |
| 20 | +## 服务器命令 [#server-commands] |
| 21 | + |
| 22 | +### `os start` —— 零配置启动 [#os-start--zero-config-boot] |
| 23 | + |
| 24 | +自动检测 4 种逐级递进的模式: |
| 25 | + |
| 26 | +1. **空启动** —— 当前目录中既没有产物也没有配置 → 启动一个带 UI + 市场的裸内核。 |
| 27 | +2. **项目启动** —— 当前目录中有 `objectstack.config.ts` → 自动编译到 |
| 28 | + `./dist/objectstack.json` 并从它启动。 |
| 29 | +3. **产物启动** —— 能找到 `dist/objectstack.json` → 直接从它启动。 |
| 30 | +4. **显式覆盖** —— `--artifact`、`--database`、`--port` 优先。 |
| 31 | + |
| 32 | +```bash |
| 33 | +os start # default port 3000 |
| 34 | +os start --port 8080 |
| 35 | +os start --artifact ./build/myapp.json |
| 36 | +os start --artifact https://cdn.example.com/app.json |
| 37 | +os start --database file:./data/prod.db |
| 38 | +os start --database postgres://user:pass@host:5432/mydb |
| 39 | +os start --database libsql://my-db.turso.io --database-auth-token $TURSO |
| 40 | +os start --auth-secret "$(openssl rand -hex 32)" |
| 41 | +os start --home /var/lib/objectos # persistent home dir |
| 42 | +os start --no-ui # API only (no UI/Account portals) |
| 43 | +``` |
| 44 | + |
| 45 | +HOME 目录默认值: |
| 46 | +- 当前目录中有项目配置 → `<cwd>/.objectstack`(项目本地) |
| 47 | +- 没有 → `~/.objectstack`(全局,在多次调用之间共享) |
| 48 | + |
| 49 | +### `os serve` —— 生产服务器 [#os-serve--production-server] |
| 50 | + |
| 51 | +如果存在 `objectstack.config.ts` 就读取它,否则回落到 `dist/objectstack.json`。当你 |
| 52 | +希望行为严格时,在生产容器中使用它。 |
| 53 | + |
| 54 | +如果要改为启动一个**已发布的**产物,就用 `OS_ARTIFACT_URL` 指定它 —— 一个绝对的 |
| 55 | +`https://`、`http://` 或 `file://` URL,可以在片段中带一个可选的 `#sha256=` 完整性 |
| 56 | +固定值。它在读取任何配置之前解析。旧的 `OS_ARTIFACT_PATH` 写法已经退役:在 |
| 57 | +ObjectOS 运行时镜像上,非默认值会**拒绝启动**并点名替代项。参见 |
| 58 | +[环境变量](/docs/reference/environment-variables#retired-names)。 |
| 59 | + |
| 60 | +```bash |
| 61 | +os serve # port 3000 |
| 62 | +os serve --port 4000 --no-ui |
| 63 | +``` |
| 64 | + |
| 65 | +### `os dev` —— 带热重载的开发 [#os-dev--development-with-hot-reload] |
| 66 | + |
| 67 | +监视 `objectstack.config.ts` 和 `src/`。保存时重新编译 + 重新加载。在脚手架生成的 |
| 68 | +项目中用作 npm 的 `dev` 脚本。 |
| 69 | + |
| 70 | +```bash |
| 71 | +os dev # port 3000 by default |
| 72 | +os dev -p 4000 |
| 73 | +``` |
| 74 | + |
| 75 | +注意,`os dev` 请求的端口与 `os start` 相同(3000)。如果该端口被占用,它会绑定下一个 |
| 76 | +空闲端口并说明:`↪ server bound to port 3001 (requested 3000)`。 |
| 77 | + |
| 78 | +## 项目命令 [#project-commands] |
| 79 | + |
| 80 | +### `os init` —— 搭建新项目 [#os-init--scaffold-a-new-project] |
| 81 | + |
| 82 | +```bash |
| 83 | +os init my-app # interactive |
| 84 | +os init my-app -t app --install # auto-install with default pnpm |
| 85 | +os init my-app -t app --install -p npm |
| 86 | +os init my-app -t plugin # plugin scaffold |
| 87 | +os init my-app -t empty # bare minimum |
| 88 | +``` |
| 89 | + |
| 90 | +模板: |
| 91 | +| 模板 | 你会得到什么 | |
| 92 | +|---|---| |
| 93 | +| `app` | 完整的应用 —— 对象、视图、动作,随时可以扩展 | |
| 94 | +| `plugin` | 用于分发可复用能力的插件脚手架 | |
| 95 | +| `empty` | 只有清单 + tsconfig | |
| 96 | + |
| 97 | +### `os create` —— 搭建内核代码插件 [#os-create--scaffold-a-kernel-code-plugin] |
| 98 | + |
| 99 | +```bash |
| 100 | +os create plugin my-plugin |
| 101 | +``` |
| 102 | + |
| 103 | +`os create plugin` 会写一个实现内核 `Plugin` 契约的 TypeScript 包,这是 `os init` |
| 104 | +不做的。`os create example` 已在 ObjectStack 17.4 中退役:它写的内容是 `os init` |
| 105 | +所写内容的一个子集,现在它会报错退出,并点名 `os init`。 |
| 106 | + |
| 107 | +## 构建 / 校验命令 [#build--validate-commands] |
| 108 | + |
| 109 | +| 命令 | 用途 | |
| 110 | +|---|---| |
| 111 | +| `os compile`(`os build`) | 把 `objectstack.config.ts` 编译为 `dist/objectstack.json` | |
| 112 | +| `os validate` | 依据协议 schema 校验配置 | |
| 113 | +| `os lint` | 风格和约定检查(推荐在 CI 中使用) | |
| 114 | +| `os diff <old> <new>` | 比较两份配置,检测破坏性变更 | |
| 115 | +| `os info` | 打印配置或产物的元数据摘要 | |
| 116 | +| `os explain <object>` | 对象 schema 的人类可读说明 | |
| 117 | +| `os doctor` | 健康检查:检测循环依赖、损坏的引用、环境问题 | |
| 118 | +| `os generate`(`os g`) | 生成 TypeScript 类型 / 元数据文件 | |
| 119 | +| `os db clean` | 一次性的 SQLite `VACUUM` + 启用增量自动清理;报告回收的字节数(14.5+) | |
| 120 | + |
| 121 | +`os doctor` 是**感觉不对劲时首先要运行的命令** —— 它能捕获运行时只会以运行时警告 |
| 122 | +形式呈现的错误配置。 |
| 123 | + |
| 124 | +从 ObjectStack 13 起,`os compile` 还会按**安全姿态**门控构建: |
| 125 | +`validateSecurityPosture` 错误(未设置/使用别名的共享模型、高权限锚点绑定、残留的 |
| 126 | +角色词汇、未带对象限定的字段权限键……)会让构建失败,而 `os lint` 会在更早的环节 |
| 127 | +报告它们。如果你提交了一个 `access-matrix.json` 快照,任何权限能力的漂移都会让 |
| 128 | +`os compile` 失败,直到用 `--update-access-matrix` 重新生成快照 —— 每一次能力变更 |
| 129 | +都变得可以评审。 |
| 130 | + |
| 131 | +## 数据命令 [#data-commands] |
| 132 | + |
| 133 | +在终端中操作记录 —— 适用于种子数据、迁移和 CI 冒烟测试。 |
| 134 | + |
| 135 | +```bash |
| 136 | +os data create <object> --data '{"subject": "Hello"}' |
| 137 | +os data get <object> <id> |
| 138 | +os data query <object> --filter 'status:active' --limit 10 |
| 139 | +os data update <object> <id> --data '{"done": true}' |
| 140 | +os data delete <object> <id> |
| 141 | +``` |
| 142 | + |
| 143 | +## 元数据命令 [#metadata-commands] |
| 144 | + |
| 145 | +在运行时读写元数据记录(对象、视图、权限集……)。 |
| 146 | + |
| 147 | +```bash |
| 148 | +os meta list <kind> |
| 149 | +os meta get <kind> <id> |
| 150 | +os meta register <file> |
| 151 | +os meta delete <kind> <id> |
| 152 | +``` |
| 153 | + |
| 154 | +## 数据库迁移 —— `os migrate` [#database-migrations--os-migrate] |
| 155 | + |
| 156 | +`os migrate` 让部署的数据库与其元数据保持一致,并运行某个版本所要求的一次性数据 |
| 157 | +迁移。每个子命令都作用于 `--database-url` 指定的数据库,它默认为 `$OS_DATABASE_URL`, |
| 158 | +然后是项目自己的数据库 —— 所以请显式指定部署的数据库。每个命令都会启动应用来读取 |
| 159 | +其元数据。先备份数据库。某次升级需要哪些命令,见 |
| 160 | +[升级与回滚](/docs/operate/upgrade#version-specific-steps)。 |
| 161 | + |
| 162 | +### 模式 [#schema] |
| 163 | + |
| 164 | +| 命令 | 做什么 | |
| 165 | +|---|---| |
| 166 | +| `os migrate plan` | 显示数据库与元数据之间的偏差,分为安全、需确认和破坏性三类变更。不写入任何内容 | |
| 167 | +| `os migrate apply` | 在一次确认提示后应用非破坏性变更(`--yes` 跳过提示)。`--allow-destructive` 还会删除孤立的列、收紧 `NOT NULL` 并收窄类型 | |
| 168 | +| `os migrate multi-value-columns` | 在字段声明了 `multiple: true` 的地方,把过时的文本列迁移为 JSON,这是 `apply` 从不做的那一种类型变更。在加 `--apply` 之前都是试运行 | |
| 169 | +| `os migrate unmapped-columns --object NAME` | 读取已退役字段的列中残留的值,以记录 id 为键。只读。从 17.7 起没有任何运行时入口会返回这些列,所以转换要在 `apply --allow-destructive` 删除它们之前用这个命令读取 | |
| 170 | +| `os migrate duplicates` | 以 JSON 报告已被签发两次的标识符,以及阻止唯一索引建立的行。只读 | |
| 171 | + |
| 172 | +```bash |
| 173 | +os migrate plan # what would change; writes nothing |
| 174 | +os migrate apply --allow-destructive --yes # also drop and tighten (CI / scripts) |
| 175 | +os migrate unmapped-columns --object contact --json > contact-unmapped.json |
| 176 | +``` |
| 177 | + |
| 178 | +### 数据迁移 [#data-migrations] |
| 179 | + |
| 180 | +这些命令改写的是行而不是模式,它们关乎的是你的数据库,而不是你安装的版本:运行时 |
| 181 | +不会替你运行它们。除非其所在行另有说明,否则在你传入 `--apply` 之前,每个命令都是 |
| 182 | +试运行。 |
| 183 | + |
| 184 | +| 命令 | 做什么 | |
| 185 | +|---|---| |
| 186 | +| `os migrate files-to-references` | (17.0)把内联存储的文件字段值转换为 `sys_file` 引用。`--apply` 会记录为本部署开启严格媒体值的标志 | |
| 187 | +| `os migrate value-shapes` | (17.0)依据契约扫描引用值和结构化 JSON 值。一旦扫描没有发现任何问题,`--apply` 会记录它的标志 | |
| 188 | +| `os migrate meta --from 17` | 在应用的**源码**上重放元数据转换链,并列出它做的编辑;`--out FILE` 写出转换后的技术栈。这是一个编写步骤:它不触及数据库 | |
| 189 | +| `os migrate meta --stored` | 在本部署**存储的**元数据上执行同样的转换;`--apply` 会改写这些行 | |
| 190 | +| `os migrate account-issuer` | (17.5)检查 `sys_account.issuer` 列能否被删除。只读;只要有两个账户会冲突,就以非零状态退出 | |
| 191 | +| `os migrate audit-metadata-bodies` | (17.6)改写审计轨迹和活动流中保存的元数据正文副本,移除其中的凭据和存储的内容哈希 | |
| 192 | +| `os migrate summary-nulls` | 填充较旧父记录上仍以 `NULL` 存储的汇总 `count` / `sum` 列 | |
| 193 | +| `os migrate recorded-by` | 把元数据历史中遗留的 `'system'` 标记改写为 `NULL` | |
| 194 | +| `os migrate resume` | 列出被中断的迁移运行。在加 `--run ID` 之前都是只读的,加上后会恢复或撤销其中一个 | |
| 195 | + |
| 196 | +```bash |
| 197 | +os migrate account-issuer # read-only pre-flight |
| 198 | +os migrate audit-metadata-bodies # dry run: what it would rewrite |
| 199 | +os migrate audit-metadata-bodies --apply # rewrite |
| 200 | +``` |
| 201 | + |
| 202 | +### 密钥和存储的文件 [#secrets-and-stored-files] |
| 203 | + |
| 204 | +| 命令 | 做什么 | |
| 205 | +|---|---| |
| 206 | +| `os secret rewrap` | 以 17.7 格式重新封装较旧的 `sys_secret` 密文。在加 `--apply` 之前都是试运行;必须告诉它主机是否在代码中声明了数据源(`--declared-datasources FILE` 或 `--no-declared-datasources`)。⚠️ 17.7 之前的版本无法打开它写入的内容 | |
| 207 | +| `os secret orphans` | 报告不再被任何东西引用的 `sys_secret` 行。只有加 `--delete` 才会删除 | |
| 208 | +| `os storage orphans` | 报告没有任何东西持有、也没有任何保留策略会回收的存储文件。不写入任何内容 | |
| 209 | + |
| 210 | +## i18n 命令 [#i18n-commands] |
| 211 | + |
| 212 | +```bash |
| 213 | +os i18n extract # extract translation keys from source |
| 214 | +os i18n check # find missing keys across configured locales |
| 215 | +``` |
| 216 | + |
| 217 | +## 云命令 [#cloud-commands] |
| 218 | + |
| 219 | +面向 ObjectOS Cloud(可选的托管控制平面)的用户。 |
| 220 | + |
| 221 | +```bash |
| 222 | +os login # interactive |
| 223 | +os logout |
| 224 | +os whoami |
| 225 | +os register # create an account |
| 226 | +os cloud login | logout | whoami |
| 227 | +``` |
| 228 | + |
| 229 | +## 发布 / 打包命令 [#publish--package-commands] |
| 230 | + |
| 231 | +```bash |
| 232 | +os package publish # publish artifact as a versioned package |
| 233 | +os package install <id|file> # install into a RUNNING runtime: catalog id |
| 234 | + # (com.acme.crm) or a compiled artifact JSON |
| 235 | + # (air-gapped, inline). --runtime / --email / |
| 236 | + # --password identify the runtime to install into |
| 237 | +``` |
| 238 | + |
| 239 | +## 环境命令 [#environment-commands] |
| 240 | + |
| 241 | +面向每个项目使用多个环境(dev、staging、prod)并由控制平面支持的部署。 |
| 242 | + |
| 243 | +```bash |
| 244 | +os environments list |
| 245 | +os environments show <id> |
| 246 | +os environments create <name> |
| 247 | +os environments switch <id> |
| 248 | +os environments bind # bind local artifact to an environment |
| 249 | +``` |
| 250 | + |
| 251 | +## 测试命令 [#test-command] |
| 252 | + |
| 253 | +```bash |
| 254 | +os test # run Quality Protocol scenarios against a running server |
| 255 | +``` |
| 256 | + |
| 257 | +## 通用参数约定 [#common-flag-conventions] |
| 258 | + |
| 259 | +| 参数 | 含义 | |
| 260 | +|---|---| |
| 261 | +| `-p, --port` | HTTP 端口 | |
| 262 | +| `-a, --artifact` | 编译后产物的路径或 URL | |
| 263 | +| `-d, --database` | 数据库 URL | |
| 264 | +| `--home` | 持久化状态目录 | |
| 265 | +| `-v, --verbose` | 详细输出 | |
| 266 | +| `--no-ui` | 禁用 UI 和 Account 门户 | |
| 267 | + |
| 268 | +任何命令加上 `--help` 都可以查看完整的参数列表: |
| 269 | + |
| 270 | +```bash |
| 271 | +os start --help |
| 272 | +os data query --help |
| 273 | +``` |
| 274 | + |
| 275 | +## CLI 的定位 [#where-the-cli-fits] |
| 276 | + |
| 277 | +- **运维 / 首次运行** → `os start` |
| 278 | +- **构建应用的开发者** → `os init` → `os dev` → `os compile` |
| 279 | +- **CI / 发布流水线** → `os lint && os validate && os compile && os test` |
| 280 | +- **生产运行时** → `os serve`(通常在 Docker 镜像的 `CMD` 中) |
| 281 | +- **升级部署** → [升级页面](/docs/operate/upgrade#version-specific-steps)为你的 |
| 282 | + 版本列出的 `os migrate` 步骤 |
| 283 | +- **诊断** → `os doctor`、`os info`、`os explain` |
0 commit comments