NocoBase CLI 自启动标记开启指南:nb app autostart enable命令详解与源码原理
【免费下载链接】nocobaseNocoBase is an open-source AI + no-code platform for building business systems fast. Instead of generating everything from scratch, AI works on top of production-proven infrastructure and a WYSIWYG no-code interface, so you get both speed and reliability.项目地址: https://gitcode.com/GitHub_Trending/no/nocobase
导读
nb app autostart enable是 NocoBase CLI(nb)中用于为指定local或dockerenv 开启“应用自启动标记”的命令。它本身不会立刻启动应用,而是把目标 env 写入后续nb app autostart run的批量启动名单,是搭建systemd、容器平台启动脚本等宿主机自启动流程的第一步。读完本文,你将掌握该命令的完整用法、参数语义、跨 env 交互确认机制,以及标记在 CLI 配置文件中的持久化原理与底层实现。
命令定位:autostart 命令组中的“开关注册”一环
nb app autostart命令组负责管理应用自启动相关设置,分为两类职责:
- 为某个 env 开启或关闭自启动标记(
enable/disable) - 在系统启动后批量启动所有已开启自启动的 env(
run),并通过list查看状态
从文档结构看(docs/docs/cn/api/cli/app/autostart/index.md),nb app autostart只适用于当前机器上由 CLI 托管运行态的 env,也就是local和docker。如果一个 env 只是远程 API 连接(例如kind: http),或者不是当前机器上可启动的 CLI 托管应用,就不能加入自动启动流程。enable是这组命令的入口:没有它,run就没有可启动的名单。
用法与参数
基本用法
nb app autostart enable [flags]参数一览
| 参数 | 类型 | 说明 |
|---|---|---|
--env,-e | string | 要加入自启动的 CLI env 名称,省略时使用当前 env |
--yes,-y | boolean | 当显式--env指向的 env 与当前 env 不一致时,跳过交互确认,默认false |
这两个 flag 在源码中的定义位于 packages/core/cli/src/commands/app/autostart/enable.ts#L26-L36:env是可选的字符串 flag,yes是默认关闭的布尔 flag。命令的描述为 “Enable app autostart for the selected local or Docker env.”。
典型示例
# 为当前 env 开启自启动 nb app autostart enable # 为指定 env 开启自启动(可能触发交互确认) nb app autostart enable --env app1 # 为指定 env 开启自启动,并跳过跨 env 交互确认 nb app autostart enable --env app1 --yes执行成功时,命令会输出一行Enabled app autostart for "<envName>".作为确认。
跨 env 交互确认机制
命令的核心行为之一是“只有显式传入--env时,CLI 才会检查它是否与当前 env 一致”。在 enable.ts#L42-L51 中可以看到完整流程:
const requestedEnv = flags.env?.trim() || undefined; if (requestedEnv && hasExplicitEnvSelection(this.argv)) { const confirmed = await ensureCrossEnvConfirmed({ command: this, requestedEnv, yes: flags.yes, }); if (!confirmed) { return; } }hasExplicitEnvSelection会检查argv中是否确实出现了--env;只有满足“显式指定了 env”且“与当前 env 不同”两个条件时,才会走ensureCrossEnvConfirmed交互确认。这意味着:
- 省略
--env:直接使用当前 env,不会触发确认; - 显式
--env指向当前 env:无确认; - 显式
--env指向其他 env:交互终端弹出确认; - 在非交互终端或 AI agent 场景下:必须显式追加
--yes,或者先执行nb env use <name>切换当前 env 再重试。
执行前的运行时校验
确认通过后,命令调用getEnv(requestedEnv)解析目标 env。如果 env 不存在或没有名称,会调用formatMissingManagedAppEnvMessage直接报错退出(enable.ts#L53-L56)。
随后写入标记前,updateAutostartSetting内部还会执行一次运行时校验。在 packages/core/cli/src/commands/app/autostart/shared.ts#L17-L32 中,ensureManagedAutostartRuntime会检查 env 的kind与source:
const kind = String(config.kind ?? '').trim(); const source = String(config.source ?? '').trim(); const inferredLocal = source === 'npm' || source === 'git' || source === 'local'; if (kind === 'local' || kind === 'docker' || (!kind && (source === 'docker' || inferredLocal))) { return; }也就是说,以下 env 被判定为“可自启动”:
kind显式为local或docker;- 或者
kind未设置,但source为docker、npm、git、local(推断为本地托管运行态)。
其余类型(如kind: http的远程连接)会抛出错误:Env "<name>" cannot be added to app autostart. Only local and Docker envs with CLI-managed app runtimes can be started automatically on this machine.因此,如果目标 env 不是当前机器上 CLI 托管运行态的local或dockerenv,命令会直接报错且不会保存任何标记,这与文档说明一致。
标记的持久化与存储位置
enable最终通过updateAutostartSetting(envName, true)写标记,实现在 shared.ts#L34-L65:
const config = await loadExactAuthConfig(); const previous = config.envs[envName]; if (!previous) { throw new Error(`Env "${envName}" is not configured`); } ensureManagedAutostartRuntime({ ...previous, name: envName }); const next = { ...previous }; next.autostart = { enabled: true }; config.envs[envName] = next; if (wasEnabled !== enabled) { await saveAuthConfig(config); }可见标记以autostart: { enabled: true }的形式挂在 env 配置条目上(类型定义见 packages/core/cli/src/lib/auth-store.ts#L58)。值得注意的细节:
- 若目标 env 尚未配置,会报
Env "<envName>" is not configured; - 若该 env 原本就已是
enabled: true(幂等场景),changed为false,不会重复写盘; disable方向则直接delete next.autostart,两条命令共用同一updateAutostartSetting。
CLI 的 env 配置最终落在~/.nocobase/config.json(通过 packages/core/cli/src/lib/cli-home.ts#L13-L37 中的CLI_HOME_DIRNAME = '.nocobase'与resolveCliHomeDir解析,并可用环境变量NB_CLI_ROOT覆盖根目录)。也就是说,执行nb app autostart enable后,可以直接在该 JSON 文件中看到对应 env 的"autostart": { "enabled": true }字段。
开启自启动后:与run/list/disable的协同
enable只负责“登记”,真正拉起应用的是nb app autostart run。根据 docs/docs/cn/api/cli/app/autostart/run.md 与命令组文档,run会读取所有已保存 env、筛出自启动标记已开启的条目,逐个调用nb app start --env <name> --yes,并输出结果表:
- 能正常启动的显示
started - 不适合在当前机器上自动启动的显示
skipped - 启动报错的显示
failed
只要存在failed,命令最终以错误退出并输出Some app autostart envs failed to start.,以便systemd、CI 等主机启动机制能明确感知失败。run支持--verbose透传底层启动输出。
日常管理闭环如下:
# 1. 开启自启动(本文主角) nb app autostart enable --env app1 --yes # 2. 查看所有 env 的自启动状态 nb app autostart list # Current|Env|Kind|Source|Autostart # * |app1|local|npm |yes # |app2|docker|docker|yes # |remote1|http|- |no # 3. 系统启动后批量拉起 nb app autostart run nb app autostart run --verbose # 4. 关闭某 env 的自启动(不会停止已运行的应用) nb app autostart disable --env app1 --yeslist输出表包含Current、Env、Kind、Source、Autostart五列,当前 env 以*标注;若尚未配置任何 env,则输出No environments are configured.。disable只移除标记、不停止应用,若需停止运行中的实例还要执行nb app stop。
从测试用例看行为契约
命令行为由 packages/core/cli/src/tests/app-autostart-commands.test.ts 固化,其中与enable直接相关的两个用例可作为可验证依据:
- enable 在选中 env 上写入 autostart:对
app1(kind: local)执行 enable 后,断言state.envs.app1?.autostart等于{ enabled: true },且日志输出Enabled app autostart for "app1".; - enable 拒绝不支持的 env 类型:对
remote1(kind: http)执行--env remote1时,断言抛出Env "remote1" cannot be added to app autostart.错误。
测试中的run用例还验证了run会按保存顺序调用app:start --env <name> --yes,且单个 env 失败不影响后续 env 启动(失败后继续处理app2),最终以错误退出——这与文档描述的批量启动与失败感知语义完全一致。
典型部署落地:接入 systemd
一个常见的生产落地方式是把run接进宿主机自启动流程。示例(需按实际环境调整):
# /etc/systemd/system/nocobase-autostart.service [Unit] Description=NocoBase app autostart After=network-online.target docker.service Wants=network-online.target [Service] Type=oneshot User=nocobase ExecStart=/path/to/nb app autostart run --yes RemainAfterExit=yes [Install] WantedBy=multi-user.target整体流程为:先用nb app autostart enable --env <name> --yes为各 env 打上标记(标记持久化在~/.nocobase/config.json),再让系统启动机制执行nb app autostart run;run内部逐个调用nb app start --env <name> --yes拉起所有已启用 env,并通过failed状态与错误退出码让上层机制感知失败。
相关命令速查
nb app autostart disable:关闭指定 env 的自启动标记nb app autostart list:查看所有 env 的自启动状态nb app autostart run:批量启动所有已开启自启动的 envnb app start/nb app stop:单个 env 的启动与停止nb env list/nb env use:查看与切换当前 env
【免费下载链接】nocobaseNocoBase is an open-source AI + no-code platform for building business systems fast. Instead of generating everything from scratch, AI works on top of production-proven infrastructure and a WYSIWYG no-code interface, so you get both speed and reliability.项目地址: https://gitcode.com/GitHub_Trending/no/nocobase
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考