Skip to content
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

feat: provide experimental flags to the defineCommand handler #7455

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions packages/wrangler/src/core/register-yargs-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { fetchResult } from "../cfetch";
import { readConfig } from "../config";
import { defaultWranglerConfig } from "../config/config";
import { FatalError, UserError } from "../errors";
import { run } from "../experimental-flags";
import { logger } from "../logger";
import { printWranglerBanner } from "../update-check";
import { demandSingleValue } from "./helpers";
Expand Down Expand Up @@ -78,18 +79,27 @@ function createHandler(def: CommandDefinition) {
// TODO(telemetry): send command started event

await def.validateArgs?.(args);
const experimentalFlags = def.behaviour?.overrideExperimentalFlags
? def.behaviour?.overrideExperimentalFlags(args)
: {
FILE_BASED_REGISTRY: false,
MULTIWORKER: false,
RESOURCES_PROVISION: args.experimentalProvision ?? false,
};

await def.handler(args, {
config:
def.behaviour?.provideConfig ?? true
? readConfig(args.config, args, {
hideWarnings: !(def.behaviour?.printConfigWarnings ?? true),
})
: defaultWranglerConfig,
errors: { UserError, FatalError },
logger,
fetchResult,
});
await run(experimentalFlags, () =>
def.handler(args, {
config:
def.behaviour?.provideConfig ?? true
? readConfig(args.config, args, {
hideWarnings: !(def.behaviour?.printConfigWarnings ?? true),
})
: defaultWranglerConfig,
errors: { UserError, FatalError },
logger,
fetchResult,
})
);

// TODO(telemetry): send command completed event
} catch (err) {
Expand Down
10 changes: 10 additions & 0 deletions packages/wrangler/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { fetchResult } from "../cfetch";
import type { Config } from "../config";
import type { OnlyCamelCase } from "../config/config";
import type { FatalError, UserError } from "../errors";
import type { ExperimentalFlags } from "../experimental-flags";
import type { Logger } from "../logger";
import type { CommonYargsOptions, RemoveIndex } from "../yargs-types";
import type { Teams } from "./teams";
Expand Down Expand Up @@ -106,6 +107,15 @@ export type CommandDefinition<
* Set this value to `false` to skip this.
*/
provideConfig?: boolean;

/**
* By default, wrangler will provide experimental flags in the handler context,
* according to the default values in register-yargs.command.ts
* Use this to override those defaults per command.
*/
overrideExperimentalFlags?: (
args: HandlerArgs<NamedArgDefs>
) => ExperimentalFlags;
};

/**
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export function deployOptions(yargs: CommonYargsArgv) {

export type DeployArgs = StrictYargsOptionsToInterface<typeof deployOptions>;

// TODO: move this into defineCommand behaviour
export async function deployHandler(args: DeployArgs) {
await run(
{
Expand Down
14 changes: 6 additions & 8 deletions packages/wrangler/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import registerDevHotKeys from "./dev/hotkeys";
import { maybeRegisterLocalWorker } from "./dev/local";
import { UserError } from "./errors";
import { run } from "./experimental-flags";

Check failure on line 26 in packages/wrangler/src/dev.ts

View workflow job for this annotation

GitHub Actions / Checks

'run' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 26 in packages/wrangler/src/dev.ts

View workflow job for this annotation

GitHub Actions / Checks

'run' is defined but never used
import isInteractive from "./is-interactive";
import { logger } from "./logger";
import { getLegacyAssetPaths, getSiteAssetPaths } from "./sites";
Expand Down Expand Up @@ -62,6 +62,11 @@
export const dev = createCommand({
behaviour: {
provideConfig: false,
overrideExperimentalFlags: (args) => ({
FILE_BASED_REGISTRY: args.experimentalRegistry,
MULTIWORKER: Array.isArray(args.config),
RESOURCES_PROVISION: args.experimentalProvision ?? false,
}),
},
metadata: {
description: "👂 Start a local server for developing your Worker",
Expand Down Expand Up @@ -376,14 +381,7 @@
}
},
async handler(args) {
const devInstance = await run(
{
FILE_BASED_REGISTRY: args.experimentalRegistry,
MULTIWORKER: Array.isArray(args.config),
RESOURCES_PROVISION: false,
},
() => startDev(args)
);
const devInstance = await startDev(args);
assert(devInstance.devEnv !== undefined);
await events.once(devInstance.devEnv, "teardown");
await Promise.all(devInstance.secondary.map((d) => d.teardown()));
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/experimental-flags.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AsyncLocalStorage } from "async_hooks";
import { logger } from "./logger";

type ExperimentalFlags = {
export type ExperimentalFlags = {
FILE_BASED_REGISTRY: boolean;
MULTIWORKER: boolean;
RESOURCES_PROVISION: boolean;
Expand Down
Loading