Skip to content

Commit

Permalink
Support wrangler pages functions build-env (#5279)
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed Mar 18, 2024
1 parent e1f2576 commit 0a86050
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-teachers-poke.md
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 packages/wrangler/src/__tests__/pages-build-env.test.ts
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": "",
}
`);
});
});
2 changes: 1 addition & 1 deletion packages/wrangler/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export const defaultWranglerConfig: Config = {
},
usage_model: undefined,
rules: [],
build: {},
build: { command: undefined, watch_dir: "./src", cwd: undefined },
no_bundle: undefined,
minify: undefined,
node_compat: undefined,
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function readConfig<CommandArgs>(
// If we detected a Pages project, run config file validation against
// Pages specific validation rules
if (isPagesConfig) {
logger.info(
logger.debug(
`Configuration file belonging to ⚡️ Pages ⚡️ project detected.`
);

Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/src/config/validation-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const supportedPagesConfigFields = [
"analytics_engine_datasets",
"ai",
"dev",
// normalizeAndValidateConfig() sets this value
"configPath",
] as const;

export function validatePagesConfig(
Expand Down
31 changes: 31 additions & 0 deletions packages/wrangler/src/pages/build-env.ts
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));
};
7 changes: 7 additions & 0 deletions packages/wrangler/src/pages/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-shadow */

import * as Build from "./build";
import * as BuildEnv from "./build-env";
import * as Deploy from "./deploy";
import * as DeploymentTails from "./deployment-tails";
import * as Deployments from "./deployments";
Expand Down Expand Up @@ -42,6 +43,12 @@ export function pages(yargs: CommonYargsArgv) {
Build.Options,
Build.Handler
)
.command(
"build-env",
"Render a list of environment variables from the config file",
BuildEnv.Options,
BuildEnv.Handler
)
.command(
"optimize-routes [routesPath] [outputRoutesPath]",
"Consolidate and optimize the route paths declared in _routes.json",
Expand Down

0 comments on commit 0a86050

Please sign in to comment.