-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
wrangler pages functions build-env
(#5279)
- Loading branch information
Showing
7 changed files
with
205 additions
and
2 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,5 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
feat: Support the hidden command `wrangler pages functions build-env` |
158 changes: 158 additions & 0 deletions
158
packages/wrangler/src/__tests__/pages-build-env.test.ts
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,158 @@ | ||
/* eslint-disable turbo/no-undeclared-env-vars */ | ||
import { mockConsoleMethods } from "./helpers/mock-console"; | ||
import { runInTempDir } from "./helpers/run-in-tmp"; | ||
import { runWrangler } from "./helpers/run-wrangler"; | ||
import writeWranglerToml from "./helpers/write-wrangler-toml"; | ||
|
||
describe("pages-build-env", () => { | ||
const std = mockConsoleMethods(); | ||
runInTempDir(); | ||
const originalEnv = process.env; | ||
|
||
afterEach(() => { | ||
process.env = originalEnv; | ||
}); | ||
|
||
it("should render empty object", async () => { | ||
writeWranglerToml({ | ||
pages_build_output_dir: "./dist", | ||
vars: {}, | ||
}); | ||
await runWrangler("pages functions build-env"); | ||
expect(std).toMatchInlineSnapshot(` | ||
Object { | ||
"debug": "", | ||
"err": "", | ||
"info": "", | ||
"out": "{}", | ||
"warn": "", | ||
} | ||
`); | ||
}); | ||
|
||
it("should fail with no config file", async () => { | ||
await expect( | ||
runWrangler("pages functions build-env") | ||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`"No Pages config file found"` | ||
); | ||
}); | ||
|
||
it("should return top-level by default", async () => { | ||
writeWranglerToml({ | ||
pages_build_output_dir: "./dist", | ||
vars: { | ||
VAR1: "VALUE1", | ||
VAR2: "VALUE2", | ||
JSON: { json: true }, | ||
}, | ||
env: { | ||
production: { | ||
vars: { | ||
VAR1: "PROD_VALUE1", | ||
VAR2: "PROD_VALUE2", | ||
PROD_VAR3: "PROD_VALUE3", | ||
JSON: { json: true }, | ||
}, | ||
}, | ||
preview: { | ||
vars: { | ||
VAR1: "PREVIEW_VALUE1", | ||
VAR2: "PREVIEW_VALUE2", | ||
PREVIEW_VAR3: "PREVIEW_VALUE3", | ||
JSON: { json: true }, | ||
}, | ||
}, | ||
}, | ||
}); | ||
await runWrangler("pages functions build-env"); | ||
expect(std).toMatchInlineSnapshot(` | ||
Object { | ||
"debug": "", | ||
"err": "", | ||
"info": "", | ||
"out": "{\\"VAR1\\":\\"VALUE1\\",\\"VAR2\\":\\"VALUE2\\"}", | ||
"warn": "", | ||
} | ||
`); | ||
}); | ||
it("should return production", async () => { | ||
process.env.PAGES_ENVIRONMENT = "production"; | ||
writeWranglerToml({ | ||
pages_build_output_dir: "./dist", | ||
vars: { | ||
VAR1: "VALUE1", | ||
VAR2: "VALUE2", | ||
JSON: { json: true }, | ||
}, | ||
env: { | ||
production: { | ||
vars: { | ||
VAR1: "PROD_VALUE1", | ||
VAR2: "PROD_VALUE2", | ||
PROD_VAR3: "PROD_VALUE3", | ||
JSON: { json: true }, | ||
}, | ||
}, | ||
preview: { | ||
vars: { | ||
VAR1: "PREVIEW_VALUE1", | ||
VAR2: "PREVIEW_VALUE2", | ||
PREVIEW_VAR3: "PREVIEW_VALUE3", | ||
JSON: { json: true }, | ||
}, | ||
}, | ||
}, | ||
}); | ||
await runWrangler("pages functions build-env"); | ||
expect(std).toMatchInlineSnapshot(` | ||
Object { | ||
"debug": "", | ||
"err": "", | ||
"info": "", | ||
"out": "{\\"VAR1\\":\\"PROD_VALUE1\\",\\"VAR2\\":\\"PROD_VALUE2\\",\\"PROD_VAR3\\":\\"PROD_VALUE3\\"}", | ||
"warn": "", | ||
} | ||
`); | ||
}); | ||
|
||
it("should return preview", async () => { | ||
process.env.PAGES_ENVIRONMENT = "preview"; | ||
writeWranglerToml({ | ||
pages_build_output_dir: "./dist", | ||
vars: { | ||
VAR1: "VALUE1", | ||
VAR2: "VALUE2", | ||
JSON: { json: true }, | ||
}, | ||
env: { | ||
production: { | ||
vars: { | ||
VAR1: "PROD_VALUE1", | ||
VAR2: "PROD_VALUE2", | ||
PROD_VAR3: "PROD_VALUE3", | ||
JSON: { json: true }, | ||
}, | ||
}, | ||
preview: { | ||
vars: { | ||
VAR1: "PREVIEW_VALUE1", | ||
VAR2: "PREVIEW_VALUE2", | ||
PREVIEW_VAR3: "PREVIEW_VALUE3", | ||
JSON: { json: true }, | ||
}, | ||
}, | ||
}, | ||
}); | ||
await runWrangler("pages functions build-env"); | ||
expect(std).toMatchInlineSnapshot(` | ||
Object { | ||
"debug": "", | ||
"err": "", | ||
"info": "", | ||
"out": "{\\"VAR1\\":\\"PREVIEW_VALUE1\\",\\"VAR2\\":\\"PREVIEW_VALUE2\\",\\"PREVIEW_VAR3\\":\\"PREVIEW_VALUE3\\"}", | ||
"warn": "", | ||
} | ||
`); | ||
}); | ||
}); |
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
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,31 @@ | ||
import { readConfig } from "../config"; | ||
import { FatalError } from "../errors"; | ||
import { logger } from "../logger"; | ||
import type { | ||
CommonYargsArgv, | ||
StrictYargsOptionsToInterface, | ||
} from "../yargs-types"; | ||
|
||
export type PagesBuildEnvArgs = StrictYargsOptionsToInterface<typeof Options>; | ||
|
||
export function Options(yargs: CommonYargsArgv) { | ||
return yargs; | ||
} | ||
|
||
export const Handler = async (args: PagesBuildEnvArgs) => { | ||
const config = readConfig(undefined, { | ||
...args, | ||
// eslint-disable-next-line turbo/no-undeclared-env-vars | ||
env: process.env.PAGES_ENVIRONMENT, | ||
}); | ||
if (!config.pages_build_output_dir) { | ||
throw new FatalError("No Pages config file found"); | ||
} | ||
|
||
// Ensure JSON variables are not included | ||
const textVars = Object.fromEntries( | ||
Object.entries(config.vars).filter(([_, v]) => typeof v === "string") | ||
); | ||
|
||
logger.log(JSON.stringify(textVars)); | ||
}; |
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