@sdxc/kit 2026.10.16 → 2026.10.20
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 +391 -20
- package/changelog.jsonl +1160 -1035
- package/config.schema.json +47 -1
- package/dist/author.d.ts +36 -0
- package/dist/author.js +107 -0
- package/dist/cli.js +12 -0
- package/dist/commands/create.js +9 -4
- package/dist/commands/deploy.d.ts +23 -0
- package/dist/commands/deploy.js +167 -0
- package/dist/commands/destroy.d.ts +22 -0
- package/dist/commands/destroy.js +92 -0
- package/dist/commands/doctor.d.ts +26 -0
- package/dist/commands/doctor.js +65 -0
- package/dist/commands/initializers.d.ts +16 -0
- package/dist/commands/initializers.js +23 -0
- package/dist/commands/make.d.ts +72 -6
- package/dist/commands/make.js +127 -19
- package/dist/commands/middleware.d.ts +16 -0
- package/dist/commands/middleware.js +21 -0
- package/dist/commands/models.d.ts +18 -0
- package/dist/commands/models.js +29 -0
- package/dist/commands/notes.d.ts +17 -0
- package/dist/commands/notes.js +27 -0
- package/dist/commands/routes.d.ts +25 -0
- package/dist/commands/routes.js +50 -0
- package/dist/commands/run.d.ts +18 -2
- package/dist/commands/run.js +35 -12
- package/dist/commands/scaffold.d.ts +29 -0
- package/dist/commands/scaffold.js +33 -0
- package/dist/commands/schedule.d.ts +16 -0
- package/dist/commands/schedule.js +21 -0
- package/dist/commands/stats.d.ts +16 -0
- package/dist/commands/stats.js +21 -0
- package/dist/commands/ui/update.d.ts +6 -3
- package/dist/commands/ui/update.js +24 -7
- package/dist/commands/upgrade.d.ts +3 -1
- package/dist/commands/upgrade.js +88 -23
- package/dist/config.d.ts +18 -0
- package/dist/config.js +28 -1
- package/dist/contract.d.ts +97 -3
- package/dist/contract.js +360 -26
- package/dist/create/templates.d.ts +8 -1
- package/dist/create/templates.js +26 -2
- package/dist/dependencies.d.ts +4 -1
- package/dist/dependencies.js +13 -1
- package/dist/deploy/plan.d.ts +49 -0
- package/dist/deploy/plan.js +68 -0
- package/dist/destroy.d.ts +40 -0
- package/dist/destroy.js +311 -0
- package/dist/doctor/check.d.ts +71 -0
- package/dist/doctor/check.js +53 -0
- package/dist/doctor/checks.d.ts +23 -0
- package/dist/doctor/checks.js +68 -0
- package/dist/doctor/define.d.ts +59 -0
- package/dist/doctor/define.js +16 -0
- package/dist/doctor/emails.d.ts +18 -0
- package/dist/doctor/emails.js +243 -0
- package/dist/doctor/env.d.ts +26 -0
- package/dist/doctor/env.js +88 -0
- package/dist/doctor/files.d.ts +14 -0
- package/dist/doctor/files.js +34 -0
- package/dist/doctor/fixture.d.ts +23 -0
- package/dist/doctor/fixture.js +46 -0
- package/dist/doctor/install.d.ts +17 -0
- package/dist/doctor/install.js +37 -0
- package/dist/doctor/installed.d.ts +11 -0
- package/dist/doctor/installed.js +79 -0
- package/dist/doctor/layout.d.ts +23 -0
- package/dist/doctor/layout.js +83 -0
- package/dist/doctor/migrations.d.ts +16 -0
- package/dist/doctor/migrations.js +96 -0
- package/dist/doctor/models.d.ts +13 -0
- package/dist/doctor/models.js +69 -0
- package/dist/doctor/pins.d.ts +15 -0
- package/dist/doctor/pins.js +79 -0
- package/dist/doctor/project.d.ts +58 -0
- package/dist/doctor/project.js +261 -0
- package/dist/doctor/remix.d.ts +14 -0
- package/dist/doctor/remix.js +44 -0
- package/dist/doctor/routes.d.ts +11 -0
- package/dist/doctor/routes.js +49 -0
- package/dist/doctor/typegen.d.ts +13 -0
- package/dist/doctor/typegen.js +57 -0
- package/dist/doctor/ui.d.ts +14 -0
- package/dist/doctor/ui.js +51 -0
- package/dist/init.d.ts +5 -3
- package/dist/init.js +15 -7
- package/dist/initializers.d.ts +25 -0
- package/dist/initializers.js +89 -0
- package/dist/lock.d.ts +2 -2
- package/dist/make/actions.d.ts +23 -0
- package/dist/make/actions.js +82 -0
- package/dist/make/declare.d.ts +32 -4
- package/dist/make/declare.js +405 -27
- package/dist/make/email.d.ts +23 -0
- package/dist/make/email.js +111 -0
- package/dist/make/from-fields.d.ts +30 -0
- package/dist/make/from-fields.js +162 -0
- package/dist/make/from-model.d.ts +39 -0
- package/dist/make/from-model.js +161 -0
- package/dist/make/generate.d.ts +16 -5
- package/dist/make/generate.js +43 -14
- package/dist/make/guard.d.ts +35 -0
- package/dist/make/guard.js +73 -0
- package/dist/make/imports.d.ts +6 -0
- package/dist/make/imports.js +19 -0
- package/dist/make/index.d.ts +47 -4
- package/dist/make/index.js +287 -25
- package/dist/make/kinds.d.ts +31 -3
- package/dist/make/kinds.js +82 -9
- package/dist/make/mount.d.ts +25 -0
- package/dist/make/mount.js +135 -0
- package/dist/make/schema-diff.d.ts +58 -0
- package/dist/make/schema-diff.js +609 -0
- package/dist/make/schema.d.ts +76 -0
- package/dist/make/schema.js +206 -0
- package/dist/make/stubs.d.ts +7 -5
- package/dist/make/stubs.js +16 -5
- package/dist/make/test.d.ts +43 -0
- package/dist/make/test.js +149 -0
- package/dist/make/trigger.d.ts +31 -0
- package/dist/make/trigger.js +163 -0
- package/dist/models.d.ts +42 -0
- package/dist/models.js +149 -0
- package/dist/notes.d.ts +28 -0
- package/dist/notes.js +66 -0
- package/dist/reference.d.ts +27 -0
- package/dist/reference.js +103 -0
- package/dist/releases.d.ts +9 -0
- package/dist/releases.js +29 -8
- package/dist/routes/chains.d.ts +34 -0
- package/dist/routes/chains.js +95 -0
- package/dist/routes/filter.d.ts +53 -0
- package/dist/routes/filter.js +147 -0
- package/dist/routes/format.d.ts +23 -0
- package/dist/routes/format.js +123 -0
- package/dist/routes/helpers.d.ts +21 -0
- package/dist/routes/helpers.js +165 -0
- package/dist/routes/index.d.ts +56 -0
- package/dist/routes/index.js +167 -0
- package/dist/routes/modules.d.ts +116 -0
- package/dist/routes/modules.js +235 -0
- package/dist/routes/parse.d.ts +161 -0
- package/dist/routes/parse.js +621 -0
- package/dist/routes/routers.d.ts +69 -0
- package/dist/routes/routers.js +327 -0
- package/dist/scaffold/fields.d.ts +129 -0
- package/dist/scaffold/fields.js +421 -0
- package/dist/scaffold/index.d.ts +37 -0
- package/dist/scaffold/index.js +613 -0
- package/dist/scaffold/render.d.ts +51 -0
- package/dist/scaffold/render.js +332 -0
- package/dist/schedule.d.ts +45 -0
- package/dist/schedule.js +124 -0
- package/dist/stats.d.ts +37 -0
- package/dist/stats.js +126 -0
- package/dist/testing.d.ts +12 -2
- package/dist/testing.js +22 -3
- package/dist/ui/commands.d.ts +10 -3
- package/dist/ui/commands.js +85 -15
- package/dist/ui/registry.d.ts +8 -8
- package/dist/upgrade/agents.d.ts +49 -0
- package/dist/upgrade/agents.js +110 -0
- package/dist/upgrade/changelog.d.ts +2 -2
- package/dist/upgrade/project.d.ts +4 -0
- package/dist/upgrade/project.js +11 -1
- package/dist/upgrade/prompt.d.ts +11 -0
- package/dist/upgrade/prompt.js +50 -7
- package/package.json +6 -4
- package/registry/ui/alert-dialog.json +2 -2
- package/registry/ui/attachment.json +2 -2
- package/registry/ui/calendar.json +2 -2
- package/registry/ui/color-picker.json +2 -2
- package/registry/ui/command.json +2 -2
- package/registry/ui/date-picker.json +2 -2
- package/registry/ui/date-range-picker.json +2 -2
- package/registry/ui/dialog.json +2 -2
- package/registry/ui/file-trigger.json +2 -2
- package/registry/ui/heading-scope.json +2 -2
- package/registry/ui/message-scroller.json +2 -2
- package/registry/ui/number-field.json +2 -2
- package/registry/ui/range-calendar.json +2 -2
- package/registry/ui/resizable.json +2 -2
- package/registry/ui/select.json +2 -2
- package/registry/ui/shared-element.json +2 -2
- package/stubs/command.stub +4 -0
- package/stubs/command.test.stub +3 -0
- package/stubs/controller.api.resource.stub +23 -0
- package/stubs/controller.api.stub +21 -0
- package/stubs/controller.api.test.stub +18 -0
- package/stubs/controller.form.stub +40 -10
- package/stubs/controller.form.test.stub +61 -0
- package/stubs/controller.resource.form.stub +67 -0
- package/stubs/controller.resource.stub +28 -0
- package/stubs/controller.stub +15 -8
- package/stubs/controller.test.stub +3 -0
- package/stubs/email.stub +15 -38
- package/stubs/email.test.stub +12 -7
- package/stubs/factory.stub +4 -1
- package/stubs/initializer.stub +2 -0
- package/stubs/initializer.test.stub +3 -0
- package/stubs/job.cron.stub +4 -0
- package/stubs/job.stub +4 -0
- package/stubs/job.test.stub +3 -0
- package/stubs/mcp.resource.stub +3 -0
- package/stubs/mcp.stub +3 -0
- package/stubs/mcp.test.stub +3 -0
- package/stubs/middleware.stub +6 -1
- package/stubs/middleware.test.stub +3 -0
- package/stubs/model.stub +12 -3
- package/stubs/model.test.stub +3 -0
- package/stubs/policy.test.stub +36 -7
- package/stubs/scaffold.api.create.stub +34 -0
- package/stubs/scaffold.api.create.test.stub +37 -0
- package/stubs/scaffold.api.destroy.stub +34 -0
- package/stubs/scaffold.api.destroy.test.stub +44 -0
- package/stubs/scaffold.api.index.stub +23 -0
- package/stubs/scaffold.api.index.test.stub +31 -0
- package/stubs/scaffold.api.show.stub +27 -0
- package/stubs/scaffold.api.show.test.stub +41 -0
- package/stubs/scaffold.api.update.stub +44 -0
- package/stubs/scaffold.api.update.test.stub +46 -0
- package/stubs/scaffold.command.create.stub +22 -0
- package/stubs/scaffold.command.create.test.stub +30 -0
- package/stubs/scaffold.command.delete.stub +20 -0
- package/stubs/scaffold.command.delete.test.stub +39 -0
- package/stubs/scaffold.command.list.stub +18 -0
- package/stubs/scaffold.command.list.test.stub +31 -0
- package/stubs/scaffold.command.show.stub +19 -0
- package/stubs/scaffold.command.show.test.stub +37 -0
- package/stubs/scaffold.controller.destroy.stub +35 -0
- package/stubs/scaffold.controller.destroy.test.stub +65 -0
- package/stubs/scaffold.controller.edit.stub +97 -0
- package/stubs/scaffold.controller.edit.test.stub +91 -0
- package/stubs/scaffold.controller.index.stub +55 -0
- package/stubs/scaffold.controller.index.test.stub +32 -0
- package/stubs/scaffold.controller.new.stub +70 -0
- package/stubs/scaffold.controller.new.test.stub +81 -0
- package/stubs/scaffold.controller.show.stub +59 -0
- package/stubs/scaffold.controller.show.test.stub +43 -0
- package/stubs/scaffold.factory.stub +16 -0
- package/stubs/scaffold.form.stub +75 -0
- package/stubs/scaffold.mcp.create.stub +24 -0
- package/stubs/scaffold.mcp.create.test.stub +36 -0
- package/stubs/scaffold.mcp.delete.stub +31 -0
- package/stubs/scaffold.mcp.delete.test.stub +43 -0
- package/stubs/scaffold.mcp.get.stub +21 -0
- package/stubs/scaffold.mcp.get.test.stub +31 -0
- package/stubs/scaffold.mcp.list.stub +23 -0
- package/stubs/scaffold.mcp.list.test.stub +25 -0
- package/stubs/scaffold.mcp.update.stub +37 -0
- package/stubs/scaffold.mcp.update.test.stub +40 -0
- package/stubs/scaffold.migration.stub +2 -0
- package/stubs/scaffold.model.stub +35 -0
- package/stubs/scaffold.model.test.stub +25 -0
- package/stubs/scaffold.test-database.stub +33 -0
- package/stubs/scaffold.test-mcp.stub +92 -0
- package/stubs/scaffold.validator.stub +18 -0
- package/stubs/scaffold.validator.test.stub +26 -0
- package/stubs/service.stub +4 -0
- package/stubs/service.test.stub +3 -0
- package/stubs/validator.stub +3 -0
- package/stubs/validator.test.stub +3 -0
- package/templates/app/.env.example +16 -0
- package/templates/app/AGENTS.md +163 -0
- package/templates/app/CLAUDE.md +1 -0
- package/templates/app/README.md +79 -37
- package/templates/app/app/commands/about.test.ts +1 -1
- package/templates/app/app/commands/console.test.ts +26 -0
- package/templates/app/app/commands/console.ts +19 -0
- package/templates/app/app/commands/db/console.ts +6 -0
- package/templates/app/app/commands/db/fresh.test.ts +57 -0
- package/templates/app/app/commands/db/fresh.ts +37 -0
- package/templates/app/app/commands/db/migrate.test.ts +46 -0
- package/templates/app/app/commands/db/migrate.ts +88 -0
- package/templates/app/app/commands/db/prepare.test.ts +39 -0
- package/templates/app/app/commands/db/prepare.ts +35 -0
- package/templates/app/app/commands/db/seed.test.ts +38 -0
- package/templates/app/app/commands/db/seed.ts +26 -0
- package/templates/app/app/commands/deploy.test.ts +34 -0
- package/templates/app/app/commands/deploy.ts +18 -0
- package/templates/app/app/commands/emails/preview.test.tsx +63 -0
- package/templates/app/app/commands/emails/preview.ts +24 -0
- package/templates/app/app/commands/index.ts +72 -1
- package/templates/app/app/commands/typegen.test.ts +17 -0
- package/templates/app/app/commands/typegen.ts +16 -0
- package/templates/app/app/emails/index.ts +10 -0
- package/templates/app/app/http/controllers/api/health.tsx +15 -0
- package/templates/app/app/http/controllers/api/not-found.tsx +16 -0
- package/templates/app/app/http/controllers/api/openapi.tsx +17 -0
- package/templates/app/app/http/controllers/home.tsx +21 -13
- package/templates/app/app/http/controllers/mcp.tsx +16 -0
- package/templates/app/app/http/controllers/not-found.tsx +23 -18
- package/templates/app/app/http/controllers/robots.test.ts +18 -0
- package/templates/app/app/http/controllers/robots.ts +24 -0
- package/templates/app/app/http/controllers/sitemap.test.ts +28 -0
- package/templates/app/app/http/controllers/sitemap.ts +25 -0
- package/templates/app/app/http/middleware/access.test.ts +43 -0
- package/templates/app/app/http/middleware/access.ts +41 -0
- package/templates/app/app/http/middleware/api.ts +19 -0
- package/templates/app/app/http/middleware/page.test.ts +43 -0
- package/templates/app/app/http/middleware/page.ts +51 -0
- package/templates/app/app/http/middleware/server-timing.test.ts +15 -0
- package/templates/app/app/http/middleware/server-timing.ts +42 -0
- package/templates/app/app/initializers/assets.ts +1 -1
- package/templates/app/app/initializers/captcha.ts +11 -0
- package/templates/app/app/initializers/cli.ts +68 -2
- package/templates/app/app/initializers/database.test.ts +18 -0
- package/templates/app/app/initializers/database.ts +18 -0
- package/templates/app/app/initializers/dispatcher.ts +26 -0
- package/templates/app/app/initializers/honeypot.ts +11 -0
- package/templates/app/app/initializers/i18n.test.ts +16 -0
- package/templates/app/app/initializers/i18n.ts +28 -0
- package/templates/app/app/initializers/local-database.test.ts +33 -0
- package/templates/app/app/initializers/local-database.ts +53 -0
- package/templates/app/app/initializers/mail-transport.test.ts +27 -0
- package/templates/app/app/initializers/mail-transport.ts +26 -0
- package/templates/app/app/initializers/mail.test.ts +27 -0
- package/templates/app/app/initializers/mail.ts +19 -0
- package/templates/app/app/initializers/mcp.ts +1 -1
- package/templates/app/app/initializers/openapi.ts +21 -0
- package/templates/app/app/initializers/router.test.ts +68 -0
- package/templates/app/app/initializers/router.tsx +81 -25
- package/templates/app/app/initializers/seo.ts +15 -0
- package/templates/app/app/initializers/session.test.ts +34 -0
- package/templates/app/app/initializers/session.ts +29 -0
- package/templates/app/app/initializers/spam.ts +10 -0
- package/templates/app/app/jobs/email.test.tsx +70 -0
- package/templates/app/app/jobs/email.ts +16 -0
- package/templates/app/app/jobs/index.ts +6 -2
- package/templates/app/app/models/index.ts +10 -0
- package/templates/app/app/policies/abilities.ts +13 -0
- package/templates/app/app/policies/policy.test.ts +15 -0
- package/templates/app/app/policies/policy.ts +30 -0
- package/templates/app/app/services/commands.ts +30 -0
- package/templates/app/app/services/migrations.test.ts +92 -0
- package/templates/app/app/services/migrations.ts +117 -0
- package/templates/app/app/test/cloudflare-workers.ts +25 -0
- package/templates/app/app/test/local-database.ts +67 -0
- package/templates/app/app/test/session.ts +28 -0
- package/templates/app/bootstrap/cli.ts +1 -1
- package/templates/app/bootstrap/console.ts +13 -0
- package/templates/app/bootstrap/worker.ts +4 -4
- package/templates/app/cloudflare.config.ts +30 -2
- package/templates/app/config/constants.ts +19 -0
- package/templates/app/config/problems.ts +24 -0
- package/templates/app/config/security-policy.ts +34 -0
- package/templates/app/config/security-txt.ts +28 -0
- package/templates/app/database/migrations/.gitkeep +0 -0
- package/templates/app/database/seed.ts +12 -0
- package/templates/app/package.json +37 -6
- package/templates/app/resources/layouts/document.tsx +28 -7
- package/templates/app/resources/locales/en.json +10 -0
- package/templates/app/routes/api.ts +29 -0
- package/templates/app/routes/web.ts +6 -4
- package/templates/app/vite.config.ts +4 -0
- package/stubs/policy.stub +0 -13
- package/templates/app/config/router-context.d.ts +0 -20
package/templates/app/README.md
CHANGED
|
@@ -1,57 +1,99 @@
|
|
|
1
1
|
# App
|
|
2
2
|
|
|
3
|
-
A Remix v3
|
|
3
|
+
A Remix v3 app on Cloudflare Workers, built on the `@sdxc/*` packages.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Start
|
|
6
6
|
|
|
7
7
|
```sh
|
|
8
8
|
bun install
|
|
9
|
-
|
|
9
|
+
sdxc run typegen
|
|
10
|
+
sdxc run db prepare
|
|
10
11
|
bun dev
|
|
11
12
|
```
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
In a fresh clone, copy `.env.example` to `.dev.vars` first. `db prepare` applies the pending
|
|
15
|
+
migrations, and seeds a database that had none applied. Run `sdxc run typegen` again
|
|
16
|
+
whenever you change a binding in `cloudflare.config.ts`.
|
|
15
17
|
|
|
16
|
-
##
|
|
17
|
-
|
|
18
|
-
`sdxc make <kind> <name>` writes a file of that kind where it belongs, with a test beside it,
|
|
19
|
-
and adds any `@sdxc/*` package the file imports to `package.json`. The `sdxc` command comes
|
|
20
|
-
from `npm add -g @sdxc/kit`:
|
|
18
|
+
## Everyday commands
|
|
21
19
|
|
|
22
20
|
```sh
|
|
23
|
-
sdxc make controller about
|
|
21
|
+
sdxc make controller about # a page, mapped in routes/web.ts
|
|
22
|
+
sdxc make controller api/keys --routes api # a JSON endpoint
|
|
23
|
+
sdxc make model post title body # ctx.models.post, its migration and factory
|
|
24
|
+
sdxc make migration create-tags name # then: sdxc run db migrate
|
|
24
25
|
sdxc make job reports/daily --cron "0 8 * * *"
|
|
25
|
-
sdxc make
|
|
26
|
+
sdxc make email welcome name # then: ctx.email.later(emails.welcome, { to, props })
|
|
27
|
+
sdxc make command users/grant # then: sdxc run users grant
|
|
28
|
+
sdxc scaffold post title body:text # model, migration, CRUD pages and tests
|
|
29
|
+
|
|
30
|
+
sdxc run db console # SQL prompt over the local database
|
|
31
|
+
sdxc run console # Bun's REPL with db and models loaded
|
|
32
|
+
sdxc run db seed # run database/seed.ts on the local database
|
|
33
|
+
sdxc run db fresh --seed # drop every local table, migrate, seed
|
|
34
|
+
sdxc run emails preview welcome # print an email from its preview(); /_emails in bun dev
|
|
35
|
+
sdxc routes # every route and its controller (-g, -c, --unused)
|
|
36
|
+
sdxc doctor # release drift, secrets, migrations and wiring
|
|
37
|
+
bun check # format, lint and types
|
|
38
|
+
bun run test
|
|
26
39
|
```
|
|
27
40
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
`app/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
`sdxc make` writes the file where it belongs with a test beside it, and maps it so it works
|
|
42
|
+
right away. `sdxc` comes with the project; `npm add -g @sdxc/kit` puts it on your path.
|
|
43
|
+
|
|
44
|
+
`AGENTS.md` teaches an AI coding agent these commands and conventions, and `CLAUDE.md` points
|
|
45
|
+
Claude Code at it. `sdxc upgrade` keeps its sdxc section current; add your own rules below it.
|
|
46
|
+
|
|
47
|
+
## Where things go
|
|
48
|
+
|
|
49
|
+
| Folder | What lives there |
|
|
50
|
+
| ---------------------- | ----------------------------------------------------------- |
|
|
51
|
+
| `routes/` | URL contracts: `web.ts` for pages, `api.ts` for endpoints |
|
|
52
|
+
| `app/http/` | Controllers, middleware and input validators |
|
|
53
|
+
| `app/models/` | Database models, one per table |
|
|
54
|
+
| `app/jobs/` | Background jobs |
|
|
55
|
+
| `app/emails/` | Emails |
|
|
56
|
+
| `app/commands/` | Commands for `sdxc run` |
|
|
57
|
+
| `app/initializers/` | The one instance of each service: database, mailer, router… |
|
|
58
|
+
| `resources/` | Layouts, components and translations (`resources/locales/`) |
|
|
59
|
+
| `database/migrations/` | SQL migrations |
|
|
60
|
+
| `database/seed.ts` | The rows `sdxc run db seed` writes into the local database |
|
|
61
|
+
| `config/` | Site settings, security policy, security.txt, API problems |
|
|
62
|
+
|
|
63
|
+
## What a handler gets
|
|
64
|
+
|
|
65
|
+
Every request, job and command can read:
|
|
66
|
+
|
|
67
|
+
- `ctx.db` and `ctx.models`: the D1 database and its models.
|
|
68
|
+
- `ctx.email`: sends an email `app/emails/index.ts` declares, `await send()` now, `later()`
|
|
69
|
+
after the response, or `await queue()` to the `email` job, which retries it. A command prints
|
|
70
|
+
it instead of sending it.
|
|
71
|
+
- `ctx.log`, with a `trace_id` shared by a request and the jobs it starts.
|
|
72
|
+
- `ctx.timing`: adds entries to the `Server-Timing` header (requests only).
|
|
73
|
+
- `ctx.ip` and `ctx.userAgent` (requests only): the client's address, or `null`, and its parsed
|
|
74
|
+
`User-Agent` header.
|
|
75
|
+
|
|
76
|
+
Pages also get `ctx.session`, `ctx.intl` for translated copy, and forms protected by a
|
|
77
|
+
honeypot, Turnstile and a spam filter. Before a page loads a script, style, image or font from
|
|
78
|
+
another origin, add that origin to `config/security-policy.ts`. API errors use the catalog in
|
|
79
|
+
`config/problems.ts`.
|
|
80
|
+
|
|
81
|
+
## Before the first deploy
|
|
82
|
+
|
|
83
|
+
1. Create the resources and put their ids in `cloudflare.config.ts`:
|
|
84
|
+
`bunx cf d1 create --name <name>`, `bunx cf kv namespaces create --title <name>`, and
|
|
85
|
+
`bunx cf queues create --queue-name <name>` (the `name` in `package.json`).
|
|
86
|
+
2. Set every secret in `.env.example` with `bunx cf workers secrets update <NAME>`.
|
|
87
|
+
3. Edit `config/constants.ts`: your domain, site name and description, and a `MAIL_FROM`
|
|
88
|
+
address on a domain with Cloudflare Email Routing.
|
|
89
|
+
4. Put your contact in `config/security-txt.ts`.
|
|
90
|
+
5. Deploy:
|
|
48
91
|
|
|
49
92
|
```sh
|
|
50
93
|
bun check
|
|
51
|
-
|
|
52
|
-
bun build
|
|
53
|
-
bun cf:deploy
|
|
94
|
+
sdxc deploy
|
|
54
95
|
```
|
|
55
96
|
|
|
56
|
-
|
|
57
|
-
|
|
97
|
+
`sdxc deploy` builds, then runs `sdxc run db migrate --remote` and `sdxc run deploy`, and asks
|
|
98
|
+
before it changes production. To migrate or deploy differently, edit `app/commands/db/migrate.ts`
|
|
99
|
+
or `app/commands/deploy.ts`.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `console` command, which hands the terminal to Bun's REPL with the app preloaded.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { invoke } from "@sdxc/cli/test";
|
|
6
|
+
import { describe, expect, test } from "vitest";
|
|
7
|
+
|
|
8
|
+
import { cli } from "~/app/initializers/cli";
|
|
9
|
+
|
|
10
|
+
/** The REPL's command line, as `console` starts it. */
|
|
11
|
+
const REPL = ["bun", "--preload", "./bootstrap/console.ts", "--interactive"];
|
|
12
|
+
|
|
13
|
+
describe("console", () => {
|
|
14
|
+
test("starts Bun's REPL with bootstrap/console.ts preloaded", async () => {
|
|
15
|
+
let run = await invoke(cli, ["console"], { exec: { [REPL.join(" ")]: { code: 0 } } });
|
|
16
|
+
|
|
17
|
+
expect(run.code).toBe(0);
|
|
18
|
+
expect(run.exec.map((call) => call.command)).toEqual([REPL]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("fails when the REPL does", async () => {
|
|
22
|
+
let run = await invoke(cli, ["console"], { exec: { [REPL.join(" ")]: { code: 1 } } });
|
|
23
|
+
|
|
24
|
+
expect(run.code).toBe(1);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `console`: Bun's own REPL with `bootstrap/console.ts` preloaded, so the local database is
|
|
3
|
+
* `db` and the app's models are `models` at the prompt (`await models.post.findMany()`), and
|
|
4
|
+
* top-level `await` works as in a module.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createCommand, success } from "@sdxc/cli";
|
|
8
|
+
import { isFailure } from "@sdxc/result";
|
|
9
|
+
|
|
10
|
+
import commandset from "~/app/commands";
|
|
11
|
+
import { PROJECT_ROOT } from "~/app/initializers/local-database";
|
|
12
|
+
|
|
13
|
+
/** Hands the terminal to the REPL until `.exit` or Ctrl+D, from the project's root. */
|
|
14
|
+
export default createCommand(commandset.console, async (ctx) => {
|
|
15
|
+
let repl = await ctx.exec(["bun", "--preload", "./bootstrap/console.ts", "--interactive"], {
|
|
16
|
+
cwd: PROJECT_ROOT,
|
|
17
|
+
});
|
|
18
|
+
return isFailure(repl) ? repl : success();
|
|
19
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `db fresh`, run on a temporary local database holding rows and a table no migration makes.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { invoke } from "@sdxc/cli/test";
|
|
6
|
+
import { openDatabase } from "@sdxc/cloudflare-mocks/sqlite";
|
|
7
|
+
import { describe, expect, test, vi } from "vitest";
|
|
8
|
+
|
|
9
|
+
import { postsSeed, useTemporaryProject } from "~/app/test/local-database";
|
|
10
|
+
|
|
11
|
+
let project = useTemporaryProject({
|
|
12
|
+
"0001_CreatePosts.sql": "CREATE TABLE posts (id TEXT PRIMARY KEY);",
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
vi.doMock("~/database/seed", () => ({ default: postsSeed("seeded") }));
|
|
16
|
+
|
|
17
|
+
let { cli } = await import("~/app/initializers/cli");
|
|
18
|
+
|
|
19
|
+
/** The ids in the temporary database's `posts`, and whether a stray table survived. */
|
|
20
|
+
function snapshot() {
|
|
21
|
+
let database = openDatabase(project.databaseFile);
|
|
22
|
+
let posts = database.query("SELECT id FROM posts").all();
|
|
23
|
+
let stray = database.query("SELECT name FROM sqlite_master WHERE name = 'stray'").all();
|
|
24
|
+
database.close();
|
|
25
|
+
return { posts, stray: stray.length > 0 };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe("db fresh", () => {
|
|
29
|
+
test("drops every table and applies the chain again", async () => {
|
|
30
|
+
await invoke(cli, ["db", "migrate"]);
|
|
31
|
+
let database = openDatabase(project.databaseFile);
|
|
32
|
+
database.exec("INSERT INTO posts (id) VALUES ('old'); CREATE TABLE stray (id TEXT);");
|
|
33
|
+
database.close();
|
|
34
|
+
|
|
35
|
+
let run = await invoke(cli, ["db", "fresh"]);
|
|
36
|
+
|
|
37
|
+
expect(run.code).toBe(0);
|
|
38
|
+
expect(run.stdout).toBe("Dropped 3 tables\nApplied 0001_CreatePosts.sql\n");
|
|
39
|
+
expect(snapshot()).toEqual({ posts: [], stray: false });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("--seed runs db seed on the empty tables", async () => {
|
|
43
|
+
let run = await invoke(cli, ["db", "fresh", "--seed"]);
|
|
44
|
+
|
|
45
|
+
expect(run.code).toBe(0);
|
|
46
|
+
expect(run.stdout).toContain("Seeded posts\n");
|
|
47
|
+
expect(snapshot().posts).toEqual([{ id: "seeded" }]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("refuses --remote and leaves the database alone", async () => {
|
|
51
|
+
let run = await invoke(cli, ["db", "fresh", "--remote"]);
|
|
52
|
+
|
|
53
|
+
expect(run.code).toBe(1);
|
|
54
|
+
expect(run.stderr).toContain("only on the local database");
|
|
55
|
+
expect(snapshot().posts).toEqual([{ id: "seeded" }]);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `db fresh`: drops every table in the local database and applies the whole migration chain
|
|
3
|
+
* again, for starting over after editing a migration that already ran. `--seed` runs `db seed`
|
|
4
|
+
* on the empty tables.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createCommand, failure, success } from "@sdxc/cli";
|
|
8
|
+
import { openDatabase } from "@sdxc/cloudflare-mocks/sqlite";
|
|
9
|
+
import { isFailure } from "@sdxc/result";
|
|
10
|
+
|
|
11
|
+
import commandset from "~/app/commands";
|
|
12
|
+
import { LOCAL_DATABASE_FILE, PROJECT_ROOT } from "~/app/initializers/local-database";
|
|
13
|
+
import { runCommand } from "~/app/services/commands";
|
|
14
|
+
import { applyMigrations, dropTables, readMigrations } from "~/app/services/migrations";
|
|
15
|
+
|
|
16
|
+
/** Refuses `--remote`: emptying production has no undo. */
|
|
17
|
+
export default createCommand(commandset.db.fresh, async (ctx) => {
|
|
18
|
+
if (ctx.flags.remote) {
|
|
19
|
+
return failure(new Error("db fresh drops every table, so it runs only on the local database"));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let database = openDatabase(LOCAL_DATABASE_FILE);
|
|
23
|
+
try {
|
|
24
|
+
let dropped = dropTables(database);
|
|
25
|
+
if (isFailure(dropped)) return dropped;
|
|
26
|
+
ctx.stdout(
|
|
27
|
+
`Dropped ${dropped.data.length} ${dropped.data.length === 1 ? "table" : "tables"}\n`,
|
|
28
|
+
);
|
|
29
|
+
let applied = applyMigrations(database, await readMigrations(PROJECT_ROOT));
|
|
30
|
+
if (isFailure(applied)) return applied;
|
|
31
|
+
ctx.stdout(applied.data.map((name) => `Applied ${name}\n`).join(""));
|
|
32
|
+
} finally {
|
|
33
|
+
database.close();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return ctx.flags.seed ? runCommand(ctx, ["db", "seed"]) : success();
|
|
37
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `db migrate --remote`, which hands the chain to `cf d1 migrations apply` for the production
|
|
3
|
+
* database named in `cloudflare.config.ts`, and lists what it would apply with `--pending`.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { invoke } from "@sdxc/cli/test";
|
|
7
|
+
import { describe, expect, test } from "vitest";
|
|
8
|
+
|
|
9
|
+
import { cli } from "~/app/initializers/cli";
|
|
10
|
+
|
|
11
|
+
describe("db migrate", () => {
|
|
12
|
+
test("--remote applies the chain through cf to the configured database", async () => {
|
|
13
|
+
let run = await invoke(cli, ["db", "migrate", "--remote"], {
|
|
14
|
+
exec: {
|
|
15
|
+
"cf d1 migrations apply 00000000-0000-4000-8000-000000000000 --dir database/migrations": {
|
|
16
|
+
code: 0,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
expect(run.code).toBe(0);
|
|
22
|
+
expect(run.exec[0]?.command.slice(0, 5)).toEqual([
|
|
23
|
+
"cf",
|
|
24
|
+
"d1",
|
|
25
|
+
"migrations",
|
|
26
|
+
"apply",
|
|
27
|
+
"00000000-0000-4000-8000-000000000000",
|
|
28
|
+
]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("--remote --pending prints each migration production lacks and applies nothing", async () => {
|
|
32
|
+
let run = await invoke(cli, ["db", "migrate", "--remote", "--pending"], {
|
|
33
|
+
exec: {
|
|
34
|
+
"cf d1 migrations list 00000000-0000-4000-8000-000000000000 --dir database/migrations": {
|
|
35
|
+
stdout: JSON.stringify([{ Name: "0001_init.sql" }, { Name: "0002_tags.sql" }]),
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
expect(run.code).toBe(0);
|
|
41
|
+
expect(run.stdout).toBe("0001_init.sql\n0002_tags.sql\n");
|
|
42
|
+
expect(run.exec.map((call) => call.command.slice(0, 4))).toEqual([
|
|
43
|
+
["cf", "d1", "migrations", "list"],
|
|
44
|
+
]);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `db migrate`: applies every migration in `database/migrations/` the database lacks. Locally it
|
|
3
|
+
* writes the SQLite file `bun dev` serves from; with `--remote` it runs `cf d1 migrations apply`
|
|
4
|
+
* against the production database. Safe to run twice: an applied migration is skipped.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { AnyCommandContext } from "@sdxc/cli";
|
|
8
|
+
import type { Result } from "@sdxc/result";
|
|
9
|
+
|
|
10
|
+
import { createCommand, success } from "@sdxc/cli";
|
|
11
|
+
import { openDatabase } from "@sdxc/cloudflare-mocks/sqlite";
|
|
12
|
+
import { failure, isFailure, wrap } from "@sdxc/result";
|
|
13
|
+
import * as s from "remix/data-schema";
|
|
14
|
+
|
|
15
|
+
import commandset from "~/app/commands";
|
|
16
|
+
import { DATABASE_ID, LOCAL_DATABASE_FILE, PROJECT_ROOT } from "~/app/initializers/local-database";
|
|
17
|
+
import {
|
|
18
|
+
applyMigrations,
|
|
19
|
+
MIGRATIONS_DIRECTORY,
|
|
20
|
+
pendingMigrations,
|
|
21
|
+
readMigrations,
|
|
22
|
+
} from "~/app/services/migrations";
|
|
23
|
+
|
|
24
|
+
/** `cf d1 migrations list`'s JSON: one row per migration the production database lacks. */
|
|
25
|
+
const REMOTE_PENDING = s.array(s.object({ Name: s.string() }));
|
|
26
|
+
|
|
27
|
+
/** Escape sequences `cf` colors its JSON with when its output is a terminal. */
|
|
28
|
+
// oxlint-disable-next-line no-control-regex -- matching the ESC that starts a color sequence
|
|
29
|
+
const ANSI = /\u001B\[[0-9;]*m/g;
|
|
30
|
+
|
|
31
|
+
/** The migrations the production database lacks, as `cf d1 migrations list` reports them. */
|
|
32
|
+
async function remotePending(ctx: AnyCommandContext): Promise<Result<string[], Error>> {
|
|
33
|
+
let listed = await ctx.exec(
|
|
34
|
+
["cf", "d1", "migrations", "list", DATABASE_ID, "--dir", MIGRATIONS_DIRECTORY],
|
|
35
|
+
{ cwd: PROJECT_ROOT, stdio: "collect" },
|
|
36
|
+
);
|
|
37
|
+
if (isFailure(listed)) return listed;
|
|
38
|
+
let printed = listed.data.stdout;
|
|
39
|
+
let json = wrap(() => JSON.parse(printed.replace(ANSI, "")) as unknown);
|
|
40
|
+
let parsed = isFailure(json) ? undefined : s.parseSafe(REMOTE_PENDING, json.data);
|
|
41
|
+
if (parsed === undefined || !parsed.success) {
|
|
42
|
+
return failure(new Error(`cf d1 migrations list printed ${printed.trim()}`));
|
|
43
|
+
}
|
|
44
|
+
return success(parsed.value.map((row) => row.Name));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* With `--pending`, prints one migration name per line and applies nothing; an up-to-date
|
|
49
|
+
* database prints nothing. `sdxc deploy` reads that list to show what production will run.
|
|
50
|
+
*/
|
|
51
|
+
export default createCommand(commandset.db.migrate, async (ctx) => {
|
|
52
|
+
if (ctx.flags.remote) {
|
|
53
|
+
if (ctx.flags.pending) {
|
|
54
|
+
let pending = await remotePending(ctx);
|
|
55
|
+
if (isFailure(pending)) return pending;
|
|
56
|
+
ctx.stdout(pending.data.map((name) => `${name}\n`).join(""));
|
|
57
|
+
return success();
|
|
58
|
+
}
|
|
59
|
+
let applied = await ctx.exec(
|
|
60
|
+
["cf", "d1", "migrations", "apply", DATABASE_ID, "--dir", MIGRATIONS_DIRECTORY],
|
|
61
|
+
{ cwd: PROJECT_ROOT },
|
|
62
|
+
);
|
|
63
|
+
return isFailure(applied) ? applied : success();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let database = openDatabase(LOCAL_DATABASE_FILE);
|
|
67
|
+
try {
|
|
68
|
+
let migrations = await readMigrations(PROJECT_ROOT);
|
|
69
|
+
if (ctx.flags.pending) {
|
|
70
|
+
ctx.stdout(
|
|
71
|
+
pendingMigrations(database, migrations)
|
|
72
|
+
.map((migration) => `${migration.name}\n`)
|
|
73
|
+
.join(""),
|
|
74
|
+
);
|
|
75
|
+
return success();
|
|
76
|
+
}
|
|
77
|
+
let applied = applyMigrations(database, migrations);
|
|
78
|
+
if (isFailure(applied)) return applied;
|
|
79
|
+
ctx.stdout(
|
|
80
|
+
applied.data.length === 0
|
|
81
|
+
? "No migrations to apply\n"
|
|
82
|
+
: applied.data.map((name) => `Applied ${name}\n`).join(""),
|
|
83
|
+
);
|
|
84
|
+
return success();
|
|
85
|
+
} finally {
|
|
86
|
+
database.close();
|
|
87
|
+
}
|
|
88
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `db prepare`, run twice on a temporary local database: the first run migrates and seeds, the
|
|
3
|
+
* second finds nothing to do.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { invoke } from "@sdxc/cli/test";
|
|
7
|
+
import { openDatabase } from "@sdxc/cloudflare-mocks/sqlite";
|
|
8
|
+
import { describe, expect, test, vi } from "vitest";
|
|
9
|
+
|
|
10
|
+
import { postsSeed, useTemporaryProject } from "~/app/test/local-database";
|
|
11
|
+
|
|
12
|
+
let project = useTemporaryProject({
|
|
13
|
+
"0001_CreatePosts.sql": "CREATE TABLE posts (id TEXT PRIMARY KEY);",
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
vi.doMock("~/database/seed", () => ({ default: postsSeed("p1") }));
|
|
17
|
+
|
|
18
|
+
let { cli } = await import("~/app/initializers/cli");
|
|
19
|
+
|
|
20
|
+
describe("db prepare", () => {
|
|
21
|
+
test("migrates and seeds a new database, then only migrates", async () => {
|
|
22
|
+
let first = await invoke(cli, ["db", "prepare"]);
|
|
23
|
+
let second = await invoke(cli, ["db", "prepare"]);
|
|
24
|
+
|
|
25
|
+
expect(first.code).toBe(0);
|
|
26
|
+
expect(first.stdout).toBe("Applied 0001_CreatePosts.sql\nSeeded posts\n");
|
|
27
|
+
expect(second.code).toBe(0);
|
|
28
|
+
expect(second.stdout).toBe("No migrations to apply\n");
|
|
29
|
+
let database = openDatabase(project.databaseFile);
|
|
30
|
+
expect(database.query("SELECT id FROM posts").all()).toEqual([{ id: "p1" }]);
|
|
31
|
+
database.close();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("refuses --remote", async () => {
|
|
35
|
+
let run = await invoke(cli, ["db", "prepare", "--remote"]);
|
|
36
|
+
|
|
37
|
+
expect(run.code).toBe(1);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `db prepare`: brings the local database up to date for a fresh clone, CI or after a pull, by
|
|
3
|
+
* running `db migrate`, then `db seed` when the database had no migration applied. Safe to run
|
|
4
|
+
* on every start: a database in use only gains its pending migrations.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createCommand, failure } from "@sdxc/cli";
|
|
8
|
+
import { openDatabase } from "@sdxc/cloudflare-mocks/sqlite";
|
|
9
|
+
import { isFailure } from "@sdxc/result";
|
|
10
|
+
|
|
11
|
+
import commandset from "~/app/commands";
|
|
12
|
+
import { LOCAL_DATABASE_FILE } from "~/app/initializers/local-database";
|
|
13
|
+
import { runCommand } from "~/app/services/commands";
|
|
14
|
+
import { hasAppliedMigrations } from "~/app/services/migrations";
|
|
15
|
+
|
|
16
|
+
/** Seeds only a new database, so its seeded rows are written once. */
|
|
17
|
+
export default createCommand(commandset.db.prepare, async (ctx) => {
|
|
18
|
+
if (ctx.flags.remote) {
|
|
19
|
+
return failure(
|
|
20
|
+
new Error("db prepare runs only on the local database; deploy migrates production"),
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let database = openDatabase(LOCAL_DATABASE_FILE);
|
|
25
|
+
let isNew: boolean;
|
|
26
|
+
try {
|
|
27
|
+
isNew = !hasAppliedMigrations(database);
|
|
28
|
+
} finally {
|
|
29
|
+
database.close();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let migrated = await runCommand(ctx, ["db", "migrate"]);
|
|
33
|
+
if (isFailure(migrated) || !isNew) return migrated;
|
|
34
|
+
return runCommand(ctx, ["db", "seed"]);
|
|
35
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `db seed`, run on a temporary local database with a seeder standing in for the app's own.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { invoke } from "@sdxc/cli/test";
|
|
6
|
+
import { openDatabase } from "@sdxc/cloudflare-mocks/sqlite";
|
|
7
|
+
import { describe, expect, test, vi } from "vitest";
|
|
8
|
+
|
|
9
|
+
import { postsSeed, useTemporaryProject } from "~/app/test/local-database";
|
|
10
|
+
|
|
11
|
+
let project = useTemporaryProject({
|
|
12
|
+
"0001_CreatePosts.sql": "CREATE TABLE posts (id TEXT PRIMARY KEY);",
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
vi.doMock("~/database/seed", () => ({ default: postsSeed("p1") }));
|
|
16
|
+
|
|
17
|
+
let { cli } = await import("~/app/initializers/cli");
|
|
18
|
+
|
|
19
|
+
describe("db seed", () => {
|
|
20
|
+
test("runs each seeder on the local database and names it", async () => {
|
|
21
|
+
await invoke(cli, ["db", "migrate"]);
|
|
22
|
+
|
|
23
|
+
let run = await invoke(cli, ["db", "seed"]);
|
|
24
|
+
|
|
25
|
+
expect(run.code).toBe(0);
|
|
26
|
+
expect(run.stdout).toBe("Seeded posts\n");
|
|
27
|
+
let database = openDatabase(project.databaseFile);
|
|
28
|
+
expect(database.query("SELECT id FROM posts").all()).toEqual([{ id: "p1" }]);
|
|
29
|
+
database.close();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("refuses --remote", async () => {
|
|
33
|
+
let run = await invoke(cli, ["db", "seed", "--remote"]);
|
|
34
|
+
|
|
35
|
+
expect(run.code).toBe(1);
|
|
36
|
+
expect(run.stderr).toContain("only to the local database");
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `db seed`: runs the steps of `database/seed.ts` on the local database, through the app's
|
|
3
|
+
* models and factories. It writes rows as often as it runs, so a database seeds once through
|
|
4
|
+
* `db prepare` and is reseeded from empty with `db fresh --seed`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createCommand, failure, success } from "@sdxc/cli";
|
|
8
|
+
import { isFailure } from "@sdxc/result";
|
|
9
|
+
|
|
10
|
+
import commandset from "~/app/commands";
|
|
11
|
+
import seeder from "~/database/seed";
|
|
12
|
+
|
|
13
|
+
/** Prints each step's name once it ran; production rows come from the app itself. */
|
|
14
|
+
export default createCommand(commandset.db.seed, async (ctx) => {
|
|
15
|
+
if (ctx.flags.remote) {
|
|
16
|
+
return failure(new Error("db seed writes only to the local database"));
|
|
17
|
+
}
|
|
18
|
+
if (Object.keys(seeder.steps).length === 0) {
|
|
19
|
+
ctx.stdout("No seed steps in database/seed.ts\n");
|
|
20
|
+
return success();
|
|
21
|
+
}
|
|
22
|
+
let ran = await seeder.run({ db: ctx.db });
|
|
23
|
+
if (isFailure(ran)) return ran;
|
|
24
|
+
ctx.stdout(ran.data.map((name) => `Seeded ${name}\n`).join(""));
|
|
25
|
+
return success();
|
|
26
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `deploy` command, which uploads the existing build with `cf` from the project's root.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { invoke } from "@sdxc/cli/test";
|
|
6
|
+
import { describe, expect, test } from "vitest";
|
|
7
|
+
|
|
8
|
+
import { cli } from "~/app/initializers/cli";
|
|
9
|
+
|
|
10
|
+
describe("deploy", () => {
|
|
11
|
+
test("uploads the prebuilt Worker with cf", async () => {
|
|
12
|
+
let run = await invoke(cli, ["deploy"], { exec: { "cf deploy --prebuilt": { code: 0 } } });
|
|
13
|
+
|
|
14
|
+
expect(run.code).toBe(0);
|
|
15
|
+
expect(run.exec.map((call) => call.command)).toEqual([["cf", "deploy", "--prebuilt"]]);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("--dry-run hands cf its own dry run", async () => {
|
|
19
|
+
let run = await invoke(cli, ["deploy", "--dry-run"], {
|
|
20
|
+
exec: { "cf deploy --prebuilt --dry-run": { code: 0 } },
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
expect(run.code).toBe(0);
|
|
24
|
+
expect(run.exec.map((call) => call.command)).toEqual([
|
|
25
|
+
["cf", "deploy", "--prebuilt", "--dry-run"],
|
|
26
|
+
]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("fails when cf does", async () => {
|
|
30
|
+
let run = await invoke(cli, ["deploy"], { exec: { "cf deploy --prebuilt": { code: 1 } } });
|
|
31
|
+
|
|
32
|
+
expect(run.code).toBe(1);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deploy`: uploads the Worker the build wrote to `.cloudflare/output` with `cf deploy
|
|
3
|
+
* --prebuilt`. `sdxc deploy` runs it once the build and the production migrations succeed, so
|
|
4
|
+
* this command is where the app decides how it ships; edit it to deploy anywhere else.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createCommand, success } from "@sdxc/cli";
|
|
8
|
+
import { isFailure } from "@sdxc/result";
|
|
9
|
+
|
|
10
|
+
import commandset from "~/app/commands";
|
|
11
|
+
import { PROJECT_ROOT } from "~/app/initializers/local-database";
|
|
12
|
+
|
|
13
|
+
/** With `--dry-run`, `cf` checks the build and the account and uploads nothing. */
|
|
14
|
+
export default createCommand(commandset.deploy, async (ctx) => {
|
|
15
|
+
let command = ["cf", "deploy", "--prebuilt", ...(ctx.flags.dryRun ? ["--dry-run"] : [])];
|
|
16
|
+
let deployed = await ctx.exec(command, { cwd: PROJECT_ROOT });
|
|
17
|
+
return isFailure(deployed) ? deployed : success();
|
|
18
|
+
});
|