@vite-hub/codemods 0.5.1 → 0.6.0

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 CHANGED
@@ -38,7 +38,7 @@ The command exits with `0` when every codemod completes, also when they report f
38
38
  vitehub migrate [dir] [--dry-run]
39
39
  ```
40
40
 
41
- `vitehub migrate` runs the upgrade codemods, the ones that move an app from an older ViteHub API, in this order: `vitehub-options`, `define-forms`, `runtime-contracts`, `root-accessors`, `one-entry-imports`, `queue-workflow-verbs`, `queue-durations`, `result-tuples`, `agent-surface`, `driver-key`, `renamed-apis`, `tsconfig-paths`, `shell-driver`, `access-spellings`, `auth-sign-in`, `channels`, `auth-accessors`, `workspace-repo`, `kv-list-prefix`, `cli-url-flag`, `cli-yes-flag`, `cms-roles`, `choices-package`, `cli-get-list`, `cli-agent-tree`, and `nuxt-server-imports`. Each one sees the edits of the ones before it, and the report is the same as above. Install `@vite-hub/codemods` as a development dependency first. The codemods that move an app from another module, such as `nuxthub`, do not run. The report ends with a reminder to run Vite Doctor: rule `VHUB0003` finds Result calls, such as `await email.send(...)`, whose error the code drops.
41
+ `vitehub migrate` runs the upgrade codemods, the ones that move an app from an older ViteHub API, in this order: `vitehub-options`, `define-forms`, `runtime-contracts`, `root-accessors`, `one-entry-imports`, `queue-workflow-verbs`, `queue-durations`, `result-tuples`, `agent-surface`, `driver-key`, `renamed-apis`, `tsconfig-paths`, `shell-driver`, `access-spellings`, `auth-sign-in`, `channels`, `auth-accessors`, `workspace-repo`, `kv-list-prefix`, `cli-url-flag`, `cli-yes-flag`, `cms-roles`, `console-out-of-app`, `choices-package`, `cli-get-list`, `cli-agent-tree`, and `nuxt-server-imports`. Each one sees the edits of the ones before it, and the report is the same as above. Install `@vite-hub/codemods` as a development dependency first. The codemods that move an app from another module, such as `nuxthub`, do not run. The report ends with a reminder to run Vite Doctor: rule `VHUB0003` finds Result calls, such as `await email.send(...)`, whose error the code drops.
42
42
 
43
43
  ## Run one codemod
44
44
 
@@ -68,7 +68,7 @@ The command runs only the named codemod. Run it with `--help` to list the codemo
68
68
  | `renamed-apis` | An app from the ViteHub names that this release renamed, such as `hubDb` and `kv.del()` |
69
69
  | `define-forms` | The former define forms of Queue, Schedule, Sandbox Task, Workflow, Content, and Database to the one form of each |
70
70
  | `one-entry-imports` | Imports of subpaths that moved into one entry per concept, and `vite-hub/task` to `nitro/task` |
71
- | `vitehub-options` | The options that moved in `vitehub()`: `preset` to `host`, `observability.logs` to `logs`, Env declarations into `vitehub({ env })`, and `devtools` and `contentImportBase` removed |
71
+ | `vitehub-options` | The options that moved in `vitehub()`: `preset` to `host`, `observability.logs` to `logs`, Env declarations into `vitehub({ env })`, the Console journal options into `agent.invocations`, and `devtools`, `contentImportBase` and the Console options that left the app removed |
72
72
  | `tsconfig-paths` | `#vitehub/*` entries of tsconfig `paths` that point into `.vitehub/` or `.nuxt/`, which ViteHub now types itself |
73
73
  | `shell-driver` | The Shell `provider` names to `driver`: `createShellRuntime({ driver })`, `ShellDriver`, and `createJustBashDriver()`. `vitehub migrate` runs it. |
74
74
  | `access-spellings` | Realtime `auth: true` to `authorize: true`, and MCP `public: true` to `access: "public"`. `vitehub migrate` runs it. |
@@ -80,6 +80,7 @@ The command runs only the named codemod. Run it with `--help` to list the codemo
80
80
  | `cli-yes-flag` | Replace old CLI prompt flags in scripts and CI, or report a manual migration. `vitehub migrate` runs it. |
81
81
  | `cli-url-flag` | `vitehub` calls in scripts and CI: `--server` to `--url`, the app URL. `vitehub migrate` runs it. |
82
82
  | `cms-roles` | The `cms` preset option `access: "roles"` to `roles: true`. `vitehub migrate` runs it. |
