@celljs/cli 4.3.0 → 4.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -25
- package/agent/guide.md +15 -6
- package/docs/coding-agents.md +79 -0
- package/docs/components.md +157 -0
- package/docs/deployment.md +111 -0
- package/docs/frontend.md +180 -0
- package/docs/getting-started.md +159 -0
- package/docs/packages.md +94 -0
- package/docs/project.md +181 -0
- package/docs/testing.md +78 -0
- package/docs/web.md +168 -0
- package/lib/agent/agent-guide.js +1 -1
- package/lib/agent/agent-guide.js.map +1 -1
- package/lib/build/single-compile.spec.js +2 -2
- package/lib/build/single-compile.spec.js.map +1 -1
- package/lib/check/project-checker.d.ts +20 -0
- package/lib/check/project-checker.d.ts.map +1 -1
- package/lib/check/project-checker.js +87 -23
- package/lib/check/project-checker.js.map +1 -1
- package/lib/check/project-checker.spec.js +19 -3
- package/lib/check/project-checker.spec.js.map +1 -1
- package/lib/cli/build-command.js +2 -2
- package/lib/cli/build-command.js.map +1 -1
- package/lib/cli/cell-cli.spec.js +12 -12
- package/lib/cli/cell-cli.spec.js.map +1 -1
- package/lib/cli/config-command.d.ts +3 -3
- package/lib/cli/config-command.d.ts.map +1 -1
- package/lib/cli/config-command.js +12 -8
- package/lib/cli/config-command.js.map +1 -1
- package/lib/cli/config-command.spec.js +38 -26
- package/lib/cli/config-command.spec.js.map +1 -1
- package/lib/cli/dev-command.js +2 -2
- package/lib/cli/dev-command.js.map +1 -1
- package/lib/cli/dev-command.spec.js +7 -7
- package/lib/cli/dev-command.spec.js.map +1 -1
- package/lib/cli/docs-command.d.ts +3 -2
- package/lib/cli/docs-command.d.ts.map +1 -1
- package/lib/cli/docs-command.js +3 -2
- package/lib/cli/docs-command.js.map +1 -1
- package/lib/cli/info-command.spec.js +28 -26
- package/lib/cli/info-command.spec.js.map +1 -1
- package/lib/cli/mcp-command.spec.js +5 -5
- package/lib/cli/mcp-command.spec.js.map +1 -1
- package/lib/cli/preview-command.js +3 -3
- package/lib/cli/preview-command.js.map +1 -1
- package/lib/config/config-build.spec.js +4 -4
- package/lib/config/config-build.spec.js.map +1 -1
- package/lib/config/config-explainer.d.ts +16 -11
- package/lib/config/config-explainer.d.ts.map +1 -1
- package/lib/config/config-explainer.js +31 -29
- package/lib/config/config-explainer.js.map +1 -1
- package/lib/docs/docs-index.d.ts +4 -1
- package/lib/docs/docs-index.d.ts.map +1 -1
- package/lib/docs/docs-index.js +6 -1
- package/lib/docs/docs-index.js.map +1 -1
- package/lib/docs/docs-index.spec.js +14 -4
- package/lib/docs/docs-index.spec.js.map +1 -1
- package/lib/info/project-inspector.d.ts +6 -5
- package/lib/info/project-inspector.d.ts.map +1 -1
- package/lib/info/project-inspector.js +10 -15
- package/lib/info/project-inspector.js.map +1 -1
- package/lib/mcp/dev-mcp-endpoint.js +3 -3
- package/lib/mcp/dev-mcp-endpoint.js.map +1 -1
- package/lib/mcp/dev-mcp-endpoint.spec.js +2 -2
- package/lib/mcp/dev-mcp-endpoint.spec.js.map +1 -1
- package/lib/mcp/project-mcp-tools.js +3 -3
- package/lib/mcp/project-mcp-tools.js.map +1 -1
- package/lib/modules/module-host.spec.js +1 -1
- package/lib/modules/module-host.spec.js.map +1 -1
- package/lib/preset/builtin-presets.d.ts +1 -1
- package/lib/preset/builtin-presets.d.ts.map +1 -1
- package/lib/preset/builtin-presets.js +1 -1
- package/lib/preset/builtin-presets.js.map +1 -1
- package/lib/preset/cloudflare/cloudflare.spec.js +2 -2
- package/lib/preset/cloudflare/cloudflare.spec.js.map +1 -1
- package/lib/preset/serverless-presets.spec.js +3 -3
- package/lib/preset/serverless-presets.spec.js.map +1 -1
- package/lib/preset/serverless-scripts.js +2 -2
- package/lib/preset/serverless-scripts.js.map +1 -1
- package/lib/preset/vercel/vercel-preset.js +2 -2
- package/lib/preset/vercel/vercel-preset.js.map +1 -1
- package/lib/testing/create-test-app.spec.js +5 -5
- package/lib/testing/create-test-app.spec.js.map +1 -1
- package/package.json +9 -8
- package/src/agent/agent-guide.ts +1 -1
- package/src/check/project-checker.ts +93 -22
- package/src/cli/build-command.ts +2 -2
- package/src/cli/config-command.ts +12 -8
- package/src/cli/dev-command.ts +2 -2
- package/src/cli/docs-command.ts +3 -2
- package/src/cli/preview-command.ts +3 -3
- package/src/config/config-explainer.ts +36 -32
- package/src/docs/docs-index.ts +7 -1
- package/src/info/project-inspector.ts +15 -22
- package/src/mcp/dev-mcp-endpoint.ts +3 -3
- package/src/mcp/project-mcp-tools.ts +3 -3
- package/src/preset/builtin-presets.ts +1 -1
- package/src/preset/serverless-scripts.ts +2 -2
- package/src/preset/vercel/vercel-preset.ts +2 -2
- package/templates/backend/cell.yml +5 -3
- package/templates/backend/src/node/home-controller.ts +1 -1
- package/templates/backend/template.json +1 -1
- package/templates/fullstack/cell.yml +7 -6
- package/templates/fullstack/src/browser/module.ts +1 -1
- package/templates/fullstack/src/browser/{home.view.tsx → pages/index.tsx} +3 -7
- package/templates/fullstack/src/node/greeting-server.ts +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @celljs/cli
|
|
2
2
|
|
|
3
|
-
Cell 4 的命令行工具 `cell`:读取 `cell.config.ts`,按 preset
|
|
3
|
+
Cell 4 的命令行工具 `cell`:读取 `cell.config.ts`,按 preset 构建应用,运行和部署构建产物。入门和各主题的用法见[指南](https://cell.cellbang.com/zh/guide/getting-started)(随本包发布在 `docs/`,`cell docs` 可以检索)。设计见仓库的 [docs/design/architecture.md](https://github.com/cellbang/cell/blob/main/docs/design/architecture.md),从 3.x 迁移见 [docs/migration-4.md](https://github.com/cellbang/cell/blob/main/docs/migration-4.md)。
|
|
4
4
|
|
|
5
5
|
需要 Node.js `^20.19.0 || >=22.12.0`。在项目里安装:
|
|
6
6
|
|
|
@@ -21,7 +21,7 @@ npx cell build
|
|
|
21
21
|
| `cell preview` | 按 `manifest.json` 运行构建产物,不重新构建 |
|
|
22
22
|
| `cell deploy` | 用 preset 构建,再交给 preset 的部署器发布;`--dry-run` 只给出计划 |
|
|
23
23
|
| `cell config get <key>` | 某个配置项合并后的值,`--why` 列出设置它的文件和环境变量 |
|
|
24
|
-
| `cell docs <query>` |
|
|
24
|
+
| `cell docs <query>` | 检索已安装组件和模块随包的文档,以及 `@celljs/cli` 自带的指南与本文档(与项目所用版本一致) |
|
|
25
25
|
| `cell mcp` | stdio MCP 服务器,把 `info`、`check`、`config_get`、`docs_search`、`build`、`deploy_plan` 和模块注册的工具提供给 coding agent |
|
|
26
26
|
| `cell agent sync` | 按已安装的版本重写 AGENTS.md 中由 Cell 管理的部分和 `.agents/skills/cell/SKILL.md` |
|
|
27
27
|
|
|
@@ -49,19 +49,19 @@ cell deploy --preset cloudflare --dry-run # 构建并执行 wrangler deploy --
|
|
|
49
49
|
cell deploy --preset cloudflare # 构建并部署,输出 workers.dev 地址
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
- **运行方式**:Cell
|
|
52
|
+
- **运行方式**:Cell 后端运行在 Workers 的 `node:http` 服务端上(需要 `nodejs_compat`),由 `@celljs/express-adapter` 的 `http` 入口提供,与 `node` preset 相同。前端(`public/`)由 Cloudflare 作为静态资源直接返回,只有不匹配静态文件的请求进入 Worker。产物只支持 ESM,`--format cjs` 报 `CELL_CONFIG_CONFLICT`。
|
|
53
53
|
- **启动时机**:Workers 只允许在模块加载阶段生成代码(`new Function`,部分依赖会用到),只允许在处理请求时做 I/O。所以应用的模块在 Worker 启动时加载,`Application.start()` 和 `onStart` 在第一个请求时执行(`ApplicationHost.beforeStart`)。模块加载失败会让 Worker 启动失败,`wrangler dev` / `wrangler deploy` 直接报出。
|
|
54
54
|
- **Cloudflare 的配置写在项目根目录的 `wrangler.jsonc` 或 `wrangler.json`**(可选,按 Cloudflare 文档的写法):`name`、`compatibility_date`、`vars`、KV / D1 / R2 等绑定、`routes` 都在这里。构建时 Cell 以它为基础生成 `.cell/output/wrangler.json`,并设置:`main`(项目里写了会被忽略并警告)、有前端时的 `assets.directory`、`compatibility_flags` 里的 `nodejs_compat`。没有配置文件时,`name` 取自 package.json 的包名(如 `@acme/shop` → `acme-shop`),`compatibility_date` 默认 `2026-03-17`,这也是允许的最早日期(`node:http` 服务端与 `node:tty` 从这天起默认可用)。`wrangler.toml` 不支持,请改写成 `wrangler.jsonc`。
|
|
55
55
|
- **单页应用**:有前端时默认 `assets.not_found_handling: "single-page-application"`:浏览器导航到不存在的路径时由 Cloudflare 返回 `index.html`(与 Node.js 上 `@celljs/serve-static` 的 `spa` 一致),API 等其他请求仍进入 Worker;在 `wrangler.jsonc` 里写了 `not_found_handling` 就以项目为准。Worker 里没有前端文件,`@celljs/serve-static` 发现没有前端目录时直接放行,未知路径得到正常的 404。
|
|
56
56
|
- **自定义域名**:域名的 DNS 托管在 Cloudflare 时,在 `wrangler.jsonc` 里写 `"routes": [{ "pattern": "app.example.com", "custom_domain": true }]`,部署时 Cloudflare 自动创建 DNS 记录和证书,`deployment.url` 报告这个域名。国内网络无法访问 `*.workers.dev`,面向国内访问时要绑定自定义域名。
|
|
57
|
-
- **应用配置**:`vars` 和 secrets(`wrangler secret put`)进入 `process.env`,所以 `cell.yml` 里的 `${env.X}` 和 `
|
|
57
|
+
- **应用配置**:`vars` 和 secrets(`wrangler secret put`)进入 `process.env`,所以 `cell.yml` 里的 `${env.X}` 和 `CELL_*` 变量照常生效。
|
|
58
58
|
- **Wrangler**:不是 Cell 的依赖。项目装了 `wrangler` 就用项目的,否则由 `npx --yes wrangler@4` 在第一次使用时安装(npm 缓存)。凭据由 Wrangler 管理:设置 `CLOUDFLARE_API_TOKEN`(必要时加 `CLOUDFLARE_ACCOUNT_ID`),或执行一次 `npx wrangler login`。也可以不用 `cell deploy`,直接执行 `npx wrangler deploy --config .cell/output/wrangler.json`。
|
|
59
59
|
- **Workers Builds**(Cloudflare 的 CI,设置 `WORKERS_CI=1`)里自动选用 `cloudflare` preset:构建命令 `npx cell build`,部署命令 `npx cell deploy --skip-build`。
|
|
60
60
|
- `wrangler dev` 会在项目根目录创建 `.wrangler/`,请加入 `.gitignore`。
|
|
61
61
|
|
|
62
62
|
### fc、scf、lambda
|
|
63
63
|
|
|
64
|
-
三个函数平台的做法相同:Cell 服务端原样运行,产物目录整体打成 zip 作为函数代码;部署器直接调用云 API(请求签名自己实现:阿里云 ACS3、腾讯云 TC3、AWS SigV4),不需要云 SDK 或云厂商 CLI。函数名默认取包名(`@acme/shop` → `shop`),可用 `name` 指定;`env` 写入函数的环境变量(另带 `NODE_ENV=production`),`cell.yml` 里的 `${env.X}` 和 `
|
|
64
|
+
三个函数平台的做法相同:Cell 服务端原样运行,产物目录整体打成 zip 作为函数代码;部署器直接调用云 API(请求签名自己实现:阿里云 ACS3、腾讯云 TC3、AWS SigV4),不需要云 SDK 或云厂商 CLI。函数名默认取包名(`@acme/shop` → `shop`),可用 `name` 指定;`env` 写入函数的环境变量(另带 `NODE_ENV=production`),`cell.yml` 里的 `${env.X}` 和 `CELL_*` 照常生效。
|
|
65
65
|
|
|
66
66
|
| preset | 运行方式 | 选项 | 凭据(环境变量) | 部署结果 |
|
|
67
67
|
| --- | --- | --- | --- | --- |
|
|
@@ -93,8 +93,8 @@ cell deploy --preset vercel # vercel deploy --prebuilt,不在 Vercel 上再
|
|
|
93
93
|
| --- | --- |
|
|
94
94
|
| `--preset <name>` | 用哪个 preset 的入口运行后端,默认 `node` |
|
|
95
95
|
| `-m, --mode <mode>` | 额外的 mode,可重复;`development` 总是自动加上 |
|
|
96
|
-
| `-p, --port <port>` | 端口,默认取 `
|
|
97
|
-
| `--host <host>` | 监听地址,默认取 `
|
|
96
|
+
| `-p, --port <port>` | 端口,默认取 `server.port`(考虑 `CELL_SERVER__PORT`),否则 3000;`0` 表示随机端口 |
|
|
97
|
+
| `--host <host>` | 监听地址,默认取 `server.host`,否则 `127.0.0.1` |
|
|
98
98
|
|
|
99
99
|
- 一个端口:Vite 提供前端页面和模块(HMR),`/favicon.ico` 与组件的 `assets` 和构建产物里的位置一致,其余请求交给后端。
|
|
100
100
|
- 后端不打包,在 Vite 的 server 环境里由 module runner 加载:Cell 包和项目源码经过 Cell 插件(生成的 DI 容器与配置写到 `.cell/generated/`),其他依赖由 Node 直接加载。入口通过 `ApplicationHost`(`@celljs/core`)把请求处理函数交给 `cell dev`,不自己监听端口。
|
|
@@ -136,7 +136,7 @@ cell deploy --preset vercel # vercel deploy --prebuilt,不在 Vercel 上再
|
|
|
136
136
|
|
|
137
137
|
| 选项 | 说明 |
|
|
138
138
|
| --- | --- |
|
|
139
|
-
| `--app-config` |
|
|
139
|
+
| `--app-config` | 同时输出每个目标的应用配置(`cell.yml`):后端是全部键,`${...}` 保持原样、运行时才求值;前端只有 public 键,已求值 |
|
|
140
140
|
|
|
141
141
|
只读取 package.json、`cell*.yml`、`cell.config.ts` 和 `manifest.json`,不运行项目代码,不执行 `hooks`。`--json` 结果的主要字段:
|
|
142
142
|
|
|
@@ -147,12 +147,12 @@ cell deploy --preset vercel # vercel deploy --prebuilt,不在 Vercel 上再
|
|
|
147
147
|
| `mode`、`targets`、`targetSources` | 生效的 mode;构建目标及其来由(`cell.config.ts`、`default` 或声明它的组件) |
|
|
148
148
|
| `build` | 生效的 `format`、`outDir`、`external`、`sourcemap`、`single`、`compile` |
|
|
149
149
|
| `components` | 按加载顺序;`dir`、`configFiles` 相对根目录,`targets` / `entries` / `toolchain` / `optionalDependencies` 只在组件声明时出现 |
|
|
150
|
-
| `frontend`、`backend` | `entry`、提供入口的 `entryComponent`、`staticModules` / `modules`(DI 模块,按加载顺序)、`config`(仅 `--app-config`) |
|
|
150
|
+
| `frontend`、`backend` | `entry`、提供入口的 `entryComponent`、`staticModules` / `modules`(DI 模块,按加载顺序)、`config`(仅 `--app-config`);前端有页面时还有 `routes`(`src/browser/pages` 的 `path`、`file`、`layouts`) |
|
|
151
151
|
| `hooks`、`modules`、`moduleOptions` | `cell.config.ts` 里的 hooks、`modules` 条目、toolchain 模块选项的键 |
|
|
152
152
|
| `output` | `dir`、`built`,构建过时还有 manifest 里的 `preset`、`format`、`targets`、`server`、`public` |
|
|
153
|
-
| `frontend.envOverrides`、`backend.envOverrides` | 当前环境里的 `
|
|
153
|
+
| `frontend.envOverrides`、`backend.envOverrides` | 当前环境里的 `CELL_*` / `CELL_*` 变量:`applied`(变量 → 配置路径)和 `problems`(找不到键或类型不符)。没有这类变量时不出现,不输出值 |
|
|
154
154
|
|
|
155
|
-
`warnings` 会提前指出不起作用或会让 `cell build` 失败的设置:没有组件提供 preset 的入口、`-m` 指定的 mode 没有任何 `cell.<mode>.yml
|
|
155
|
+
`warnings` 会提前指出不起作用或会让 `cell build` 失败的设置:没有组件提供 preset 的入口、`-m` 指定的 mode 没有任何 `cell.<mode>.yml`(附拼写建议)、不生效的 `CELL_*` / `CELL_*` 变量。
|
|
156
156
|
|
|
157
157
|
### cell check
|
|
158
158
|
|
|
@@ -163,10 +163,11 @@ cell deploy --preset vercel # vercel deploy --prebuilt,不在 Vercel 上再
|
|
|
163
163
|
|
|
164
164
|
| 检查 | 内容 |
|
|
165
165
|
| --- | --- |
|
|
166
|
-
| `config` | 与 `cell info` 的 warnings 相同:缺少入口(`CELL_ENTRY_MISSING`)、无效 mode(`CELL_MODE_UNKNOWN`)、不生效或被遮住的环境变量(`CELL_ENV_OVERRIDE_IGNORED` / `CELL_ENV_OVERRIDE_SHADOWS
|
|
166
|
+
| `config` | 与 `cell info` 的 warnings 相同:缺少入口(`CELL_ENTRY_MISSING`)、无效 mode(`CELL_MODE_UNKNOWN`)、不生效或被遮住的环境变量(`CELL_ENV_OVERRIDE_IGNORED` / `CELL_ENV_OVERRIDE_SHADOWS`) |
|
|
167
167
|
| `types` | 用项目的 TypeScript 执行 `tsc --noEmit -p tsconfig.json`,每条诊断带文件、行列和 `TSxxxx` 码;没有 tsconfig.json 或 typescript 时跳过 |
|
|
168
168
|
| `decorators` | tsconfig 要开启 `experimentalDecorators` 和 `emitDecoratorMetadata`(`CELL_TSCONFIG_DECORATORS`),与构建时的装饰器语义一致 |
|
|
169
169
|
| `di` | 用 `cell dev` 的方式加载后端 DI 容器(不启动应用),逐个解析绑定:容器抛出的 `CELL_DI_NO_BINDING`、`CELL_DI_AMBIGUOUS`、`CELL_DI_CIRCULAR`、`CELL_DI_MISSING_INJECT`、`CELL_DI_INCOMPLETE_BINDING` 为错误,信息里有谁需要它和解析链,带修复建议;同一个原因只报一次;只能在注入点或请求中创建的绑定记为 info |
|
|
170
|
+
| `security` | 后端 `SecurityCheck` 组件报告的生产环境不安全设置,均为 warning(不让检查失败),与生产启动时 `securityChecks` 处理的是同一批:`CELL_SECURITY_DEFAULT_COOKIE_KEYS`、`CELL_SECURITY_COOKIE_KEYS_MISSING`、`CELL_SECURITY_WEAK_COOKIE_KEYS`(`@celljs/web`)、`CELL_SECURITY_WEAK_JWT_SECRET`(`@celljs/jwt`);读取的是所选 mode 的配置。与 `di` 共用一次后端加载 |
|
|
170
171
|
| 模块注册的检查 | toolchain 模块用 `addCheck` 注册,名字如 `typeorm:entities` |
|
|
171
172
|
|
|
172
173
|
`--json` 结果:`passed`、`summary`(errors / warnings / info 计数)、`issues`(每条带 `check`、`level`、`code`、`message`,可能有 `file`、`line`、`column`、`hint`)、`checks`(每项的耗时、问题数、跳过原因)。不加 `--json` 时按编译器格式输出 `src/a.ts:3:5 error TS2322 …`,info 不显示。
|
|
@@ -192,23 +193,25 @@ cd my-app && npm install && npx cell agent sync && npx cell dev
|
|
|
192
193
|
### cell config get
|
|
193
194
|
|
|
194
195
|
```bash
|
|
195
|
-
cell config get
|
|
196
|
-
#
|
|
196
|
+
cell config get server.port --why
|
|
197
|
+
# server.port (backend) = 8080
|
|
198
|
+
# server-only, owned by @celljs/web
|
|
199
|
+
# Port the server listens on. Override with CELL_SERVER__PORT.
|
|
197
200
|
# node_modules/@celljs/web/cell.yml: 3000
|
|
198
|
-
# cell.yml
|
|
201
|
+
# cell.yml: 8080
|
|
199
202
|
```
|
|
200
203
|
|
|
201
204
|
| 选项 | 说明 |
|
|
202
205
|
| --- | --- |
|
|
203
206
|
| `-m, --mode <mode>` | 额外的 mode,可重复 |
|
|
204
|
-
| `--target <target>` | `backend` 或 `frontend
|
|
205
|
-
| `--why` |
|
|
207
|
+
| `--target <target>` | `backend` 或 `frontend`(只有 public 键),默认有后端时取 `backend` |
|
|
208
|
+
| `--why` | 列出该键的归属组件、是否进入前端、说明,以及按合并顺序设置它的文件和环境变量 |
|
|
206
209
|
|
|
207
|
-
键可写 `a.b.c`、`items[0].name`。`--json` 结果有 `value`、`layers`(`file`、`component`、`
|
|
210
|
+
键可写 `a.b.c`、`items[0].name`。`--json` 结果有 `value`、`owners`(在 `cell.schema.json` 里声明该键的组件)、`public`(前端是否拿到)、`description`、`layers`(`file`、`component`、`value`)、`env`(设置了该键或其上下级的 `CELL_*` 变量)、`expressions`(后端值含 `${...}`,服务端启动时求值)。键不存在时报 `CELL_CONFIG_KEY_NOT_FOUND`,带最接近的键;用 `--target frontend` 查服务端独有的键时说明它是 server-only。
|
|
208
211
|
|
|
209
212
|
### cell docs
|
|
210
213
|
|
|
211
|
-
`cell docs <query> [--limit n]`:在各组件的 `README.md`、`docs
|
|
214
|
+
`cell docs <query> [--limit n]`:在各组件的 `README.md`、`docs/`,模块 `addDocs()` 的目录,以及 `@celljs/cli` 自带的本文档和指南(`docs/`:快速开始、配置、依赖注入、HTTP、RPC、测试、部署、coding agent、组件目录,英文)里按标题分段检索,返回最匹配的段落和 `file:line`(`source` 为 `@celljs/cli` 的来自 CLI)。
|
|
212
215
|
|
|
213
216
|
### cell mcp
|
|
214
217
|
|
|
@@ -247,7 +250,7 @@ args = ["cell", "mcp"]
|
|
|
247
250
|
|
|
248
251
|
| 选项 | 说明 |
|
|
249
252
|
| --- | --- |
|
|
250
|
-
| `-p, --port <port>` | 监听端口,默认取 `
|
|
253
|
+
| `-p, --port <port>` | 监听端口,默认取 `server.port`(3000);等同于设置 `CELL_SERVER__PORT`。`cloudflare` 产物传给 `wrangler dev --port`,默认 8787 |
|
|
251
254
|
| `--out-dir <dir>` | 产物目录,默认 `.cell/output` |
|
|
252
255
|
|
|
253
256
|
产物由哪个 preset 构建,就按它的方式运行:`node`、`cli`、`fc`、`scf`、`lambda` 用 Node.js 运行 `server/index.mjs`(标准输入输出继承当前终端),`cloudflare` 用 `wrangler dev`,`vercel` 用 Node.js 运行函数里的 bundle 并从 `static/` 返回前端。
|
|
@@ -312,7 +315,7 @@ export default defineConfig({
|
|
|
312
315
|
| `build.format` | `esm` 或 `cjs` |
|
|
313
316
|
| `build.external` | 不内联的包,按依赖关系拷到 `server/node_modules` |
|
|
314
317
|
| `build.sourcemap`、`build.outDir` | 同命令行选项 |
|
|
315
|
-
| `build.single`、`build.compile` |
|
|
318
|
+
| `build.single`、`build.compile` | 同 `--single` / `--compile`(`compile` 可写 `true` 或 `bun` / `node` / `deno`) |
|
|
316
319
|
| `vite` | 合并进每个环境的 Vite 配置 |
|
|
317
320
|
| `hooks` | 在工具链事件上执行的 shell 命令,见下文 |
|
|
318
321
|
|
|
@@ -323,11 +326,11 @@ export default defineConfig({
|
|
|
323
326
|
`cell.yml` 里已有的任意键都可以用环境变量覆盖,不用改文件:
|
|
324
327
|
|
|
325
328
|
```bash
|
|
326
|
-
|
|
327
|
-
|
|
329
|
+
CELL_SERVER__PORT=8080 cell preview # 服务端启动时应用:server.port
|
|
330
|
+
CELL_APP__PUBLIC__API_BASE=/api cell build # 构建时应用,进入前端:app.public.apiBase
|
|
328
331
|
```
|
|
329
332
|
|
|
330
|
-
`__`
|
|
333
|
+
前缀后只有一个下划线,层级之间用 `__`(`CELL_SERVER__PORT`)。`CELL_PRESET`、`CELL_MODE`、`CELL_EVENT`、`CELL_OUT_DIR`、`CELL_DEBUG` 是工具链自己的变量,不当作配置;只有一段、又不是顶层键的变量(如别的工具的 `CELL_REGION`)直接忽略;带 `__` 却找不到键的会报告,写成旧的 `CELL__…` / `CELL_BACKEND__…` 时提示正确写法。`__` 分隔层级,一个键只能由一个变量设置,不区分大小写并忽略 `_`、`-`;值按 schema 类型或原值类型转换(JSON 写法的有限数、`true` / `false`、对象和数组用 JSON,原值为 `null` 时是 JSON 就按 JSON),整体是一个 `${...}` 的值保留到求值时再定类型(如 `CELL_SERVER__PORT='${env.PORT}'`),在 `${...}` 求值之前生效。只能覆盖已存在的键,找不到键或类型不符的变量不生效并给出警告:服务端在启动时打印,`cell build` 和 `cell info` 也会检查。完整规则见 [docs/design/config.md](https://github.com/cellbang/cell/blob/main/docs/design/config.md)。
|
|
331
334
|
|
|
332
335
|
## hooks
|
|
333
336
|
|
|
@@ -392,14 +395,18 @@ export default function (cell: CellAPI) {
|
|
|
392
395
|
| `CELL_CONFIG_LOAD`、`CELL_CONFIG_AMBIGUOUS` | `cell.config.ts` 加载或执行失败、存在多个配置文件 |
|
|
393
396
|
| `CELL_CONFIG_INVALID`、`CELL_CONFIG_CONFLICT` | 配置字段错误(带字段路径和拼写建议)、选项互相冲突 |
|
|
394
397
|
| `CELL_CONFIG_PARSE`、`CELL_CONFIG_NOT_OBJECT`、`CELL_CONFIG_EXPRESSION` | `cell.yml` 不是合法 YAML、顶层不是映射、`${...}` 表达式错误 |
|
|
398
|
+
| `CELL_CONFIG_INVALID`(cell.yml) | 没有归属的顶层键、组件没有的选项、类型不符;带文件、行号和拼写建议,自己的配置请放在 `app` 下 |
|
|
399
|
+
| `CELL_CONFIG_KEY_CONFLICT`、`CELL_CONFIG_SCHEMA_INVALID` | 两个组件的 `cell.schema.json` 声明了同一个键、schema 用了不支持的关键字或项目 schema 声明了 `app` 以外的键 |
|
|
395
400
|
| `CELL_COMPONENT_NOT_FOUND`、`CELL_LOCAL_MODULE_NOT_FOUND` | `modules` 里写的组件或本地模块不存在 |
|
|
396
401
|
| `CELL_MODULE_NOT_FOUND`、`CELL_CORE_NOT_FOUND` | 组件声明的 DI 模块无法解析、项目没有依赖 `@celljs/core` |
|
|
402
|
+
| `CELL_ROUTE_CONFLICT`、`CELL_ROUTE_INVALID` | `src/browser/pages` 里两个文件对应同一路径(`about.tsx` 与 `about/index.tsx`)、文件名不是合法的路径段 |
|
|
397
403
|
| `CELL_CORE_EL_MISSING` | 项目的 `@celljs/core` 早于 4.1.0,没有构建期求值配置所需的 `@celljs/core/el` 入口 |
|
|
398
404
|
| `CELL_PRESET_NOT_FOUND`、`CELL_PRESET_DUPLICATE`、`CELL_PRESET_CYCLE` | preset 不存在、重名、`extends` 成环 |
|
|
399
405
|
| `CELL_BUILD_FAILED` | 打包失败,带出错文件和位置 |
|
|
400
406
|
| `CELL_BUILD_CANCELLED`、`CELL_HOOK_FAILED` | hook 或模块取消了构建、hook 命令失败 |
|
|
401
407
|
| `CELL_MODULE_ERROR`、`CELL_MODULE_INVALID` | toolchain 模块的工厂或处理函数出错、默认导出不是工厂函数 |
|
|
402
408
|
| `CELL_CHECK_FAILED`、`CELL_DI_MODULE_FAILED` | 某项检查自身出错、`cell check` 加载后端 DI 模块失败 |
|
|
409
|
+
| `CELL_SECURITY_*`、`CELL_SECURITY_CHECK_FAILED` | 生产环境不安全的设置(`cell check` 的 `security` 检查报告为 warning);生产启动时 `securityChecks: fail` 拒绝启动 |
|
|
403
410
|
| `CELL_CONFIG_UNKNOWN_OPTION` | `cell.config.ts` 有没有模块认领的选项字段 |
|
|
404
411
|
| `CELL_DEV_FAILED`、`CELL_DEV_BACKEND_FAILED`、`CELL_DEV_START_TIMEOUT` | `cell dev` 无法启动(端口等)、后端启动失败、后端入口超时未就绪 |
|
|
405
412
|
| `CELL_DEV_BACKEND_NOT_RUNNING` | dev MCP 工具需要运行中的后端,但后端启动失败或尚未启动 |
|
|
@@ -428,7 +435,7 @@ app.get(UserService); // DI 容器里的
|
|
|
428
435
|
await app.close(); // 停止应用(onStop)
|
|
429
436
|
```
|
|
430
437
|
|
|
431
|
-
`app.get(X)` 中测试从项目源码导入的类或 `Symbol('X')` 与应用里的不是同一个模块实例(应用在 Vite 的 module runner 里),按类名或 symbol 描述匹配;没有或重名时报 `CELL_DI_NO_BINDING` / `CELL_DI_AMBIGUOUS`。要拿到应用用的那一份(重名时、或要用模块里的其他导出),用 `app.import()`:项目内的文件写 `/src
|
|
438
|
+
`app.get(X)` 中测试从项目源码导入的类或 `Symbol('X')` 与应用里的不是同一个模块实例(应用在 Vite 的 module runner 里),按类名或 symbol 描述匹配;没有或重名时报 `CELL_DI_NO_BINDING` / `CELL_DI_AMBIGUOUS`。要拿到应用用的那一份(重名时、或要用模块里的其他导出),用 `app.import()`:项目内的文件写 `/src/...`,包写包名。带 `@Value` / `@Autowired` 的类必须从 `app.import()` 取:测试文件自己导入的这种类按名字匹配后解析会失败(`CELL_DI_NO_BINDING`)。
|
|
432
439
|
|
|
433
440
|
在测试进程里像 `cell dev` 一样从源码加载后端,不构建、不监听端口;请求经内存中的 req/res 对象走完整的 HTTP 处理链,`fetch` 返回标准 `Response`。启动失败抛 `CellError`(如 `CELL_DEV_BACKEND_FAILED`,带出错文件);后端没有 HTTP 入口时 `fetch` 报 `CELL_TEST_APP_NO_HTTP`。
|
|
434
441
|
|
package/agent/guide.md
CHANGED
|
@@ -5,17 +5,25 @@ This project uses Cell {{cellVersion}}: TypeScript with dependency injection, MV
|
|
|
5
5
|
## Layout
|
|
6
6
|
|
|
7
7
|
- `src/node/` is the backend, `src/browser/` the frontend, `src/common/` code for both. `src/<node|browser>/module.ts` is the DI module of its side: it imports every file that has components and ends with `export default autoBind();`. A file it does not import is not loaded.
|
|
8
|
-
- `cell.yml` holds the application configuration,
|
|
8
|
+
- `cell.yml` holds the application configuration: keys of the components at the top level (`server.port`), your own under `app`; only keys marked public and `app.public` reach the browser; a wrong key is a `CELL_CONFIG_INVALID` error. `cell.<mode>.yml` adds to it per mode (`development` in `cell dev`, `production` in `cell build`, `test` in tests, more with `-m <mode>`).
|
|
9
9
|
- `cell.config.ts` (optional) configures the toolchain only: `preset`, `modules`, `build`, `vite`, `hooks`. Application configuration never goes there.
|
|
10
|
+
- A dependency whose package.json has a `cell` field is a component: installing it is enough, its DI modules and `cell.yml` load automatically. Keep every `@celljs/*` package on the same version.
|
|
10
11
|
|
|
11
12
|
## Writing code
|
|
12
13
|
|
|
13
14
|
- Services: `@Component()` on the class, `@Autowired(Type)` on a property to inject. For an interface, export a `Symbol` with the same name and bind the class with `@Component(Symbol)`.
|
|
14
|
-
- Configuration: `@Value('
|
|
15
|
+
- Configuration: `@Value('app.key')` reads your keys under `app` in `cell.yml`, `@Value('server.port')` a key of a component. `${env.NAME ?? 'default'}` is evaluated at startup and converted to the key's schema type. `CELL_SERVER__PORT=8080` overrides an existing key. `npx cell config get <key> --why` tells the owner and whether the frontend sees it.
|
|
15
16
|
- HTTP: `@Controller('path')` with `@Get()`, `@Post()`, `@Put(':id')`, `@Delete(':id')` from `@celljs/mvc/lib/node`; parameters `@Param('id')`, `@Query('q')`, `@Body()`. Returned objects are JSON; add `@Text()` for text. `String`, `Number`, `Boolean` and `Date` parameters are converted. To validate a body, add `@celljs/validation` (with `class-validator`) to the dependencies and type the `@Body()` parameter with a class that has class-validator decorators: invalid bodies get 400.
|
|
16
17
|
- API docs: add `@celljs/openapi` to the dependencies; the app serves the OpenAPI document at `/openapi.json` and Swagger UI at `/api-docs`, built from the routes, parameter and return types and class-validator decorators. Async methods and arrays need `@ApiResponse({ type: [Pet] })`; properties without class-validator decorators need `@ApiProperty()`.
|
|
17
|
-
- gRPC: add `@celljs/grpc`; `Proto.parse(source).service('pkg.Service')` from the `.proto` text (`import source from './x.proto?raw'`), `@GrpcService(Service)` on the class implementing its methods in lowerCamelCase (streams are async iterables), `new GrpcChannel('host:50051').createClient(Service)` to call one. Throw `GrpcError(GrpcStatus.X, 'details')` for statuses. The server listens on `
|
|
18
|
-
-
|
|
18
|
+
- gRPC: add `@celljs/grpc`; `Proto.parse(source).service('pkg.Service')` from the `.proto` text (`import source from './x.proto?raw'`), `@GrpcService(Service)` on the class implementing its methods in lowerCamelCase (streams are async iterables), `new GrpcChannel('host:50051').createClient(Service)` to call one. Throw `GrpcError(GrpcStatus.X, 'details')` for statuses. The server listens on `grpc.server.port` (50051).
|
|
19
|
+
- Errors: throw `HttpError(status, message)` or `NotFoundError(message)` from `@celljs/web/lib/node` for a status; other errors answer 500. `@Catch(ErrorType)` on a controller method handles that type for the controller; an `@Component(ErrorHandler)` with `priority` above 2000 for the whole app.
|
|
20
|
+
- Middleware: `@Component(Middleware)` implementing `handle(ctx, next)` and `priority` (`@celljs/web/lib/node`); `Context.getRequest()` gives the current request anywhere.
|
|
21
|
+
- Startup and shutdown work: `@Component(ApplicationLifecycle)` with `onStart` / `onStop`, not constructors. Logging: `@Autowired(Logger)`.
|
|
22
|
+
- Payments: `@celljs/wechat-pay` (WeChat Pay API v3), `@celljs/alipay` (Alipay gateway) and `@celljs/stripe` (with the official `stripe` package) inject a client (`WechatPayClient`, `AlipayClient`, `StripeClient`) configured under `backend.wechatPay` / `alipay` / `stripe` with `${env.X}` secrets, and serve a notification endpoint (`/wechat-pay/notify`, `/alipay/notify`, `/stripe/webhook`) that verifies signatures and runs `@WechatPayNotificationHandler('TRANSACTION.SUCCESS')`, `@AlipayNotificationHandler('TRADE_SUCCESS')` or `@StripeWebhookHandler('checkout.session.completed')` components. Handlers must be idempotent; throwing makes the provider retry. WeChat amounts are integers in fen, Alipay amounts strings in yuan.
|
|
23
|
+
- Frontend and backend: `@Rpc(Symbol)` on the backend class, `RpcUtil.get<T>(Symbol)` (`@celljs/rpc`) in the frontend. With `rpc.transport: websocket` the backend calls `@RpcClient(Symbol)` components of the frontend through `RpcContext.requireConnection().client<T>(Symbol)` (`@celljs/rpc/lib/node`).
|
|
24
|
+
- Frontend pages are files of `src/browser/pages/` with the component as default export (`users/[id].tsx` is `/users/:id`, `_layout.tsx` wraps a directory); do not import them anywhere. Router hooks and `Link` come from `@celljs/react`.
|
|
25
|
+
- React components get frontend DI components with `useInject<T>(Id)` and configuration with `useValue(key)` from `@celljs/react`; never `ContainerUtil.get` in a component. Tests render a component inside `<ContainerContext.Provider value={container}>`.
|
|
26
|
+
- Packages for common needs: database `@celljs/typeorm` (+ driver; `autoBindEntities`, `OrmContext`, `@Transactional()`), cache `@celljs/cache`, cron `@celljs/schedule`, login `@celljs/security`, tokens `@celljs/jwt`, outgoing HTTP `@celljs/http`, LLMs `@celljs/ai-core` + `@celljs/ai-pi`, payments (above). Read a package's docs with `npx cell docs <package or topic>` before using it.
|
|
19
27
|
|
|
20
28
|
## Verify
|
|
21
29
|
|
|
@@ -25,6 +33,7 @@ This project uses Cell {{cellVersion}}: TypeScript with dependency injection, MV
|
|
|
25
33
|
```ts
|
|
26
34
|
const app = await createTestApp({ root: join(__dirname, '..') });
|
|
27
35
|
const res = await app.fetch('/todos', { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}' });
|
|
36
|
+
const { TodoService } = await app.import('/src/node/todo-service.ts'); // classes from app.import(), not from a test import
|
|
28
37
|
const service = app.get(TodoService);
|
|
29
38
|
await app.close();
|
|
30
39
|
```
|
|
@@ -36,6 +45,6 @@ This project uses Cell {{cellVersion}}: TypeScript with dependency injection, MV
|
|
|
36
45
|
|
|
37
46
|
- `npx cell info --json`: components, DI modules, entries, preset, hooks, output.
|
|
38
47
|
- `npx cell config get <key> --why`: a merged configuration value and the files and variables that set it.
|
|
39
|
-
- `npx cell docs <words>`: the docs of the installed Cell packages.
|
|
48
|
+
- `npx cell docs <words>`: the docs of the installed Cell packages and the Cell guides (configuration, DI, HTTP, RPC, testing, deployment), in the installed versions.
|
|
40
49
|
- `npx cell mcp` serves these as MCP tools. While `cell dev` runs, `http://127.0.0.1:<port>/_cell/mcp` has `get_errors`, `list_routes`, `list_bindings`, `get_config`, `recent_requests` and `logs` of the running application.
|
|
41
|
-
- Errors read `[CELL_CODE] message`, then `fix:` and a docs link: do what the fix says.
|
|
50
|
+
- Errors read `[CELL_CODE] message`, then `fix:` and a docs link: do what the fix says.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Working with coding agents
|
|
2
|
+
|
|
3
|
+
Cell is designed so that a coding agent (Claude Code, Codex, Cursor, CodeBuddy, pi, ...) can use it correctly on the first try: one way to write each thing, structured errors with fixes, machine-readable commands and documentation that matches the installed version. This page is for the people who set agents up; the guide the agents themselves read is [AGENTS.md](https://cell.cellbang.com/agent-guide).
|
|
4
|
+
|
|
5
|
+
## What a project gets
|
|
6
|
+
|
|
7
|
+
`cell init` writes these files, and `npx cell agent sync` rewrites the Cell part of them after you upgrade Cell or add a component:
|
|
8
|
+
|
|
9
|
+
| File | For |
|
|
10
|
+
| --- | --- |
|
|
11
|
+
| `AGENTS.md` | The Cell guide of the installed `@celljs/cli` version, followed by the guide snippets of installed components. Cell only rewrites the part between `<!-- cell:begin -->` and `<!-- cell:end -->`; write project rules outside it |
|
|
12
|
+
| `CLAUDE.md` | `@AGENTS.md`, for Claude Code |
|
|
13
|
+
| `.agents/skills/cell/SKILL.md` | The same guide as an agent skill |
|
|
14
|
+
| `.mcp.json` | Starts `npx cell mcp` |
|
|
15
|
+
|
|
16
|
+
Add `npx cell agent sync` to your upgrade routine, or run it in a `postinstall` script.
|
|
17
|
+
|
|
18
|
+
## MCP
|
|
19
|
+
|
|
20
|
+
`npx cell mcp` is a stdio MCP server for the project. Its tools return the same JSON as the commands:
|
|
21
|
+
|
|
22
|
+
| Tool | Command | Side effects |
|
|
23
|
+
| --- | --- | --- |
|
|
24
|
+
| `info` | `cell info` | none |
|
|
25
|
+
| `check` | `cell check` | none |
|
|
26
|
+
| `config_get` | `cell config get` | none |
|
|
27
|
+
| `docs_search` | `cell docs` | none |
|
|
28
|
+
| `build` | `cell build` | writes the output directory |
|
|
29
|
+
| `deploy_plan` | `cell deploy --dry-run` | writes the output directory; may reach the network |
|
|
30
|
+
|
|
31
|
+
There is deliberately no deploy tool: deploying stays with a person running `cell deploy`.
|
|
32
|
+
|
|
33
|
+
```jsonc
|
|
34
|
+
// .mcp.json (Claude Code, Cursor, CodeBuddy) or .cursor/mcp.json
|
|
35
|
+
{ "mcpServers": { "cell": { "command": "npx", "args": ["cell", "mcp"] } } }
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```toml
|
|
39
|
+
# Codex: ~/.codex/config.toml
|
|
40
|
+
[mcp_servers.cell]
|
|
41
|
+
command = "npx"
|
|
42
|
+
args = ["cell", "mcp"]
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
While `cell dev` runs, `http://127.0.0.1:<port>/_cell/mcp` is a second MCP server (HTTP) for the running application: `get_errors`, `list_routes`, `list_bindings`, `get_config`, `recent_requests` and `logs`. It only accepts requests from the local machine.
|
|
46
|
+
|
|
47
|
+
## Commands agents use
|
|
48
|
+
|
|
49
|
+
| Command | Gives |
|
|
50
|
+
| --- | --- |
|
|
51
|
+
| `npx cell check --json` | Every type, configuration, decorator and DI problem with `code`, `file`, `line` and `hint` |
|
|
52
|
+
| `npx cell info --json` | Preset, components, DI modules, entries, hooks, output state |
|
|
53
|
+
| `npx cell config get <key> --why` | A merged configuration value and where each layer came from |
|
|
54
|
+
| `npx cell docs <words>` | Sections of the docs shipped with the installed packages (their READMEs, these guides and the CLI reference) |
|
|
55
|
+
| `npx cell build --json`, `npx cell deploy --dry-run --json` | One-line results instead of progress output |
|
|
56
|
+
|
|
57
|
+
Every command accepts `--json` (one document with `schemaVersion` on stdout, everything else on stderr), never prompts when not on a terminal, and side-effecting commands have `--dry-run`. Exit codes: 0 success, 1 failure, 2 usage error.
|
|
58
|
+
|
|
59
|
+
## Errors
|
|
60
|
+
|
|
61
|
+
Every toolchain and runtime error has a stable code, a message, a one-sentence fix and a docs link:
|
|
62
|
+
|
|
63
|
+
```text
|
|
64
|
+
[CELL_DI_NO_BINDING] No binding for Symbol(Mailer), needed by Signup.mailer (TodoController → Signup → Symbol(Mailer)).
|
|
65
|
+
fix: Bind the identifier in a DI module (@Component(<id>) in a file the module imports), or mark the injection @Optional().
|
|
66
|
+
docs: https://cell.cellbang.com/errors/cell-di-no-binding
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
The [error code list](https://cell.cellbang.com/errors/) has a page per code. Codes and `--json` fields stay stable within 4.x.
|
|
70
|
+
|
|
71
|
+
## Documentation for models
|
|
72
|
+
|
|
73
|
+
The docs site publishes [`llms.txt`](https://cell.cellbang.com/llms.txt) (an index of every page) and [`llms-full.txt`](https://cell.cellbang.com/llms-full.txt) (the guides, the CLI reference and the package references in one file) for agents that read the web. Inside a project, `cell docs` is better: it searches the version you have installed.
|
|
74
|
+
|
|
75
|
+
## Habits that work
|
|
76
|
+
|
|
77
|
+
- Ask the agent to run `npx cell check` and the tests after every change; both are fast and their output is made for it.
|
|
78
|
+
- Keep project rules in `AGENTS.md` outside the Cell markers.
|
|
79
|
+
- Let the agent use `cell deploy --dry-run` freely; keep `cell deploy` itself, and the credentials it needs, for people.
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# Components and dependency injection
|
|
2
|
+
|
|
3
|
+
Every part of a Cell application is a **component**: a class the DI container creates, wires and keeps. Controllers, services, middleware and RPC services are all components. Cell's container is its own (`@celljs/core`), with no third-party IoC library.
|
|
4
|
+
|
|
5
|
+
## Declaring and injecting
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Autowired, Component, Value } from '@celljs/core';
|
|
9
|
+
|
|
10
|
+
@Component()
|
|
11
|
+
export class TodoRepository {
|
|
12
|
+
protected readonly todos: string[] = [];
|
|
13
|
+
add(title: string) { this.todos.push(title); }
|
|
14
|
+
all() { return [...this.todos]; }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@Component()
|
|
18
|
+
export class TodoService {
|
|
19
|
+
@Autowired() // by the declared type
|
|
20
|
+
protected readonly repository: TodoRepository;
|
|
21
|
+
|
|
22
|
+
@Value('app.todos.max') // configuration, see cell.yml
|
|
23
|
+
protected readonly max: number;
|
|
24
|
+
|
|
25
|
+
add(title: string) {
|
|
26
|
+
if (this.repository.all().length >= this.max) {
|
|
27
|
+
throw new Error('Too many todos');
|
|
28
|
+
}
|
|
29
|
+
this.repository.add(title);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
- Components are **singletons** by default: one instance per application. `@Component({ scope: Scope.Transient })` makes a new one for every injection.
|
|
35
|
+
- Inject into properties (above) or constructor parameters (`constructor(@Autowired(TodoRepository) protected readonly repository: TodoRepository)`). Constructor parameters without a decorator are injected by their declared class.
|
|
36
|
+
- The file must be imported by a [DI module](./project.md#di-modules), or the class is never registered.
|
|
37
|
+
- `@Value('key')` reads the merged `cell.yml`; `@Value()` without a key uses the property name.
|
|
38
|
+
|
|
39
|
+
## Interfaces: a Symbol with the same name
|
|
40
|
+
|
|
41
|
+
TypeScript interfaces do not exist at runtime, so the identifier of an interface is a `Symbol` exported under the same name. This is the convention throughout Cell:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
// src/common/mailer.ts
|
|
45
|
+
export const Mailer = Symbol('Mailer');
|
|
46
|
+
|
|
47
|
+
export interface Mailer {
|
|
48
|
+
send(to: string, text: string): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
// src/node/mailer-impl.ts
|
|
54
|
+
import { Component } from '@celljs/core';
|
|
55
|
+
import { Mailer } from '../common/mailer';
|
|
56
|
+
|
|
57
|
+
@Component(Mailer)
|
|
58
|
+
export class MailerImpl implements Mailer {
|
|
59
|
+
async send(to: string, text: string): Promise<void> { /* ... */ }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// elsewhere
|
|
63
|
+
@Autowired(Mailer)
|
|
64
|
+
protected readonly mailer: Mailer;
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`@Component(Mailer)` registers the class under the symbol **and** under the class itself; both give the same instance. `@Component({ id: [A, B] })` registers more identifiers.
|
|
68
|
+
|
|
69
|
+
## Several implementations
|
|
70
|
+
|
|
71
|
+
| Need | Write |
|
|
72
|
+
| --- | --- |
|
|
73
|
+
| All implementations | `@Autowired(Handler) protected readonly handlers: Handler[]` (an array type) |
|
|
74
|
+
| None is fine | add `@Optional()`: `undefined`, or `[]` for an array |
|
|
75
|
+
| Pick one by name | `@Component({ id: Store, name: 'redis' })` and `@Autowired(Store) @Named('redis')` |
|
|
76
|
+
| Pick one by tag | `@Component({ id: Store, tag: { tag: 'region', value: 'eu' } })` and `@Autowired(Store) @Tagged('region', 'eu')` |
|
|
77
|
+
| A default among several | `@Component({ id: Store, default: true })`: plain `@Autowired(Store)` gets it |
|
|
78
|
+
| Replace a framework component | `@Component({ id: ErrorHandler, rebind: true })`: removes the earlier bindings of that id |
|
|
79
|
+
| Many, sorted, created lazily | `@AutowiredProvider(Step) protected readonly steps: Provider<Step>`, then `this.steps.sortSync()` (highest `priority` first) |
|
|
80
|
+
|
|
81
|
+
Extension points of the framework (`Middleware`, `ErrorHandler`, `ApplicationLifecycle`, `HealthIndicator`, ...) are such multi-implementation symbols: you add an implementation by declaring `@Component(Middleware)` on your class, and `rebind: true` replaces the built-in one.
|
|
82
|
+
|
|
83
|
+
## Lifecycle
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { Application, ApplicationLifecycle, Component, PostConstruct } from '@celljs/core';
|
|
87
|
+
|
|
88
|
+
@Component(ApplicationLifecycle)
|
|
89
|
+
export class Warmup implements ApplicationLifecycle<Application> {
|
|
90
|
+
initialize(): void { /* synchronous, before any onStart */ }
|
|
91
|
+
async onStart(app: Application): Promise<void> { /* awaited one after another */ }
|
|
92
|
+
onStop(app: Application): void { /* on shutdown and before every reload in cell dev */ }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@Component()
|
|
96
|
+
export class Cache {
|
|
97
|
+
@PostConstruct()
|
|
98
|
+
protected init(): void { /* after the properties are injected, once per instance */ }
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
An error thrown in `initialize`, `onStart` or `onStop` is logged and does not stop the other lifecycles. For work that must not run at module load (opening connections, timers), use `onStart`, not the constructor: `cell check` creates the components without starting the application.
|
|
103
|
+
|
|
104
|
+
## Logging
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import { Autowired, Component, Logger } from '@celljs/core';
|
|
108
|
+
|
|
109
|
+
@Component()
|
|
110
|
+
export class TodoService {
|
|
111
|
+
@Autowired(Logger)
|
|
112
|
+
protected readonly logger: Logger;
|
|
113
|
+
|
|
114
|
+
add(title: string) {
|
|
115
|
+
this.logger.info(`added ${title}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
The level comes from `logger.level` (`verbose`, `debug`, `info` (default), `warn`, `error`). `@celljs/logger` replaces the console logger with winston.
|
|
121
|
+
|
|
122
|
+
## Aspects (AOP)
|
|
123
|
+
|
|
124
|
+
An aspect runs code around the methods of components created with `proxy: true` (controllers and RPC services are proxied by default):
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
import { Aspect, MethodBeforeAdvice } from '@celljs/core';
|
|
128
|
+
|
|
129
|
+
@Aspect({ id: MethodBeforeAdvice, pointcut: 'Audited' })
|
|
130
|
+
export class AuditAdvice implements MethodBeforeAdvice {
|
|
131
|
+
async before(method: string | number | symbol, args: any[], target: any): Promise<void> {
|
|
132
|
+
console.log('call', String(method), args);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
@Component({ proxy: true, sysTags: ['Audited'] })
|
|
137
|
+
export class AccountService { /* ... */ }
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
`AfterReturningAdvice` and `AfterThrowsAdvice` run after a method returns or throws. Without `pointcut` an advice applies to every proxied component. A proxied method always returns a Promise, also when it is synchronous. `aop.enabled: false` turns proxies off.
|
|
141
|
+
|
|
142
|
+
## Outside the container
|
|
143
|
+
|
|
144
|
+
Code that is not a component (a script, a plain function) gets components with `ContainerUtil.get(TodoService)` or `ContainerUtil.getAll(Handler)` once the application has started.
|
|
145
|
+
|
|
146
|
+
## When wiring fails
|
|
147
|
+
|
|
148
|
+
The container reports wiring problems as structured errors, and `npx cell check` finds them without starting the application:
|
|
149
|
+
|
|
150
|
+
| Code | Meaning | Usual fix |
|
|
151
|
+
| --- | --- | --- |
|
|
152
|
+
| `CELL_DI_NO_BINDING` | Nothing is registered for an identifier | Import the file from a DI module; add `@Component(Id)`; install the package |
|
|
153
|
+
| `CELL_DI_AMBIGUOUS` | Several bindings match a single injection | Inject an array, or use `@Named` / `@Tagged` / `default: true`, or `rebind: true` |
|
|
154
|
+
| `CELL_DI_CIRCULAR` | Components need each other while being created | Get one side lazily (`@AutowiredProvider`, or `@Autowired({ detached: true })` on a property), or move the shared part into another component |
|
|
155
|
+
| `CELL_DI_MISSING_INJECT` | A constructor parameter has no identifier | Add `@Autowired(Id)` |
|
|
156
|
+
|
|
157
|
+
The message names the identifier, who needs it and the resolution chain. The [core reference](https://cell.cellbang.com/packages/core) covers every decorator in detail.
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Building and deploying
|
|
2
|
+
|
|
3
|
+
Your code does not change between platforms: a **preset**, chosen at build time, decides the shape of the output and how it is deployed.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npx cell build # preset node: .cell/output
|
|
7
|
+
npx cell build --preset cloudflare
|
|
8
|
+
npx cell deploy --preset fc --dry-run # the plan; nothing is uploaded
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The preset comes from `--preset`, else `preset` in `cell.config.ts`, else the `CELL_PRESET` variable, else the CI platform (`cloudflare` in Workers Builds, `vercel` on Vercel), else `node`. `npx cell info` shows which one applies and why.
|
|
12
|
+
|
|
13
|
+
## Presets
|
|
14
|
+
|
|
15
|
+
| Preset | Output | Run locally | Deploy |
|
|
16
|
+
| --- | --- | --- | --- |
|
|
17
|
+
| `node` (default) | Node.js HTTP server, `server/index.mjs` | `cell preview` | Copy `.cell/output` anywhere Node.js 18+, Bun or Deno runs |
|
|
18
|
+
| `cli` | A command-line program on Cell DI, no HTTP server | `cell preview` | Distribute the file or an executable |
|
|
19
|
+
| `cloudflare` | Worker + static assets + `wrangler.json` | `cell preview` (workerd through Wrangler) | `cell deploy --preset cloudflare` |
|
|
20
|
+
| `fc` | Alibaba Cloud Function Compute 3.0, custom runtime | `cell preview` | `cell deploy --preset fc` |
|
|
21
|
+
| `scf` | Tencent Cloud SCF web function | `cell preview` | `cell deploy --preset scf` |
|
|
22
|
+
| `lambda` | AWS Lambda with a function URL | `cell preview` | `cell deploy --preset lambda` |
|
|
23
|
+
| `vercel` | Vercel Build Output API (`.vercel/output`) | `cell preview` | `cell deploy --preset vercel` |
|
|
24
|
+
|
|
25
|
+
## The output
|
|
26
|
+
|
|
27
|
+
```text
|
|
28
|
+
.cell/output/
|
|
29
|
+
├── server/index.mjs # the backend: one ES module with every dependency inlined
|
|
30
|
+
├── public/ # the frontend build, when there is one
|
|
31
|
+
└── manifest.json # preset, format, targets: what cell preview and cell deploy read
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The output does not need the project's `node_modules`. Packages that cannot be bundled (native modules, `build.external` in `cell.config.ts`) are copied into `server/node_modules`.
|
|
35
|
+
|
|
36
|
+
| Option | Result |
|
|
37
|
+
| --- | --- |
|
|
38
|
+
| `--single` | The frontend is embedded in `server/index.mjs`: one file runs the whole application |
|
|
39
|
+
| `--compile [bun\|node\|deno]` | An executable `.cell/output/app` that needs no Node.js on the host |
|
|
40
|
+
| `-m <mode>` | Also reads `cell.<mode>.yml` (`production` is always active in builds) |
|
|
41
|
+
| `--sourcemap`, `--format cjs`, `--out-dir <dir>` | Source maps, CommonJS output, another directory |
|
|
42
|
+
|
|
43
|
+
## Configuration in production
|
|
44
|
+
|
|
45
|
+
- **Backend configuration is evaluated when the server starts.** The same build runs everywhere; give each environment its values through environment variables, either referenced in `cell.yml` (`"${env.DATABASE_URL}"`) or as overrides (`CELL_SERVER__PORT=8080`). See [configuration](./project.md#application-configuration-cell-yml).
|
|
46
|
+
- **Never mark a secret public.** Configuration is server-only unless a schema marks it public or it is under `app.public`; frontend values are evaluated at build time and shipped to browsers.
|
|
47
|
+
- The server listens on `127.0.0.1:3000` by default. In a container set `CELL_SERVER__HOST=0.0.0.0`; when the platform gives the port in `PORT`, set `CELL_SERVER__PORT='${env.PORT}'`.
|
|
48
|
+
- Set your own `web.cookies.keys` (signing key of cookies and sessions), e.g. `CELL_WEB__COOKIES__KEYS='["..."]'`.
|
|
49
|
+
- **Security checks.** `cell check` lists settings that are unsafe in production (`CELL_SECURITY_*` warnings: the development cookie key, a short `jwt.secret`...). A production start (`NODE_ENV=production`, which `cell build` writes into the server bundle) logs the same warnings; set `securityChecks: fail` (or `CELL_SECURITY_CHECKS=fail`) to refuse to start instead, `off` to skip them.
|
|
50
|
+
- **Error messages.** In production a server error answers with its status text only (`Internal Server Error`); its message and stack go to the log. 4xx `HttpError` messages are sent. `web.errors.exposeMessages` overrides this.
|
|
51
|
+
|
|
52
|
+
### A container image
|
|
53
|
+
|
|
54
|
+
```dockerfile
|
|
55
|
+
FROM node:22-alpine AS build
|
|
56
|
+
WORKDIR /app
|
|
57
|
+
COPY package*.json ./
|
|
58
|
+
RUN npm install
|
|
59
|
+
COPY . .
|
|
60
|
+
RUN npx cell build
|
|
61
|
+
|
|
62
|
+
FROM node:22-alpine
|
|
63
|
+
ENV NODE_ENV=production
|
|
64
|
+
ENV CELL_SERVER__HOST=0.0.0.0
|
|
65
|
+
WORKDIR /app
|
|
66
|
+
COPY --from=build --chown=node:node /app/.cell/output ./
|
|
67
|
+
USER node
|
|
68
|
+
EXPOSE 3000
|
|
69
|
+
CMD ["node", "server/index.mjs"]
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## `cell deploy`
|
|
73
|
+
|
|
74
|
+
`cell deploy` builds with the preset, makes a plan (the resources it will create or update), then applies it. `--dry-run` stops after the plan and works without credentials; `--skip-build` deploys the existing output.
|
|
75
|
+
|
|
76
|
+
| Preset | Credentials (environment) | Platform options in `cell.config.ts` |
|
|
77
|
+
| --- | --- | --- |
|
|
78
|
+
| `cloudflare` | `CLOUDFLARE_API_TOKEN` (and `CLOUDFLARE_ACCOUNT_ID`), or `npx wrangler login` | `wrangler.jsonc` in the project root: name, vars, bindings, routes |
|
|
79
|
+
| `fc` | `ALIBABA_CLOUD_ACCESS_KEY_ID`, `ALIBABA_CLOUD_ACCESS_KEY_SECRET` | `fc: { name, region, memory, cpu, timeout, concurrency, env }` |
|
|
80
|
+
| `scf` | `TENCENTCLOUD_SECRET_ID`, `TENCENTCLOUD_SECRET_KEY` | `scf: { name, region, namespace, memory, timeout, env }` |
|
|
81
|
+
| `lambda` | `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, or `AWS_PROFILE` | `lambda: { name, region, architecture, memory, timeout, role, env }` |
|
|
82
|
+
| `vercel` | `VERCEL_TOKEN`, or `npx vercel login` | `vercel: { project, scope, environment, memory, maxDuration, regions }` |
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
// cell.config.ts
|
|
86
|
+
import { defineConfig } from '@celljs/cli';
|
|
87
|
+
|
|
88
|
+
export default defineConfig({
|
|
89
|
+
preset: 'fc',
|
|
90
|
+
fc: { region: 'cn-shanghai', memory: 512, env: { DATABASE_URL: process.env.DATABASE_URL ?? '' } },
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The function platforms (`fc`, `scf`, `lambda`) call the cloud APIs directly: no vendor SDK or CLI is installed. Wrangler and the Vercel CLI are used from the project when installed, otherwise through `npx`. Every failure has a code: `CELL_DEPLOY_CREDENTIALS_MISSING`, `CELL_DEPLOY_UNAUTHORIZED`, `CELL_DEPLOY_PACKAGE_TOO_LARGE`, ...
|
|
95
|
+
|
|
96
|
+
Platform details (custom domains, runtimes, package size limits, what each deployer creates) are in the [CLI reference](../README.md#preset).
|
|
97
|
+
|
|
98
|
+
## Hooks
|
|
99
|
+
|
|
100
|
+
Shell commands on toolchain events, for steps such as code generation:
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
export default defineConfig({
|
|
104
|
+
hooks: {
|
|
105
|
+
'build:before': 'prisma generate', // a failure cancels the build
|
|
106
|
+
'output:done': 'cp extra/* "$CELL_OUT_DIR/server/"',
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Extensions that need code (Vite plugins, extra output files, checks, MCP tools) are toolchain modules; see the [CLI reference](../README.md).
|