Skip to content

Commit

Permalink
Add --compatibility-date, --compatibility-flags, --latest cli a…
Browse files Browse the repository at this point in the history
…rguments to `dev` and `publish` (#215)

* Add `--compatibility-date`, `--compatibility-flags`, `--latest` cli arguments to `dev` and `publish`

Closes #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 <today>`. 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.
  • Loading branch information
threepointone authored Jan 11, 2022
1 parent 8ff5537 commit 41d4c3e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
12 changes: 12 additions & 0 deletions .changeset/pretty-starfishes-judge.md
Original file line number Diff line number Diff line change
@@ -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`
51 changes: 48 additions & 3 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,20 @@ export async function main(argv: string[]): Promise<void> {
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",
Expand Down Expand Up @@ -520,7 +534,7 @@ export async function main(argv: string[]): Promise<void> {
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
Expand All @@ -539,8 +553,15 @@ export async function main(argv: string[]): Promise<void> {
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(
Expand Down Expand Up @@ -592,6 +613,20 @@ export async function main(argv: string[]): Promise<void> {
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",
Expand Down Expand Up @@ -645,6 +680,12 @@ export async function main(argv: string[]): Promise<void> {

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();
Expand All @@ -668,6 +709,10 @@ export async function main(argv: string[]): Promise<void> {
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"],
Expand Down
10 changes: 9 additions & 1 deletion packages/wrangler/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Props = {
script?: string;
name?: string;
env?: string;
compatibilityDate?: string;
compatibilityFlags?: string[];
public?: string;
site?: string;
triggers?: (string | number)[];
Expand Down Expand Up @@ -48,6 +50,13 @@ export default async function publish(props: Props): Promise<void> {
__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;

Expand Down Expand Up @@ -200,7 +209,6 @@ export default async function publish(props: Props): Promise<void> {
)
: { 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
Expand Down

0 comments on commit 41d4c3e

Please sign in to comment.