83
+ | `console-out-of-app` | The `via: "page"` branch of `console.authorize` and `ConsolePageAuthorizeContext` removed, and `VITEHUB_CONSOLE_MCP_KEY` lines reported. `vitehub migrate` runs it. |
83
84
  | `choices-package` | `@vite-hub/decisions` and `vite-hub/decisions` imports and the `vitehub decisions` command to `choices`. `vitehub migrate` runs it. |
84
85
  | `cli-get-list` | `vitehub` read commands in scripts and CI: `show` and `info` to `get`, `env inspect` and `env status` to `env list`, `schedule runs` to `runs list`. `vitehub migrate` runs it. |
85
86
  | `cli-agent-tree` | `vitehub` Agent calls in scripts and CI: `agents …`, `invocations …`, and `conversations export` move under `vitehub agent`. `vitehub migrate` runs it. |
@@ -188,8 +189,10 @@ npx @vite-hub/codemods define-forms [dir] [--dry-run]
188
189
  | `vite: { env }` in `nuxt.config.ts` | `env` in the `vite-hub/nuxt` options |
189
190
  | A top-level Vite key such as `kv` or `blob` | `vitehub({ kv })`, or the argument of `hubKV()` |
190
191
  | `devtools`, `contentImportBase` | removed |
192
+ | `console: { pill }`, `console.mcpClient`, `console.embed`, `console.mcp`, `console.origins`, `console.signInPage`, `console.auth: 'token'` | removed. `console: true` when no option stays |
193
+ | `console: { databaseUrl, observations, retention }` | `agent: { invocations: { databaseUrl, observations, retention } }` |
191
194
 
192
- The report lists what the codemod cannot move: `preset` next to `host`, `sandbox` options that are not a boolean, `cloudflare.secretsFile`, an `observability` or `env` value that is an expression, `env` set in two places, `vitehub({ env: false })` next to Env declarations, and source lines that name a renamed option type, such as `KVModuleOptions`, which is now `KVOptions`.
195
+ The report lists what the codemod cannot move: `preset` next to `host`, `sandbox` options that are not a boolean, `cloudflare.secretsFile`, `console.auth: 'app'` and `console.auth: { ... }`, an `observability` or `env` value that is an expression, `env` set in two places, `vitehub({ env: false })` next to Env declarations, and source lines that name a renamed option type, such as `KVModuleOptions`, which is now `KVOptions`. The codemod leaves a Console journal option in place and reports it when the move could change the effective journal, would overwrite an existing option, or would put `databaseUrl` on an explicit store where it has no effect.
193
196
 
194
197
  Sandbox Task option objects belong in `hubSandboxTask()`. The codemod passes them to an existing standalone plugin call without arguments. When the config uses `vitehub()` or `vite-hub/nuxt`, it keeps the options in place and reports a manual follow-up because the framework accepts only boolean Sandbox Task enablement.
195
198
 
@@ -254,18 +257,40 @@ The codemod does not move local data or baseline the database. The follow-ups li
254
257
 
255
258
  ## `nuxt-safe-runtime-config`
256
259
 
260
+ The `.env.example` migration requires `vite-hub` in the range `>=0.5.1 <1`. The codemod reads a `catalog:` version from `pnpm-workspace.yaml` and an `npm:vite-hub@` alias. If the declared range allows an unsupported release, it stops without writing files. Update the range in `package.json`, then run the codemod again. If the codemod cannot read the declaration, such as `workspace:*`, `link:`, `file:`, a pkg.pr.new URL, or `latest`, it keeps it and lists a follow-up.
261
+
257
262
  The codemod reads one of the two schema sources of the module:
258
263
 
