create-wervt 0.2.2 → 0.2.4

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/index.js CHANGED
@@ -13,21 +13,28 @@ const BUNDLED_TEMPLATE = new URL('./template', import.meta.url).pathname
13
13
  const NAME_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
14
14
 
15
15
  /**
16
- * The newest @wervt/nuxt on npm. Releases of the module don't release this package, so new apps
17
- * ask the registry instead of keeping the version bundled at pack time (the fallback, offline).
16
+ * The newest version of a package on npm. Releases of @wervt/nuxt and @wervt/cli don't release
17
+ * this package, so new apps ask the registry instead of keeping the versions bundled at pack
18
+ * time (the fallback, offline).
18
19
  */
19
- async function latestModule() {
20
+ async function latest(name) {
20
21
  const registry = (process.env.npm_config_registry ?? 'https://registry.npmjs.org').replace(
21
22
  /\/$/,
22
23
  '',
23
24
  )
24
25
  try {
25
- const res = await fetch(`${registry}/@wervt/nuxt/latest`, { signal: AbortSignal.timeout(3000) })
26
+ const res = await fetch(`${registry}/${name}/latest`, { signal: AbortSignal.timeout(3000) })
26
27
  if (res.ok) return (await res.json()).version
27
28
  } catch {}
28
29
  return undefined
29
30
  }
30
31
 
32
+ /** The pnpm running create-wervt (`pnpm create wervt`), else the newest one. */
33
+ async function pnpmVersion() {
34
+ const agent = process.env.npm_config_user_agent?.match(/^pnpm\/(\d+\.\d+\.\d+)/)
35
+ return agent?.[1] ?? (await latest('pnpm'))
36
+ }
37
+
31
38
  /** Root of the wervt repo when running inside it, so apps are created as workspace packages. */
32
39
  function findMonorepo(from) {
33
40
  for (let dir = resolve(from); ; dir = dirname(dir)) {
@@ -88,11 +95,22 @@ const main = defineCommand({
88
95
  if (monorepo) {
89
96
  // Inside the wervt repo: use the local module, root Vite+ config and lockfile.
90
97
  pkg.dependencies['@wervt/nuxt'] = 'workspace:*'
98
+ pkg.devDependencies['@wervt/cli'] = 'workspace:*'
91
99
  delete pkg.devDependencies['vite-plus']
92
100
  await rm(join(dir, 'vite.config.ts'), { force: true })
93
- } else if (pkg.dependencies?.['@wervt/nuxt']) {
94
- const version = await latestModule()
95
- if (version) pkg.dependencies['@wervt/nuxt'] = `^${version}`
101
+ // Apps in the repo are deployed from the repo, not by their own workflow.
102
+ await rm(join(dir, '.github'), { recursive: true, force: true })
103
+ } else {
104
+ for (const [deps, name] of [
105
+ ['dependencies', '@wervt/nuxt'],
106
+ ['devDependencies', '@wervt/cli'],
107
+ ]) {
108
+ const version = pkg[deps]?.[name] && (await latest(name))
109
+ if (version) pkg[deps][name] = `^${version}`
110
+ }
111
+ // The deploy workflow's pnpm/action-setup installs this version.
112
+ const pnpm = !pkg.packageManager && (await pnpmVersion())
113
+ if (pnpm) pkg.packageManager = `pnpm@${pnpm}`
96
114
  }
97
115
  await writeFile(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`)
98
116
  spinner.stop(`Created ${relative(process.cwd(), dir) || '.'}`)
@@ -110,6 +128,7 @@ const main = defineCommand({
110
128
  !args.install && 'pnpm install',
111
129
  'pnpm dev',
112
130
  monorepo && `pnpm app:deploy ${name} # from the repo root`,
131
+ !monorepo && 'pnpm exec wervt deploy # or from CI: .github/workflows/deploy.yml',
113
132
  ]
114
133
  .filter(Boolean)
115
134
  .join('\n'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-wervt",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Create a new wervt app: Nuxt 5, Postgres with Drizzle, live data with Electric.",
5
5
  "homepage": "https://wervt.app",
6
6
  "license": "MIT",
@@ -0,0 +1,48 @@
1
+ name: deploy
2
+
3
+ # Deploys the app to wervt on every push to main, with a deploy token that can deploy this app
4
+ # and nothing else. Setup (once):
5
+ # wervt tokens <app> --create github # or Deploy tokens in the dashboard
6
+ # then in the repository settings, Secrets and variables > Actions:
7
+ # secret WERVT_TOKEN the token
8
+ # variable WERVT_URL the control plane URL
9
+ # Each release records the commit, branch, actor and this run.
10
+
11
+ on:
12
+ push:
13
+ branches: [main]
14
+ workflow_dispatch:
15
+
16
+ # One deploy at a time; a newer push waits instead of racing the running one.
17
+ concurrency:
18
+ group: deploy
19
+ cancel-in-progress: false
20
+
21
+ permissions:
22
+ contents: read
23
+
24
+ jobs:
25
+ deploy:
26
+ runs-on: ubuntu-latest
27
+ environment:
28
+ name: production
29
+ url: ${{ steps.deploy.outputs.url }}
30
+ steps:
31
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
32
+
33
+ # The pnpm version comes from packageManager in package.json.
34
+ - uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
35
+
36
+ - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
37
+ with:
38
+ node-version: 24
39
+ cache: pnpm
40
+
41
+ - run: pnpm install --frozen-lockfile
42
+
43
+ # Builds, uploads, migrates the database and activates the release.
44
+ - id: deploy
45
+ run: pnpm exec wervt deploy
46
+ env:
47
+ WERVT_URL: ${{ vars.WERVT_URL }}
48
+ WERVT_TOKEN: ${{ secrets.WERVT_TOKEN }}
@@ -99,15 +99,50 @@ const { data: todos, isLive } = await useShape<Todo>('/api/shapes/todos')
99
99
  - For your own state (anonymous visitors too), use `useSession(event, { name })` from
100
100
  `nuxt/server`. The secret is set by wervt.
101
101
 
102
+ ## Tasks (cron)
103
+
104
+ Periodic work (cleanup, syncs, renewals) goes into tasks, not into requests:
105
+
106
+ ```ts
107
+ // server/tasks/presence/sweep.ts → task "presence:sweep"
108
+ import { defineTask, useDb } from '@wervt/nuxt/server'
109
+ import { lt } from 'drizzle-orm'
110
+ import { presence } from '../../db/schema'
111
+
112
+ export default defineTask({
113
+ meta: { description: 'Remove presence older than 5 minutes' },
114
+ async run() {
115
+ await useDb()
116
+ .delete(presence)
117
+ .where(lt(presence.seenAt, new Date(Date.now() - 5 * 60_000)))
118
+ return { result: 'ok' }
119
+ },
120
+ })
121
+ ```
122
+
123
+ ```ts
124
+ // nuxt.config.ts
125
+ nitro: { scheduledTasks: { '*/5 * * * *': ['presence:sweep'] } },
126
+ wervt: { timezone: 'Europe/Berlin' }, // optional, UTC by default
127
+ ```
128
+
129
+ - wervt runs scheduled tasks; `nuxt dev` runs them locally. Import `defineTask` from
130
+ `@wervt/nuxt/server`.
131
+ - A run has the limits of a request (5 minutes, limited CPU). Keep tasks short and idempotent:
132
+ a run that's still going when the next one is due is skipped, and runs missed while wervt was
133
+ down aren't caught up.
134
+ - At most once a minute. Tasks without a schedule can still be run by hand (dashboard or
135
+ `pnpm exec wervt tasks <app> --run <name>`), e.g. for a backfill.
136
+ - Every run is logged: `pnpm exec wervt logs <app> 'type:=Task'`.
137
+
102
138
  ## Don'ts
103
139
 
104
140
  - No Node-only APIs that Deno lacks (`child_process`, native addons, writing to the filesystem).
105
141
  Store data in Postgres.
106
142
  - No long-running work in requests (CPU is limited per request) and no in-memory state that must
107
143
  survive: isolates are started and stopped on demand.
108
- - No WebSockets, SSE hubs or timers that push to clients. Write to the database and let clients
109
- read live data through shapes. No background jobs or cron either: do periodic cleanup lazily in
110
- requests.
144
+ - No WebSockets, SSE hubs or timers (`setInterval`) that push to clients. Write to the database
145
+ and let clients read live data through shapes. For periodic work, use a task (above).
111
146
  - No secrets in client code. Server env vars come from wervt; don't hardcode credentials.
112
147
 
113
148
  ## Logs
@@ -137,3 +172,6 @@ Queries are LogsQL. Logs may contain user data: quote only what's needed.
137
172
  - `pnpm dev`: dev server against the local wervt stack (`docker compose up -d` in the wervt repo).
138
173
  - `pnpm db:generate`: generate a migration from the schema.
139
174
  - `pnpm exec vp check --fix`: format, lint and type check. Run it before finishing.
175
+ - Deploys: pushing to `main` deploys through `.github/workflows/deploy.yml`. `pnpm exec wervt
176
+ releases <app>` lists releases with their commits. Don't deploy, roll back or create tokens
177
+ unless asked.
@@ -9,12 +9,13 @@
9
9
  "postinstall": "nuxt prepare"
10
10
  },
11
11
  "dependencies": {
12
- "@wervt/nuxt": "^0.2.1",
12
+ "@wervt/nuxt": "^0.2.2",
13
13
  "drizzle-orm": "1.0.0-rc.4",
14
14
  "nuxt": "npm:nuxt-nightly@5x",
15
15
  "valibot": "^1.5.0"
16
16
  },
17
17
  "devDependencies": {
18
+ "@wervt/cli": "^0.2.4",
18
19
  "drizzle-kit": "1.0.0-rc.4",
19
20
  "vite-plus": "^1.0.0"
20
21
  }