From 41d4c3e0ae24f3edbe1ee510ec817f6aca528e6e Mon Sep 17 00:00:00 2001 From: Sunil Pai Date: Tue, 11 Jan 2022 14:52:23 +0000 Subject: [PATCH] Add `--compatibility-date`, `--compatibility-flags`, `--latest` cli arguments to `dev` and `publish` (#215) * Add `--compatibility-date`, `--compatibility-flags`, `--latest` cli arguments to `dev` and `publish` Closes https://github.com/cloudflare/wrangler2/issues/193. This PR adds cli fields for 2 existing configuration fields: `--compatibility-date`, `--compatibility-flags` matching `compatibility_date` and `compatibility_flags` respectively. It also adds a cli arg `--latest` which is shorthand for `--compatibility_date `. It follows the rules from the linked issue - - A cli arg for adding a compatibility data, e.g `--compatibility_date 2022-01-05` - A shorthand `--latest` that sets `compatibility_date` to today's date. Usage of this flag logs a warning. - `latest` is NOT a config field in `wrangler.toml`. - In `dev`, when a compatibility date is not available in either `wrangler.toml` or as a cli arg, then we default to `--latest`, and log a warning. - In `publish` we error if a compatibility date is not available in either `wrangler.toml` or as a cli arg. Usage of `--latest` logs a warning. - We also accept compatibility flags via the cli, e.g: `--compatibility-flags formdata_parser_supports_files` I haven't added validation for the actual values of these args, I'll get to that next week when I work on the config validation work. The wording of the messages/errors are also up for discussion. I'm happy to keep this PR alive until we get it right. * remove the warning when using `--latest` for `wrangler dev`. * update changeset with the change to not log a warning during dev + latest. --- .changeset/pretty-starfishes-judge.md | 12 +++++++ packages/wrangler/src/index.tsx | 51 +++++++++++++++++++++++++-- packages/wrangler/src/publish.ts | 10 +++++- 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 .changeset/pretty-starfishes-judge.md diff --git a/.changeset/pretty-starfishes-judge.md b/.changeset/pretty-starfishes-judge.md new file mode 100644 index 000000000000..7cc03cd3daee --- /dev/null +++ b/.changeset/pretty-starfishes-judge.md @@ -0,0 +1,12 @@ +--- +"wrangler": patch +--- + +Add `--compatibility-date`, `--compatibility-flags`, `--latest` cli arguments to `dev` and `publish`. + +- A cli arg for adding a compatibility data, e.g `--compatibility_date 2022-01-05` +- A shorthand `--latest` that sets `compatibility_date` to today's date. Usage of this flag logs a warning. +- `latest` is NOT a config field in `wrangler.toml`. +- In `dev`, when a compatibility date is not available in either `wrangler.toml` or as a cli arg, then we default to `--latest`. +- In `publish` we error if a compatibility date is not available in either `wrangler.toml` or as a cli arg. Usage of `--latest` logs a warning. +- We also accept compatibility flags via the cli, e.g: `--compatibility-flags formdata_parser_supports_files` diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index 29afa023a139..b5b1f1af2340 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -435,6 +435,20 @@ export async function main(argv: string[]): Promise { type: "string", // TODO: get choices for the toml file? }) + .option("compatibility-date", { + describe: "Date to use for compatibility checks", + type: "string", + }) + .option("compatibility-flags", { + describe: "Flags to use for compatibility checks", + type: "array", + alias: "compatibility-flag", + }) + .option("latest", { + describe: "Use the latest version of the worker runtime", + type: "boolean", + default: true, + }) .option("ip", { describe: "IP address to listen on", type: "string", @@ -520,7 +534,7 @@ export async function main(argv: string[]): Promise { if ("durable_objects" in envRootObj) { if (!(args.name || config.name)) { console.warn( - 'A worker with durable objects need to be named, or it may not work as expected. Add a "name" into wrangler.toml, or pass it in the command line with --name.' + 'A worker with durable objects needs to be named, or it may not work as expected. Add a "name" into wrangler.toml, or pass it in the command line with --name.' ); } // TODO: if not already published, publish a draft worker @@ -539,8 +553,15 @@ export async function main(argv: string[]): Promise { site={args.site || config.site?.bucket} port={args.port || config.dev?.port} public={args["experimental-public"]} - compatibilityDate={config.compatibility_date} - compatibilityFlags={config.compatibility_flags} + compatibilityDate={ + args["compatibility-date"] || + config.compatibility_date || + new Date().toISOString().substring(0, 10) + } + compatibilityFlags={ + (args["compatibility-flags"] as string[]) || + config.compatibility_flags + } usageModel={config.usage_model} bindings={{ kv_namespaces: envRootObj.kv_namespaces?.map( @@ -592,6 +613,20 @@ export async function main(argv: string[]): Promise { describe: "name to use when uploading", type: "string", }) + .option("compatibility-date", { + describe: "Date to use for compatibility checks", + type: "string", + }) + .option("compatibility-flags", { + describe: "Flags to use for compatibility checks", + type: "array", + alias: "compatibility-flag", + }) + .option("latest", { + describe: "Use the latest version of the worker runtime", + type: "boolean", + default: false, + }) .option("experimental-public", { describe: "Static assets to be served", type: "string", @@ -645,6 +680,12 @@ export async function main(argv: string[]): Promise { const config = args.config as Config; + if (args.latest) { + console.warn( + "⚠️ Using the latest version of the Workers runtime. To silence this warning, please choose a specific version of the runtime with --compatibility-date, or add a compatibility_date to your wrangler.toml.\n" + ); + } + // -- snip, extract -- if (!args.local) { const loggedIn = await loginOrRefreshIfRequired(); @@ -668,6 +709,10 @@ export async function main(argv: string[]): Promise { name: args.name, script: args.script, env: args.env, + compatibilityDate: args.latest + ? new Date().toISOString().substring(0, 10) + : args["compatibility-date"], + compatibilityFlags: args["compatibility-flags"] as string[], triggers: args.triggers, jsxFactory: args["jsx-factory"], jsxFragment: args["jsx-fragment"], diff --git a/packages/wrangler/src/publish.ts b/packages/wrangler/src/publish.ts index 9f8647becaf5..bbb6ef3dfa26 100644 --- a/packages/wrangler/src/publish.ts +++ b/packages/wrangler/src/publish.ts @@ -19,6 +19,8 @@ type Props = { script?: string; name?: string; env?: string; + compatibilityDate?: string; + compatibilityFlags?: string[]; public?: string; site?: string; triggers?: (string | number)[]; @@ -48,6 +50,13 @@ export default async function publish(props: Props): Promise { __path__, } = config; + const envRootObj = props.env ? config.env[props.env] || {} : config; + + assert( + envRootObj.compatibility_date || props["compatibility-date"], + "A compatibility_date is required when publishing. Add one to your wrangler.toml file, or pass it in your terminal as --compatibility_date. See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information." + ); + const triggers = props.triggers || config.triggers?.crons; const routes = props.routes || config.routes; @@ -200,7 +209,6 @@ export default async function publish(props: Props): Promise { ) : { manifest: undefined, namespace: undefined }; - const envRootObj = props.env ? config.env[props.env] || {} : config; const bindings: CfWorkerInit["bindings"] = { kv_namespaces: envRootObj.kv_namespaces?.concat( assets.namespace