259
- - `safeRuntimeConfig.envSchema` points at a `.env.example` with `@env-spec` annotations. The codemod replaces it with `envSchema(new URL("./.env.example", import.meta.url), { prefix: "NUXT_" })` from `@vite-hub/env/vite`. With the `NUXT_` prefix, `NUXT_PUBLIC_*` keys become Public Env and other `NUXT_*` keys become Server Env, with the same names and variables. Server `paths` stay the same. Public Env is flat, so a nested public path falls back to its flat name.
264
+ - `safeRuntimeConfig.envSchema` points at a `.env.example` with `@env-spec` annotations. The codemod replaces it with `envSchema(new URL("./.env.example", import.meta.url), { prefix: "NUXT_" })` from `vite-hub/env/vite`. With the `NUXT_` prefix, `NUXT_PUBLIC_*` keys become Public Env and other `NUXT_*` keys become Server Env, with the same names and variables. Server `paths` stay the same. Public Env is flat, so a nested public path falls back to its flat name.
260
265
  - `safeRuntimeConfig.$schema` is a Valibot, Zod 3, Zod 4, or ArkType schema, inline or in a module. For Zod 4 before 4.2, the codemod calls `toJSONSchema()` from the app's own Zod. The codemod converts it to JSON Schema and writes `env()`, `env.number()`, `env.boolean()`, and `env.enum()` declarations under `vitehub.env`. Each declaration reads its `NUXT_*` variable with `env.source()`. A `process.env.NAME || fallback` value reads `NAME` with `env.source()` and keeps `fallback` as the default. Env treats an empty variable as unset, as `||` did. Literal `runtimeConfig` values, including empty strings, become defaults. The codemod checks each default against its field schema, applying the source module’s input coercion for runtime validation. A rejected value, such as `""` for `v.minLength(16)`, is dropped when validation is fatal, so Env requires its variable. Nonfatal `onError` modes do not drop a default solely because validation rejects it. The checks follow the installed module version. Before 0.4.0, the module checked only at build unless `validateAtRuntime: true`. From 0.4.0, it also checks when the server starts unless `validateAtRuntime: false`, so `validateAtBuild: false` alone still rejects. When the codemod cannot read the version, it uses the 0.4.0 rules. Keys under `runtimeConfig.public` become Public Env with `mode: "build"`. Credential-like server names become `secret: true`.
261
266
 
267
+ Env options cannot hold every schema rule, such as `v.minLength(16)`, `z.url()`, or `v.transform()`, or the shape of a JSON array. For these fields the codemod writes `server/env.schema.ts`, or `server/runtime-config.schema.ts` when the first file exists. The module exports the schema of each field: `.entries.x` for Valibot, `.shape.x` for Zod, and `.get("x")` for ArkType. It imports a `$schema` module. It moves an inline `$schema` and its imports into the module, unless the schema uses other `nuxt.config` bindings. The codemod runs each export on the loaded schema before it writes it. A server field reads its export with `env.schemaFrom()`: `env({ schema })` when the schema takes a string, and `env.json(schema)` when it takes a number, boolean, or array. A public field imports its export into `nuxt.config`, and Env runs it during the build. The codemod lists public ArkType rules as follow-ups. Env stops the app when a schema fails, so the codemod writes the module only when `nuxt-safe-runtime-config` did the same: `onError: 'throw'` with build or runtime validation on. With `onError: 'warn'` or `'ignore'`, the rules become follow-ups. Keep the `$schema` file and its schema library.
268
+
269
+ Whole-object fields such as records stay in `runtimeConfig`: one Env declaration cannot preserve nested Nuxt overrides such as `NUXT_LABELS_COLOR`. The report names the affected fields without printing credential defaults, expressions, or schema rejection details.
270
+
271
+ ```diff [nuxt.config.ts]
272
+ - runtimeConfig: { apiSecret: '', allowedOrigins: 'a,b', port: '3000' },
273
+ - safeRuntimeConfig: { $schema: './schema' },
274
+ + vitehub: {
275
+ + env: {
276
+ + server: {
277
+ + apiSecret: env({ schema: env.schemaFrom('./server/env.schema.ts', 'apiSecret'), secret: true, source: env.source('NUXT_API_SECRET') }),
278
+ + allowedOrigins: env({ default: 'a,b', schema: env.schemaFrom('./server/env.schema.ts', 'allowedOrigins'), source: env.source('NUXT_ALLOWED_ORIGINS') }),
279
+ + port: env({ default: '3000', schema: env.schemaFrom('./server/env.schema.ts', 'port'), source: env.source('NUXT_PORT') }),
280
+ + },
281
+ + },
282
+ + },
283
+ ```
284
+
262
285
  When `safeRuntimeConfig` declares neither and the app has no layers, the module validates nothing, so the codemod removes it from `modules`, `nuxt.config`, and `package.json`. It stops instead when code still calls `useSafeRuntimeConfig()`. Schema-free removal also stops when the config extends layers, uses a root spread, or has automatic `layers/` directories: inherited schemas and composable calls are unknown. Review those layers and remove the module by hand, or declare the schema in the app config before retrying.
263
286
 
264
287
  In both cases the codemod replaces `'nuxt-safe-runtime-config'` with `'vite-hub/nuxt'` in `modules`, removes `safeRuntimeConfig` and the migrated `runtimeConfig` keys, sets `vitehub.host`, and swaps the package in `package.json`. It rewrites `useSafeRuntimeConfig()` to `useServerEnv()` from `vite-hub/env` in server files and to `usePublicEnv()` from `#vitehub/env/public` in app files.
265
288
 
