Scripts & maintenance CLIs
All scripts run under Bun. Two conventions apply everywhere:
- Destructive CLIs are dry-run by default and only act with
--execute(dev:refresh,queue:admin,backfill-cfdi-encryption,enrich-viewports). There are no interactive prompts; each prints an env banner before acting. - Env comes from Doppler: run every script as
doppler run -- bun run <script-name>from its package directory (see Local setup). The no-Doppler fallback depends on cwd: rootscripts/*rely on Bun auto-loading.env.localfrom the repo root; package-local scripts are registered with--env-file=../../.env.localin theirpackage.json(filled only for keys not already set, and skipped when the file is absent), so always run them viabun run <script-name>from the package directory.
Dev refresh
Section titled “Dev refresh”bun run dev:refresh (scripts/dev-refresh.ts) resets the entire dev environment in one command:
| Order | Step | What happens |
|---|---|---|
| 1 | stream | Full sweep — hard-deletes ALL GetStream messaging channels, then all users (app is dev-dedicated) |
| 2 | knock | Full sweep — deletes ALL Knock users in the environment (workflows/layouts untouched) |
| 3 | stripe | Deletes ONLY customers referenced by the local DB — the test account is shared with staging, so there is no sweep mode by design; products/prices are never touched |
| 4 | facturapi | Cancels test CFDIs (motive 02), then deletes customers referenced by the local DB |
| 5 | storage | Deletes the R2 app-upload prefixes (properties/ — incl. the AI-staging outputs under properties/{id}/staged/ —, pending/properties/, avatars/, agent-avatars/, teams/, floor_plans/, print-ads/) — never search-seeds/, the Meili restore artifacts |
| 6 | db | Runs bun run db:reset in packages/database as a subprocess (drop schema public + migrate to latest) |
| 7 | valkey | FLUSHDB on db0 of the volatile instance (cache) and, when VALKEY_DURABLE_URL names a second instance, on its db0 too (psearch:*); then obliterates every suffixed BullMQ queue in QUEUE_BASES (nine, mirrored from apps/worker/scripts/queue-admin.ts) on the durable db1 (never FLUSHDB there) |
| 8 | meili | Deletes the locations / service_areas / agents indexes; the worker’s ensureSearchIndexes() restores them at boot |
Flags: --execute (required to act), --only <csv>, --skip-<step>,
--allow-remote-db (needed against Railway DEV because Postgres/Valkey/Meili are remote private
services), --allow-unsuffixed, --keep-going.
Preflight. Valkey and Meilisearch are reset after the schema drop, so the script probes both
(for the steps selected) right after the banner and exits before any deletion if either fails —
otherwise Postgres would be gone while psearch:*, the queues and the indexes stayed stale. Valkey:
DBSIZE, then ACL DRYRUN for every command the step will run (FLUSHDB, and SCAN / EVALSHA /
EVAL / DEL on the queue keys when queues are selected), so a command-restricted ACL user is
refused up front rather than failing after the drop. Meilisearch: an authenticated index listing,
so a wrong MEILISEARCH_ADMIN_API_KEY fails here, not after the drop. If a store has no route from
where the script runs (a Railway service without a TCP proxy, say), fix the connection or pass
--skip-valkey / --skip-meili deliberately (and accept the staleness).
Guards: refuses under NODE_ENV=production, with a non-sk_test_ Stripe or Knock key, an sk_live_ FacturAPI key, or a data-store host that is not provably local without the flag. Note the FacturAPI check is a denylist (sk_live_), not the allowlist Stripe and Knock get — a key in neither format passes. A DATABASE_URL / VALKEY_URL / MEILISEARCH_HOST that is unset is skipped, but one that is set and unparsable (or scheme-less, which parses to an empty host) is treated as non-local and blocked. Because Stream keys and bucket names carry no dev/prod marker, --execute additionally requires the sweep-target pins in .env.local: DEV_REFRESH_STREAM_KEY must match PUBLIC_GETSTREAM_API_KEY and DEV_REFRESH_BUCKET must match AWS_S3_BUCKET_NAME — a production env copied into .env.local fails the match instead of being swept. External ids are collected before the schema drop, and recorded external failures abort before the DB is touched — unless --keep-going, and excepting CFDI cancellation failures, which are warnings by design (a stamped CFDI that refuses cancellation can’t be fixed by re-running) and so do not block the drop. An orphan-risk guard refuses to drop the DB while Stripe/FacturAPI are unconfigured but the DB still references their objects — skipping those steps silently would strand the external records forever.
After an execute run:
- Restart the worker. Obliterate removed the repeatable schedulers, so boot re-registers them and
restores Meilisearch from the existing R2
search-seeds/*.ndjson.gzartifacts. - Restart
apiandsvelte-webtoo. The schema drop recreated the enum types with new OIDs; a runningpgpool keeps decodingusers.rolesas the raw{A,B}literal until it reconnects (packages/database/src/kysely.ts). - Re-set the search readiness marker.
FLUSHDBremovedpsearch:schema-version; the worker logs a Sentry error at every boot untilcd apps/worker && doppler run -- bun run search:schema -- --verifyruns (it passes trivially on an empty index). - PostgreSQL remains empty after its migrations; no application data is restored.
Vendor sync
Section titled “Vendor sync”bun run scripts/vendor-sync/index.ts (scripts/vendor-sync/) converges the vendor dashboards on what the repo says. CI runs it — together with the Knock CLI push — as the vendor-sync job of ci.yml on every push to development once CI gate is green, so Knock, Stripe and GetStream change in the same run as the code (PRO-102). Every step reads the live state and writes only the differences; a push with nothing to sync is a no-op, which is why the job is not path-gated.
| Vendor | Source of truth | What converges |
|---|---|---|
stripe | BASE_PLANS / ADDON_UNITS (@repo/validation plan-rules), STRIPE_EVENT_ALLOWLIST (apps/api/src/stripe-webhook-events.ts), PORTAL_FEATURES (stripe-portal.ts) | One product per catalog entry (metadata.catalog_id) and one price per lookup_key — MXN, catalog cents, tax-inclusive, 1×month / 1×year, the same invariants checkout verifies; a hand-made price that already matches is adopted, product included. The endpoint at STRIPE_WEBHOOK_URL gets exactly the allowlisted events (never created here — a new endpoint means a new signing secret in Doppler). The default Customer Portal configuration’s features. |
stream | stream/channel-types/messaging.json, stream/app-settings.json | Only the keys present in the files, from an allowlist of non-secret settings; push-provider / SQS / SNS / event-hook keys are rejected outright. {} manages nothing. |
Flags: --only stripe,stream, --dry-run (print the plan, write nothing), --pull stream (operator seed: write the live app’s allowlisted settings into stream/*.json — the first sync after a pull is a no-op by construction), --allow-price-transfer (let a catalog amount change create a new price, move the lookup_key to it and archive the old one; refused by default because subscriptions still billing on the old price would lose their catalog entry — migrate them first; dev and staging share the test account).
Guards: STRIPE_SECRET_KEY must be a test-mode key (sk_test_ / rk_test_). Env: in CI the job’s GitHub environment development (see Secrets); locally doppler run -- bun run scripts/vendor-sync/index.ts --dry-run. scripts/vendor-sync/*.test.ts run in the tests job and fail a PR whose stream/*.json carries an unknown or forbidden key.
Database (packages/database)
Section titled “Database (packages/database)”| Command | Purpose |
|---|---|
bun run db:reset [--allow-remote-db] | Drop schema public, rerun the ordered Kysely migration chain (scripts/db-reset.ts). Refuses NODE_ENV=production and any non-localhost DATABASE_URL without the flag; prints the target host first. Postgres only — pair with dev:refresh for the full environment (which forwards its own --allow-remote-db) |
bun run db:migrate | Run pending Kysely migrations to latest (scripts/migrate.ts). Connects to DATABASE_URL from .env.local — it has no notion of an environment; prints the target host:port/db first and refuses to run without DATABASE_URL. Local databases only: on Railway the same script is the pre-deploy command of api and worker, run automatically before each deploy goes live (see deploy/README.md § Database migrations) |
bun run db:codegen | Regenerate src/generated/db.ts from the live DB (kysely-codegen) |
bun run scripts/check-enum-array-parser.ts | Verify the "UserRole"[] enum-array node-pg parser fix (roles must decode as string[]) |
Worker & queues (apps/worker)
Section titled “Worker & queues (apps/worker)”| Command | Purpose |
|---|---|
bun --env-file=../../.env.local run queue:admin <cmd> | Operational queue CLI — counts, inspect, retry-failed, promote, DLQ replay. See Incident guides |
bun run backfill:search | Enqueue sync-agent / sync-team for all public agents + teams → rebuilds the Meili agents index |
bun run backfill:properties | Enqueue sync-property for every ACTIVE property → rebuilds psearch:* |
bun run backfill:main-images | One-off repair: promote a cover image for properties with photos but no isMain row |
bun run search:schema | Property-search schema migration command (--dry-run / --enqueue / --verify) |
Backfills only enqueue — a running worker does the work.
Search data (apps/svelte-web)
Section titled “Search data (apps/svelte-web)”| Command | Purpose |
|---|---|
bun run sync:locations | Wipe + rebuild the Meili locations index from the SEPOMEX XML (not in git — slow path; prefer the S3 seed restore) |
bun run sync:service-areas | Same wipe-and-rebuild for service_areas |
bun run export:search-seed | Export locations + service_areas to gzipped NDJSON seeds in R2 (search-seeds/) — what ensureSearchIndexes() restores from |
bun --env-file=../../.env.local run scripts/enrich-viewports.ts | Re-geocode _geo + viewport bounds via Google Geocoding (--execute gated) |
i18n, tokens & contract codegen
Section titled “i18n, tokens & contract codegen”| Command | Purpose |
|---|---|
bun native/tooling/src/sync-native-i18n.ts | Fan the shared native/i18n/{user,agent}/{en,es}.json catalogs out to the four native apps |
bun design-tokens/export-native-tokens.mjs | Regenerate Tokens.swift / Tokens.kt / tokens.generated.css from design-tokens/tokens.cjs and copy the brand TTFs from design-tokens/fonts/ into MeridianUI and :core:design-system — commit the output |
bun run packages/contract/scripts/codegen-models.ts | Regenerate the native Swift/Kotlin DTOs from the @repo/contract Zod schemas |
bun run scripts/contract-fixtures/generate-fixtures.ts | Capture real tRPC response envelopes into the canonical packages/contract/fixtures/{user,agent}/ |
bun native/tooling/src/sync-contract-fixtures.ts | Copy packages/contract/fixtures/ into the iOS/Android test trees (--check fails on a drifted copy; CI runs it) |
cd packages/contract && bun run fixtures:validate | Parse each committed fixture through its authoritative Zod schema — fails on contract drift |
Guards & CI checks (root scripts/ + native/tooling/)
Section titled “Guards & CI checks (root scripts/ + native/tooling/)”| Command | Purpose |
|---|---|
bun run scripts/check-job-data-casts.ts | Enforce that BullMQ processors Zod-parse job.data instead of type-asserting (FSR-019/086) |
bun run scripts/check-env-schema.ts | Compare the @repo/config env schemas with turbo.json (globalEnv, task env) and the root + per-app .env.example files; fails on a key missing from either side (PRO-170) |
bun run scripts/check-process-env.ts | Fail on an environment read outside @repo/config: process.env, Bun.env or SvelteKit’s $env/*/private in src/ of a host or package. Walks the TypeScript AST, so comments never trip it; tests, scripts and build config are out of scope (PRO-170) |
bun native/tooling/src/check-i18n-parity.ts | Native catalog invariants: en↔es key parity, synced copies byte-equal, every native t("…") key exists |
bun test scripts/ci-change-scopes.test.ts | Assert ci.yml and the /commit classifier both source .github/scripts/change-scopes.sh and pick the same jobs for pinned native paths |
bun run docs:check | Build the docs site, then run docs-stale-check.ts (stale references) + docs-link-check.ts (broken relative links) |
bun run scripts/generate-email-previews.ts | Regenerate the static email-templates/ previews from the real email builders — commit the output |
One-off backfills & parity tests
Section titled “One-off backfills & parity tests”| Command | Purpose |
|---|---|
packages/api/scripts/parity-*.ts (14 scripts) | Behavior + atomicity tests for the Kysely-migrated services — run against a scratch DB (they write and clean up): DATABASE_URL=<scratch> bun run scripts/parity-leads.ts |
bun run packages/api/scripts/backfill-cfdi-encryption.ts | Encrypt pre-FSR-014 plaintext fiscal PII rows in place (--execute gated) |
bun run packages/integrations/scripts/backfill-inquiry-chat-channels.ts | One-off backfill of Inquiry.chatChannelId from derived GetStream channel ids (migration 0012) |
bun native/tooling/src/firebase-distribute.ts | Build + upload a signed native app artifact to Firebase App Distribution. See Release playbooks |