saasaloy-env
Runbook for the env capability in packages/env — the one tracked key list, the pnpm env:setup distribution command, the pnpm deploy:production deploy with env-deploy, and the typed createEnv gate each service reads its values through. Use when deploying to Cloudflare, when a deploy stops naming a blank production key, adding an environment key, when a build or a request fails naming an unset key, when choosing which services read a key, when a value must stay blank, when a key is checkout-owned, when picking a value source, or when a leftover .dev.vars is hiding .env from wrangler.
- License 许可
- MIT
- 所属仓库
- mimukit/saasaloy
- 最近更新
- 2026-11-13
- Stars
- ★ 4
- Forks
- 0
01 前言 Front Matter
- name
- saasaloy-env
- description
- Runbook for the env capability in packages/env — the one tracked key list, the pnpm env:setup distribution command, the pnpm deploy:production deploy with env-deploy, and the typed createEnv gate each service reads its values through. Use when deploying to Cloudflare, when a deploy stops naming a blank production key, adding an environment key, when a build or a request fails naming an unset key, when choosing which services read a key, when a value must stay blank, when a key is checkout-owned, when picking a value source, or when a leftover .dev.vars is hiding .env from wrangler.
03 SKILL.md 正文
env — one key list, one command, one typed gate
packages/env (@repo/env) owns both halves of this project's environment story.
The node half is a tracked key list (packages/env/.env.example), a gitignored value file (packages/env/.env), and pnpm env:setup, which writes every service's own .env from the two. The runtime half is createEnv, which validates the values a service actually got and hands back typed ones.
A service is a workspace that reads a .env: api, web, admin. The name is the workspace directory. packages/env/src/services.ts holds the table, and saasaloy add api appends a row to it. One row has no workspace and no file: deploy holds CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID, which a deploy loads in memory.
Adding a key, in two steps
1. Add its line to packages/env/.env.example, under the # @services section naming every service that reads it. Write a comment above it saying where the value comes from and what unset looks like. Put the local development default on the line.
# @services api
# Sender address every outbound message uses. The domain must be one the selected
# provider may send from. Unset, the first send throws naming this key.
EMAIL_FROM=dev@localhost
A key with no # @services line above it fails pnpm env:setup. That is deliberate: a key that reaches no service is a key the Worker throws on, and failing here is the cheaper place to find out.
2. Declare it in the preset of the package that reads it, so createEnv validates it and the type reaches the calling code.
// packages/email/src/env-preset.ts
export const emailPreset = definePreset({
module: "@repo/email",
extends: [],
server: {
EMAIL_FROM: z.string().min(1).describe("Sender address every message uses."),
},
});
The .describe() text is what the aggregate error prints, so write it for the person reading a failed deploy.
Reading a key
On the server, pass the Worker's env:
import { apiEnv } from "@api/env";
app.get("/x", (c) => {
const { EMAIL_FROM } = apiEnv(c.env);
});
In a frontend, call it with nothing — the values are inlined at build time:
import { webEnv } from "@web/env";
const base = webEnv().PUBLIC_API_URL;
createEnv validates on the first call per env object and memoizes the result in a WeakMap. A Worker cannot see its env at module scope, so the first request is the earliest honest point to check. One throw then names every failing key at once, with the module that declared it and that module's own wording, so a deploy with three unset keys takes one round trip to fix.
Server, client, shared
server— read only whereisServeris true. A Worker, a build script.client— inlined into a public bundle. Must start withPUBLIC_, and the type system says so: a mis-prefixed key does not compile.shared— read on both sides.
apps/web/src/env.ts and apps/admin/src/env.ts set isServer: false, which arms a Proxy: reading a server key from client code throws by name. typeof window === "undefined" is not used, because it is true in a Worker and in a browser bundle before hydration.
The runtimeEnvStrict map in those files is hand-written, one literal import.meta.env.PUBLIC_X per key. Vite inlines a literal member expression and nothing else, so a spread, a loop or a computed key compiles to undefined in the built bundle and fails only in production. Add the line by hand when you add a client key.
Blank on purpose
pnpm env:setup fails on a key the list carries with no value. When a key is meant to stay empty, say so in its comment block:
# Blank on purpose: unset means no proxy origin, and a link is root-relative.
STORAGE_PROXY_URL=
Checkout-owned keys
CHECKOUT_OWNED_KEYS in packages/env/src/services.ts lists keys this checkout sets for itself. A key named there keeps whatever value is already on disk, whoever wrote it, so a per-branch DATABASE_URL survives every pnpm env:setup. packages/env never learns what a database is — only that some key is the checkout's to set.
Value sources
pnpm env:setup reads its values from a source, selected by ENV_SOURCE:
local(the default, always installed) reads the gitignoredpackages/env/.env.saasaloy envprompts for each unset key and writes that file, so a project develops with no vendor account and no network. An agent with no terminal runssaasaloy env --from-file <path>instead, which writes theKEY=VALUElines of a dotenv file with no prompt and refuses a key no installed module declares. Prefer it to--set KEY=VALUE, which leaves the value in shell history.infisicalarrives withsaasaloy add env-infisical.
A source that does not answer at all — no CLI, no network — is treated as offline: env:setup keeps every service file that is already complete and warns, and fails writing nothing when one is incomplete. A refusal the source answered with, such as no access, always fails.
Production and --env prod
Each service row declares dev or prod. pnpm env:setup --env prod refuses to act without --only <service>, and refuses outright to write production values into a workspace under apps/. Two independent guards on the one command that can put a live credential on disk.
saasaloy env prints the wrangler secret put lines for a deploy. It never runs them, and pnpm deploy:production makes them unnecessary.
Deploying: pnpm deploy:production and env-deploy
pnpm deploy:production (packages/tooling/src/deploy-production.ts) deploys every Worker in the project with wrangler. The saasaloy-deploy skill covers its gates, its flags, the go-ahead an agent needs, the hotfix branch and the rollback. This section covers the environment half:
- Before any build, it runs
node packages/env/src/deploy.ts --check. That loads every service'sprodvalues from the selected source and exits non-zero, naming each blank key, when one is missing. It also stops whenCLOUDFLARE_API_TOKENis blank and wrangler holds nowrangler loginsession.pnpm deploy:production --dry-runruns the same check and reports its result. - It then runs each deploy target's
deployscript, one at a time, in the orderpackages/tooling/src/targets.tsgives. That script isenv-deploy <service>. No Worker deploy runs a migration. A production migration runs only throughpnpm deploy:production --migrate.
env-deploy <service> loads that service's prod values and the deploy credentials, runs pnpm run build with them in the environment, and runs wrangler deploy --secrets-file <file>. Code and secrets land in one Worker version, so a Worker that throws at module scope on a missing secret deploys cleanly the first time. The secrets file is JSON, mode 0600, under the OS temp directory, and deleted when the deploy ends, passed or failed. It holds the service's values minus every CLOUDFLARE_* key, minus each key the Worker's wrangler.jsonc already sets in vars, minus empty values. An assets-only Worker, one with no main, gets no secrets file.
Where the production values come from. The local source reads the gitignored packages/env/.env.prod, never packages/env/.env, so a deploy cannot ship a localhost URL by accident. Copy the keys from packages/env/.env.example into it and set each one. env-infisical reads its prod environment. A variable the shell already exports wins over both, which is how CI passes values. The base turbo.json keeps a deploy task with passThroughEnv for a project that runs turbo run deploy by hand, because turbo's strict env mode would drop the exported variables otherwise. pnpm deploy:production does not use turbo for the deploy.
Why the dev .env stays out of the build. Vite gives the environment priority over a .env file, but the Cloudflare adapters prerender inside workerd, and wrangler hands workerd the .env beside the workspace as its vars. So env-deploy builds with CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV=false, and it sets a key the source leaves out to an empty string rather than leaving it unset. apps/web/.env keeps its dev values, and nothing writes a production value under apps/.
Three more forms:
pnpm deploy:production --only <service>deploys one Worker after the same gates, and runs its smoke checks. It writes no deploy tag.pnpm --filter @repo/web run deploy -- --dry-runbuilds one Worker with the production values and uploads nothing. Pass wrangler flags after--. Do not deploy withpnpm --filter <package> run deployalone: it skips the gates, the order and the tag.env-deploy --run <service> -- <command> [args...]runs one command with that service'sprodvalues and the credentials. Thedb:prod:migrateanddb:prod:dumpscripts use it:--run apiunderdatabase-postgres, becauseDATABASE_URLis anapikey, and--run deployunderdatabase-d1, which needs only the Cloudflare credentials.env-deploy --checkis step 1 on its own.
Two things the flow does not do. --secrets-file only adds: a secret you removed from the value source stays on the Worker until you run wrangler secret delete <KEY> in its workspace. And a key marked # Blank on purpose passes the check even where production needs it, such as BETTER_AUTH_SECRET; the Worker's own createEnv gate is what refuses it.
.dev.vars is gone
This project writes .env and nothing else. A leftover .dev.vars hides .env from wrangler — wrangler loads .env only while no .dev.vars sits beside it, and it does not warn — so saasaloy doctor reports one and pnpm env:setup carries its values across and deletes it.
process.env
No file a Worker imports reads it. The Worker's env is the only source, and it arrives through createEnv. Node tooling — scripts/, env-deploy, a value source — reads it freely.
04 同仓库更多 Skills
saasaloy-subscription
Runbook for the subscription capability — provider-agnostic subscriptions in packages/subscription, with a plan file, a projection table, a billable-subject file, plan entitlements and per-provider modules. Use when adding a plan, adding a plan feature or a numeric limit, gating an API route with requireFeature or requireWithinLimit, reading the 402 it returns, working out why a subject resolves to the free plan, wiring a checkout or portal route, reading a SubscriptionError, choosing or switching SUBSCRIPTION_PROVIDER, registering a webhook in the vendor's dashboard, writing a third subscription provider, or upgrading a project that still has `billing` installed.
saasaloy-notification
Runbook for the notification module, in-app notifications for apps/app. It owns the notifications and notification_opt_outs tables, notify() in apps/api/src/lib/notify.ts, the /notifications api routes, the bell in the app header, the sidebar badge, the /notifications page with Inbox and Preferences tabs, the notification config section, and the daily prune. Use when sending a notification from an api route, adding or changing a category, changing retention, debugging a badge or bell count that does not move, or removing the module.
saasaloy-cms
Runbook for the cms module, the in-house CMS behind apps/admin. Covers code-defined collections, the Tiptap editor, drafts, revisions and redirects, the publish-time renderer, the public blog on apps/web and its 60-second cache. Use when adding a collection or a field, rendering a stored body, reusing the editor, renderer or media field from another module, moving the blog to another path, re-rendering live posts after a renderer upgrade or a STORAGE_PUBLIC_URL change, or working out why a published post is not live yet.
saasaloy-beta
The beta module's own procedure.
helper
暂无简介
saasaloy-update
Runbook for an AI agent that updates this project to a newer Saasaloy CLI and newer module versions with saasaloy update. Use when the owner says "update saasaloy", "upgrade the CLI", "pull the latest modules", "update the base template", "work the merge plan", "clear the update notes", when saasaloy update or saasaloy outdated --check exits 3 or 2, when a merge plan or .saasaloy/merge-plan.md is handed to you, or when an adopted project takes its first update. Covers the CLI upgrade, the preview, --out, the file merges, the update notes and "Notes that may apply", --resolved and --done, a file deleted locally and --restore, a three-way or two-way base, an update that skips releases, --from, the verify commands, and the exit codes.