266
289
  An existing `modules` property must be an array literal. If it is an expression such as `buildModules()`, or a root spread could supply an inherited list, the codemod stops without writing files; make the list explicit before retrying. A dynamic `nitro.preset` also stops the codemod unless a ViteHub host is already explicit; use a string literal or set `vitehub.host` before retrying. Consumed local layers inside the app are included in call-site migration. Local layers outside the app directory are left unchanged and named in the report for manual migration before using the migrated app.
267
290
 
268
- The report lists what Env cannot express and what the codemod could not rewrite: constraints and transforms, unions, JSON arrays and objects, non-empty secret defaults that are omitted from the server build, secret reads that need `.unseal()`, reads that pass the config object on, `useRuntimeConfig()` reads of migrated keys, template reads, and ESLint rules of the module. Zod 3 input coercion is reported because Env accepts different inputs. Unsupported Zod 3 wrappers, including `.catch()` fallbacks, stay in `runtimeConfig` for manual migration. Public Env values are fixed when you build, while Nuxt applied `NUXT_PUBLIC_*` overrides when the server started.
291
+ A Nitro app without `nuxt.config` that declares `nitropack`, directly or through an npm alias, stops this codemod with `CODEMODS_NITRO_UNSUPPORTED`. Keep `nuxt-safe-runtime-config/nitro` until the app runs on Nitro 3. If the app declares both `nitro` and `nitropack`, keep only the package it uses before retrying.
292
+
293
+ The report lists what Env cannot express and what the codemod could not rewrite: rules that no schema module keeps, unions of input types, unsupported defaults, non-empty secret defaults omitted from Env declarations, secret reads that need `.unseal()`, reads that pass the config object on, `useRuntimeConfig()` reads of migrated keys, template reads, and ESLint rules of the module. Zod 3 input coercion is reported because Env accepts different inputs. Unsupported Zod 3 wrappers, including `.catch()` fallbacks, stay in `runtimeConfig` for manual migration. Public Env values are fixed when you build, while Nuxt applied `NUXT_PUBLIC_*` overrides when the server started.
269
294
 
270
295
  [`examples/nuxt-safe-runtime-config`](./examples/nuxt-safe-runtime-config) is a Nuxt app that uses the module. The end-to-end test builds and starts it, migrates a copy, type-checks and builds the copy with `vite-hub/nuxt`, and compares the typed values that one server route returns:
271
296
 
@@ -649,6 +674,26 @@ npx @vite-hub/codemods cms-roles [dir] [--dry-run]
649
674
 
650
675
  The `access` option of `defineContent()` itself, such as `access: 'public'`, stays. The report lists a preset `access` value that is not the literal `'roles'`.
651
676
 
677
+ ## `console-out-of-app`
678
+
679
+ The Console runs outside the app, so `console.authorize` no longer gets `via: "page"`. `console-out-of-app` removes the code that handled it:
680
+
681
+ ```sh
682
+ npx @vite-hub/codemods console-out-of-app [dir] [--dry-run]
683
+ ```
684
+
685
+ ```diff [server/console-authorize.ts]
686
+ import { defineConsoleAuthorize } from 'vite-hub/console/auth'
687
+ -import type { ConsolePageAuthorizeContext } from 'vite-hub/console/auth'
688
+
689
+ export default defineConsoleAuthorize(({ via, user }) => {
690
+ - if (via === 'page') return user?.role === 'admin'
691
+ return true
692
+ })
693
+ ```
694
+
695
+ It edits the function passed to `defineConsoleAuthorize()` from `vite-hub/console/auth`, and the function that `server/console-authorize.ts` default-exports. It removes an `if` on `via === 'page'` without an `else`, and the `ConsolePageAuthorizeContext` import when nothing else names it. The report lists other `'page'` checks, `ConsolePageAuthorizeContext` when the file still uses it, and each env or Wrangler line that sets `VITEHUB_CONSOLE_MCP_KEY`, without its value. The SQL that removes the old Console tables and Site access users is on [Move the Console from earlier releases](https://vitehub.dev/docs/console/migrate).
696
+
652
697
  ## Add a codemod
653
698
 
654
699
  A codemod is a `Codemod` object with a `name`, a one-line `description`, a `detect()` function that tells whether the app uses what it migrates, and a `run()` function that returns planned edits and follow-ups. `run()` must not write files; the runner writes them, or prints them in a dry run. Add the codemod to `src/registry.ts`; the order there is the order in which the migration runs codemods. A codemod that moves an app from an older ViteHub API also goes in `upgradeCodemods`, so `vitehub migrate` runs it. Shared `nuxt.config` and `package.json` helpers live in `src/shared`.