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

Add --compatibility-date, --compatibility-flags, --latest cli arguments to dev and publish #215

Merged
merged 3 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we error if compatibility-date and latest are both used?
Should we log a warning if there is a compatibility-date set in config and is being overridden by --latest?

Copy link
Contributor Author

@threepointone threepointone Jan 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I... hrmm. I'd push back against the error; the use case is the commands been generated by some other tooling, or literally just inside an npm script, and you're overriding it manually to try the latest runtime version. Would be annoying if an error blocked you then. We have the warning anyway, and it's an annoying one that you can't miss.

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