Skip to content

Commit d86a157

Browse files
authored
chore: refactor defineCommand to use named imports (#7357)
* chore: convert defineCommand to createCommand * chore: convert command registry to a class * chore: add tests * chore: convert existing defineCommands to the new registry * chore: update docs and better naming of type chore: formatting
1 parent 22a4055 commit d86a157

35 files changed

+1381
-1069
lines changed

packages/wrangler/CONTRIBUTING.md

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,35 @@
1111

1212
## Defining your new command in Wrangler
1313

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".
1517

1618
```ts
17-
import { defineCommand, defineNamespace } from "./util";
19+
import { createCommand, createNamespace } from "../core/create-command";
20+
import { createKVNamespace } from "./helpers";
1821

1922
// Namespaces are the prefix before the subcommand
2023
// eg "wrangler kv" in "wrangler kv put"
2124
// eg "wrangler kv key" in "wrangler kv key put"
22-
defineNamespace({
23-
command: "wrangler kv",
25+
export const kvNamespace = createNamespace({
2426
metadata: {
2527
description: "Commands for interacting with Workers KV",
2628
status: "stable",
2729
},
2830
});
31+
2932
// Every level of namespaces must be defined
3033
// eg "wrangler kv key" in "wrangler kv key put"
31-
defineNamespace({
32-
command: "wrangler kv key",
34+
export const kvKeyNamespace = createKVNamespace({
3335
metadata: {
3436
description: "Commands for interacting with Workers KV data",
3537
status: "stable",
3638
},
3739
});
3840

3941
// 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({
4243
metadata: {
4344
description: "Put a key-value pair into a Workers KV namespace",
4445
status: "stable",
@@ -67,6 +68,22 @@ const command = defineCommand({
6768
});
6869
```
6970

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+
7087
2. Command-specific (named + positional) args vs shared args vs global args
7188

7289
- 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

Comments
 (0)