forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read
vars
overrides from a local file for wrangler dev
The `vars` bindings can be specified in the `wrangler.toml` configuration file. But "secret" `vars` are usually only provided at the server - either by creating them in the Dashboard UI, or using the `wrangler secret` command. It is useful during development, to provide these types of variable locally. When running `wrangler dev` we will look for a file called `.dev.vars`, situated next to the `wrangler.toml` file (or in the current working directory if there is no `wrangler.toml`). Any values in this file, formatted like a `dotenv` file, will add to or override `vars` bindings provided in the `wrangler.toml`. Related to cloudflare#190
- Loading branch information
1 parent
2974d5a
commit 8f74ec9
Showing
6 changed files
with
128 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
feat: read `vars` overrides from a local file for `wrangler dev` | ||
|
||
The `vars` bindings can be specified in the `wrangler.toml` configuration file. | ||
But "secret" `vars` are usually only provided at the server - | ||
either by creating them in the Dashboard UI, or using the `wrangler secret` command. | ||
|
||
It is useful during development, to provide these types of variable locally. | ||
When running `wrangler dev` we will look for a file called `.dev.vars`, situated | ||
next to the `wrangler.toml` file (or in the current working directory if there is no | ||
`wrangler.toml`). Any values in this file, formatted like a `dotenv` file, will add to | ||
or override `vars` bindings provided in the `wrangler.toml`. | ||
|
||
Related to #190 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as fs from "node:fs"; | ||
import * as path from "node:path"; | ||
import dotenv from "dotenv"; | ||
import { logger } from "../logger"; | ||
import type { Config } from "../config"; | ||
|
||
/** | ||
* Get the Worker `vars` bindings for a `wrangler dev` instance of a Worker. | ||
* | ||
* The `vars` bindings can be specified in the `wrangler.toml` configuration file. | ||
* But "secret" `vars` are usually only provided at the server - | ||
* either by creating them in the Dashboard UI, or using the `wrangler secret` command. | ||
* | ||
* It is useful during development, to provide these types of variable locally. | ||
* When running `wrangler dev` we will look for a file called `.dev.vars`, situated | ||
* next to the `wrangler.toml` file (or in the current working directory if there is no | ||
* `wrangler.toml`). | ||
* | ||
* Any values in this file, formatted like a `dotenv` file, will add to or override `vars` | ||
* bindings provided in the `wrangler.toml`. | ||
*/ | ||
export function getVarsForDev(config: Config): Config["vars"] { | ||
const configDir = path.resolve(path.dirname(config.configPath ?? ".")); | ||
const devVarsPath = path.resolve(configDir, ".dev.vars"); | ||
if (fs.existsSync(devVarsPath)) { | ||
const devVarsRelativePath = path.relative(process.cwd(), devVarsPath); | ||
logger.log( | ||
`Add vars defined in "${devVarsRelativePath}" to the "vars" bindings.` | ||
); | ||
const devVars = dotenv.parse(fs.readFileSync(devVarsPath, "utf8")); | ||
return { | ||
...config.vars, | ||
...devVars, | ||
}; | ||
} else { | ||
return config.vars; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters