|
11 | 11 |
|
12 | 12 | ## Defining your new command in Wrangler |
13 | 13 |
|
14 | | -1. Define the command structure with the utils `defineNamespace()` & `defineCommand()` |
| 14 | +1. Create the command structure and define it in the registry. |
| 15 | + |
| 16 | +First, create your namespaces and commands with the `createNamespace` and `createCommand` utilities. Namespaces are the prefix before the subcommand, eg. "wrangler kv" in "wrangler kv put". |
15 | 17 |
|
16 | 18 | ```ts |
17 | | -import { defineCommand, defineNamespace } from "./util"; |
| 19 | +import { createCommand, createNamespace } from "../core/create-command"; |
| 20 | +import { createKVNamespace } from "./helpers"; |
18 | 21 |
|
19 | 22 | // Namespaces are the prefix before the subcommand |
20 | 23 | // eg "wrangler kv" in "wrangler kv put" |
21 | 24 | // eg "wrangler kv key" in "wrangler kv key put" |
22 | | -defineNamespace({ |
23 | | - command: "wrangler kv", |
| 25 | +export const kvNamespace = createNamespace({ |
24 | 26 | metadata: { |
25 | 27 | description: "Commands for interacting with Workers KV", |
26 | 28 | status: "stable", |
27 | 29 | }, |
28 | 30 | }); |
| 31 | + |
29 | 32 | // Every level of namespaces must be defined |
30 | 33 | // eg "wrangler kv key" in "wrangler kv key put" |
31 | | -defineNamespace({ |
32 | | - command: "wrangler kv key", |
| 34 | +export const kvKeyNamespace = createKVNamespace({ |
33 | 35 | metadata: { |
34 | 36 | description: "Commands for interacting with Workers KV data", |
35 | 37 | status: "stable", |
36 | 38 | }, |
37 | 39 | }); |
38 | 40 |
|
39 | 41 | // Define the command args, implementation and metadata |
40 | | -const command = defineCommand({ |
41 | | - command: "wrangler kv key put", // the full command including the namespace |
| 42 | +export const kvKeyPutCommand = createCommand({ |
42 | 43 | metadata: { |
43 | 44 | description: "Put a key-value pair into a Workers KV namespace", |
44 | 45 | status: "stable", |
@@ -67,6 +68,22 @@ const command = defineCommand({ |
67 | 68 | }); |
68 | 69 | ``` |
69 | 70 |
|
| 71 | +Define your commands in the registry |
| 72 | + |
| 73 | +```ts |
| 74 | +import { kvKeyNamespace, kvKeyPutCommand, kvNamespace } from "./kv"; |
| 75 | + |
| 76 | +// ... |
| 77 | + |
| 78 | +registry.define([ |
| 79 | + { command: "wrangler kv", definition: kvNamespace }, |
| 80 | + { command: "wrangler kv key", definition: kvKeyNamespace }, |
| 81 | + { command: "wrangler kv key put", definition: kvKeyPutCommand }, |
| 82 | + // ...other kv commands here |
| 83 | +]); |
| 84 | +registry.registerNamespace("kv"); |
| 85 | +``` |
| 86 | + |
70 | 87 | 2. Command-specific (named + positional) args vs shared args vs global args |
71 | 88 |
|
72 | 89 | - Command-specific args are defined in the `args` field of the command definition. Command handlers receive these as a typed object automatically. To make any of these positional, add the key to the `positionalArgs` array. |
|
0 commit comments