-
Notifications
You must be signed in to change notification settings - Fork 747
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for reading build time env variables from a
.env
…
…file (#882) This change will automatically load up a `.env` file, if found, and apply its values to the current environment. An example would be to provide a specific CLOUDFLARE_ACCOUNT_ID value. Related to #190
- Loading branch information
1 parent
bae5ba4
commit 1ad7570
Showing
10 changed files
with
155 additions
and
130 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,11 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
feat: add support for reading build time env variables from a `.env` file | ||
|
||
This change will automatically load up a `.env` file, if found, and apply its | ||
values to the current environment. An example would be to provide a specific | ||
CLOUDFLARE_ACCOUNT_ID value. | ||
|
||
Related to cloudflare#190 |
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 @@ | ||
FOO="the value of foo" |
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,6 @@ | ||
name = "local-mode-tests" | ||
compatibility_date = "2022-03-27" | ||
|
||
# This custom build command will show whether the FOO | ||
# environment variable was read from the `.env` file. | ||
build.command = "node -e \"console.log(process.env.FOO)\"" |
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,15 @@ | ||
import { spawnWranglerDev } from "./helpers"; | ||
|
||
it("should use the environment variable from the .env file", async () => { | ||
const { wranglerProcess, fetchWhenReady, terminateProcess } = | ||
spawnWranglerDev("src/module.ts", "src/wrangler.dotenv.toml", 9002); | ||
|
||
try { | ||
await fetchWhenReady("http://localhost"); | ||
expect(wranglerProcess.stdout?.read().toString()).toContain( | ||
"the value of foo" | ||
); | ||
} finally { | ||
await terminateProcess(); | ||
} | ||
}); |
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,77 @@ | ||
import { spawn } from "node:child_process"; | ||
import { fetch } from "undici"; | ||
import type { Response } from "undici"; | ||
|
||
const isWindows = process.platform === "win32"; | ||
|
||
export async function sleep(ms: number): Promise<void> { | ||
await new Promise((resolvePromise) => setTimeout(resolvePromise, ms)); | ||
} | ||
|
||
/** | ||
* Spawn a child process that is running `wrangler dev`. | ||
* | ||
* @returns two helper functions: | ||
* - `fetchWhenReady()` will run a fetch against the preview Worker, once it is up and running, | ||
* and return its response. | ||
* - `terminateProcess()` send a signal to the `wrangler dev` child process to kill itself. | ||
*/ | ||
export function spawnWranglerDev( | ||
srcPath: string, | ||
wranglerTomlPath: string, | ||
port: number | ||
) { | ||
const wranglerProcess = spawn( | ||
"npx", | ||
[ | ||
"wrangler", | ||
"dev", | ||
srcPath, | ||
"--local", | ||
"--config", | ||
wranglerTomlPath, | ||
"--port", | ||
port.toString(), | ||
], | ||
{ | ||
shell: isWindows, | ||
stdio: "pipe", | ||
} | ||
); | ||
|
||
const fetchWhenReady = async (url: string): Promise<Response> => { | ||
const MAX_ATTEMPTS = 50; | ||
const SLEEP_MS = 100; | ||
let attempts = MAX_ATTEMPTS; | ||
while (attempts-- > 0) { | ||
await sleep(SLEEP_MS); | ||
try { | ||
return await fetch(`${url}:${port}`); | ||
} catch {} | ||
} | ||
throw new Error( | ||
`Failed to connect to "${url}:${port}" within ${ | ||
(MAX_ATTEMPTS * SLEEP_MS) / 1000 | ||
} seconds.` | ||
); | ||
}; | ||
|
||
const terminateProcess = () => { | ||
return new Promise((resolve, reject) => { | ||
wranglerProcess.once("exit", (code) => { | ||
if (!code) { | ||
resolve(code); | ||
} else { | ||
reject(code); | ||
} | ||
}); | ||
wranglerProcess.kill(); | ||
}); | ||
}; | ||
|
||
return { | ||
wranglerProcess, | ||
fetchWhenReady, | ||
terminateProcess, | ||
}; | ||
} |
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
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