跳到主要内容

初始化配置

cloudbaserc.json 是云开发项目的核心配置文件。您可以通过 tcb config init 命令交互式初始化。

tcb config init

tcb config init 命令用于快速初始化 cloudbaserc.json 配置文件。支持以下三种模式:

  • 综合初始化(无模块参数):智能检测项目资源,交互式生成包含云函数、静态托管、网关的完整配置
  • 云函数初始化(模块 fn):创建云函数配置模板
  • 集成初始化(模块 integration):创建集成配置模板

命令格式

tcb config init [module] [options]

其中 [module] 为可选参数,表示要初始化的模块:

模块说明
(无参数)智能检测项目资源,生成综合配置模板
fn初始化云函数配置模板
integration初始化集成配置模板

命令参数(综合初始化)

参数说明必填
--no-directory不生成配套目录骨架(仅写配置)
-o, --output <output>输出文件路径,默认为当前目录下的 cloudbaserc.json

命令参数(模块初始化)

参数说明必填
[keyId]云函数时为函数名,集成时为集成 keyId否(fn 模块)
--type <type>集成类型(仅 integration 模块),如 weixinpaydc否(交互式选择)
-o, --output <output>输出文件路径,默认为当前目录下的 cloudbaserc.json

智能检测(无模块参数)

不带模块参数执行 tcb config init 时,CLI 会自动检测项目中的已有资源:

  • 云函数检测:扫描 functions/ 目录,通过 inferFunctionConfig 推测每个函数的运行时(Node.js/Python/PHP 等)、入口函数和类型(Event/HTTP),scf_bootstrap 文件会被自动识别为 HTTP 函数
  • 框架检测:与 tcb deploy 一致的 detectFramework 逻辑,识别 Vite/Next.js/Nuxt/Angular/Vue CLI 等框架(通过 vite.config.jsnext.config.js 等配置文件 + package.json 依赖双重检测)
  • 静态托管检测:智能识别 dist/build/web/public/ 等目录,优先匹配框架输出目录
  • 自动填充:已登录时自动填充默认环境 ID;package.jsonscripts.build 脚本自动写入 buildCommand

检测完成后展示摘要,让用户确认是否基于检测结果生成配置。确认后生成 cloudbaserc.json 并创建配套目录骨架。

使用示例

综合初始化(推荐)

# 智能检测项目资源,交互式生成完整配置
tcb config init

# 只生成配置文件,不创建目录骨架
tcb config init --no-directory

# 指定输出文件路径
tcb config init -o ./config/cloudbaserc.json

生成的配置示例:

{
"$schema": "https://static.cloudbase.net/cli/cloudbaserc.schema.json",
"envId": "env-xxx",
"functionRoot": "./functions",
"functions": [
{
"name": "myPythonFn",
"handler": "main.main",
"runtime": "Python3.11",
"timeout": 3,
"memorySize": 256
}
],
"hosting": [
{
"name": "web",
"root": "web",
"framework": "vite",
"outputDir": "dist",
"buildCommand": "npm run build",
"installCommand": "npm install"
}
]
}

初始化云函数配置

# 交互式创建云函数配置
tcb config init fn

# 指定函数名创建配置
tcb config init fn myFunc

# 指定输出文件
tcb config init fn myFunc -o ./config/cloudbaserc.json

初始化集成配置

# 交互式选择集成类型并创建配置
tcb config init integration

# 指定集成类型和 keyId
tcb config init integration myPayment --type weixinpaydc

# 指定输出文件
tcb config init integration myPayment --type weixinpaydc -o ./config/cloudbaserc.json

初始化行为

综合初始化

  • 智能检测函数和框架后生成完整配置,自动填充 outputDir、buildCommand、installCommand
  • 默认创建 functions/web/ 配套目录骨架(--no-directory 跳过)
  • 已存在的文件不会被覆盖

云函数配置

  • 生成包含基本字段的函数配置模板(name、timeout、runtime、envVariables 等)
  • envVariables 字段会根据集成类型自动填充占位符,提示必填/可选项

集成配置

  • 从云端拉取集成类型的模板字段定义
  • 自动识别必填字段和可选字段
  • 对敏感字段(如密钥、证书)使用占位符 ******,提示用户填写真实值
  • 生成包含 authTypeCodenameenvVariables 的完整配置
使用场景
  • 快速开始:通过智能检测或模板快速创建符合规范的配置文件
  • 配置学习:了解云函数或集成需要配置哪些字段
  • 团队协作:生成标准化的配置模板,团队成员只需填写具体值
生成配置后如何部署?
  • 云函数精细控制(单函数部署、代码更新、查看日志、触发管理)→ 使用 tcb fn deploy [函数名]
  • 整体部署(云函数 + 静态托管 + 网关统一部署)→ 使用 tcb deploytcb deploy --only=<模块>