|
| 1 | +# resource-completions-cron |
| 2 | + |
| 3 | +A Cloudflare Workers scheduled job that pre-fetches auto-complete values for resource parameters in the Codify Editor. It runs daily and writes results to the `resource_parameter_completions` Supabase table. |
| 4 | + |
| 5 | +## How It Works |
| 6 | + |
| 7 | +### 1. Per-resource completion scripts |
| 8 | + |
| 9 | +Each resource that supports completions has one or more scripts co-located with it in the plugin: |
| 10 | + |
| 11 | +``` |
| 12 | +codify-homebrew-plugin/src/resources/ |
| 13 | + homebrew/completions/ |
| 14 | + homebrew.formulae.ts → resource_type=homebrew, parameter_path=/formulae |
| 15 | + homebrew.casks.ts → resource_type=homebrew, parameter_path=/casks |
| 16 | + javascript/nvm/completions/ |
| 17 | + nvm.nodeVersions.ts → resource_type=nvm, parameter_path=/nodeVersions |
| 18 | + python/pyenv/completions/ |
| 19 | + pyenv.pythonVersions.ts → resource_type=pyenv, parameter_path=/pythonVersions |
| 20 | +``` |
| 21 | + |
| 22 | +**Naming convention:** `<resource-type>.<parameter-name>.ts` |
| 23 | +- The filename determines the Supabase metadata — no configuration needed |
| 24 | +- Each file exports a single `default async function(): Promise<string[]>` that fetches and returns the completion values. It has no knowledge of Supabase. |
| 25 | + |
| 26 | +### 2. Code generation |
| 27 | + |
| 28 | +Running `npm run build:completions` (from the plugin root) executes `scripts/generate-completions-index.ts`, which: |
| 29 | +- Globs all `src/resources/**/completions/*.ts` files in the plugin |
| 30 | +- Parses each filename for `resourceType` and `parameterPath` |
| 31 | +- Writes `src/__generated__/completions-index.ts` — a static list of imports and metadata |
| 32 | + |
| 33 | +`src/__generated__/completions-index.ts` is auto-generated. **Do not edit it by hand.** |
| 34 | + |
| 35 | +### 3. Orchestrator |
| 36 | + |
| 37 | +`src/index.ts` is the Cloudflare Workers entry point. It: |
| 38 | +- Imports `completionModules` from the generated index |
| 39 | +- For each module, calls the `fetch()` function to get `string[]` values |
| 40 | +- Looks up the `resource_id` from the `registry_resources` Supabase table |
| 41 | +- Deletes old completions and batch-inserts new ones (1000 rows per batch) into `resource_parameter_completions` |
| 42 | +- Runs all modules concurrently via `Promise.allSettled` |
| 43 | + |
| 44 | +## Commands |
| 45 | + |
| 46 | +All commands are run from the **plugin root** (`codify-homebrew-plugin/`): |
| 47 | + |
| 48 | +```bash |
| 49 | +# Regenerate src/__generated__/completions-index.ts |
| 50 | +npm run build:completions |
| 51 | + |
| 52 | +# Build + deploy to Cloudflare Workers |
| 53 | +npm run deploy:completions |
| 54 | +``` |
| 55 | + |
| 56 | +To run/test locally (from this directory): |
| 57 | + |
| 58 | +```bash |
| 59 | +# Start local wrangler dev server with scheduled trigger support |
| 60 | +npm run dev |
| 61 | + |
| 62 | +# Trigger the scheduled handler manually against the local server |
| 63 | +npm run start:cron |
| 64 | +``` |
| 65 | + |
| 66 | +## Adding a New Completion |
| 67 | + |
| 68 | +1. Create `src/resources/<category>/<resource>/completions/<type>.<param>.ts` in the plugin |
| 69 | +2. Export a default async function returning `string[]` |
| 70 | +3. Run `npm run build:completions` from the plugin root to regenerate the index |
| 71 | +4. Run `npm run deploy:completions` to deploy |
| 72 | + |
| 73 | +No changes are needed to any file in this directory. |
| 74 | + |
| 75 | +## Environment Variables |
| 76 | + |
| 77 | +| Variable | Where set | |
| 78 | +|---|---| |
| 79 | +| `SUPABASE_URL` | `wrangler.toml` `[vars]` (public URL, safe to commit) | |
| 80 | +| `SUPABASE_SERVICE_ROLE_KEY` | Cloudflare Workers secret (set via `wrangler secret put`) | |
| 81 | + |
| 82 | +## Schedule |
| 83 | + |
| 84 | +The cron runs daily at 05:00 UTC (`0 5 * * *`), configured in `wrangler.toml`. |
0 commit comments