-
Notifications
You must be signed in to change notification settings - Fork 690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
defineCommand util #6526
base: main
Are you sure you want to change the base?
defineCommand util #6526
Conversation
|
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11123028091/npm-package-wrangler-6526 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/6526/npm-package-wrangler-6526 Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11123028091/npm-package-wrangler-6526 dev path/to/script.js Additional artifacts:npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11123028091/npm-package-create-cloudflare-6526 --no-auto-update npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11123028091/npm-package-cloudflare-kv-asset-handler-6526 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11123028091/npm-package-miniflare-6526 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11123028091/npm-package-cloudflare-pages-shared-6526 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11123028091/npm-package-cloudflare-vitest-pool-workers-6526 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11123028091/npm-package-cloudflare-workers-editor-shared-6526 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11123028091/npm-package-cloudflare-workers-shared-6526 Note that these links will no longer work once the GitHub Actions artifact expires.
Please ensure constraints are pinned, and |
68ce564
to
1103b89
Compare
packages/wrangler/src/__tests__/core/command-registration.test.ts
Outdated
Show resolved
Hide resolved
8209c19
to
c17ca68
Compare
958415a
to
348152c
Compare
|
||
statusMessage = | ||
def.metadata.statusMessage ?? | ||
`🚧 \`${def.command}\` is a ${def.metadata.status} command. Please report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, but a
won't always be appropriate here (e.g. is a alpha command
)
b730836
to
67876f5
Compare
wrangler.command("kv", `🗂️ Manage Workers KV Namespaces`, (kvYargs) => { | ||
return registerKvSubcommands(kvYargs, subHelp); | ||
}); | ||
register.registerNamespace("kv"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be better named commands
?:
commands.registerNamespace("kv");
is more descriptive.
export const COMMAND_DEFINITIONS: Array< | ||
CommandDefinition | NamespaceDefinition | AliasDefinition | ||
> = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering this too. What if they're used in multiple places, like in separate isolated packages?
To avoid any potential issues with shared state, and to make it more portable, we could create an index file here and export functions with closed state:
export const { defineCommand, defineNamespace, defineAlias } = createCommandDefineFns(); // terrible name...
This could then be created in each package where we want to isolate this behaviour, or could be used in the main wrangler package just as we're doing here in kv
.
implement subhelp
0bf9160
to
6112fe4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great work. I love the way that commands are searchable by their string value, and the args validation is extracted from the handler.
}); | ||
``` | ||
|
||
2. Command specific (named positional) args vs Shared args vs Global args |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2. Command specific (named positional) args vs Shared args vs Global args | |
2. Command-specific (named positional) args vs shared args vs global args |
What this PR solves / how to test
Fixes DEVX-1377 DEVX-1367
This PR introduces a thin abstraction around yargs to improve wrangler's CLI command registration with 3 utilities:
defineCommand
: define an absolute command (wrangler [..namespace] subcommand
) – satisfying the typescript compiler will ensure all necessary info is provided (description, status, owner, args, handler, etc...)defineNamespace
: define an absolute command namespace (wrangler <..namespace>
) – satisfying the typescript compiler will ensure all necessary info is provided (description, status, owner, etc...)defineAlias
: define an alias from any absolute command to any other absolute command. Optionally, override metadata (eg description, status, deprecated, hidden, etc...).Implementation details:
These 3 core utils are in
src/core/define-commands
. They simply push the definition into a flat array of all definitions.In
src/core/register-commands
, a tree is built from the flat list and is validated, and exposes utils to register commands with yargs:register.registerAllCommands
,register.registerNamespace
(the latter of which is needed to register commands in the same order as the existing implementation).Before registering the commands, the definitions are wrapped with
src/core/wrap-commands
to provide centralised behaviour and transforms the definition into something more friendly to pass to yargs.Addressed:
Wins:
Author has addressed the following