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

feat: add experimental_readRawConfig() #7573

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 7 additions & 0 deletions .changeset/blue-laws-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

feat: add experimental_readRawConfig()

Adds a Wrangler API to find and read a config file
2 changes: 1 addition & 1 deletion packages/wrangler/docs/how-to/add-a-binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- `CfWorkerInit` in: `packages/wrangler/src/deployment-bundle/worker.ts` [ref](https://github.com/cloudflare/workers-sdk/blob/ce7db9d9cb4f5bcd5a326b86dde051cb54b999fb/packages/wrangler/src/deployment-bundle/worker.ts#L79C1-L85C2)
- `WorkerMetadataBinding` in: `packages/wrangler/src/deployment-bundle/create-worker-upload-form.ts` [ref-1](https://github.com/cloudflare/workers-sdk/blob/ce7db9d9cb4f5bcd5a326b86dde051cb54b999fb/packages/wrangler/src/deployment-bundle/create-worker-upload-form.ts#L65) [ref-2](https://github.com/cloudflare/workers-sdk/blob/ce7db9d9cb4f5bcd5a326b86dde051cb54b999fb/packages/wrangler/src/deployment-bundle/create-worker-upload-form.ts#L219-L225)
1. Add type to DevEnv `Binding` union in: `packages/wrangler/src/api/startDevWorker/types.ts` [ref](https://github.com/cloudflare/workers-sdk/blob/ce7db9d9cb4f5bcd5a326b86dde051cb54b999fb/packages/wrangler/src/api/startDevWorker/types.ts#L246)
1. Add user-friendly output for `printBindings` in: `packages/wrangler/src/config/index.ts` [ref](https://github.com/cloudflare/workers-sdk/blob/ce7db9d9cb4f5bcd5a326b86dde051cb54b999fb/packages/wrangler/src/config/index.ts#L270-L280)
1. Add user-friendly output for `printBindings` in: `packages/wrangler/src/utils/print-bindings.ts` [ref](https://github.com/cloudflare/workers-sdk/blob/ce7db9d9cb4f5bcd5a326b86dde051cb54b999fb/packages/wrangler/src/utils/print-bindings.ts)
1. Add mapping functions to:
- `createWorkerUploadForm` in: `packages/wrangler/src/deployment-bundle/create-worker-upload-form.ts` [ref](https://github.com/cloudflare/workers-sdk/blob/ce7db9d9cb4f5bcd5a326b86dde051cb54b999fb/packages/wrangler/src/deployment-bundle/create-worker-upload-form.ts#L219-L225)
- `convertCfWorkerInitBindingstoBindings` in: `packages/wrangler/src/api/startDevWorker/utils.ts` [ref](https://github.com/cloudflare/workers-sdk/blob/ce7db9d9cb4f5bcd5a326b86dde051cb54b999fb/packages/wrangler/src/api/startDevWorker/utils.ts#L118-L123)
Expand Down
54 changes: 53 additions & 1 deletion packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "fs";
import path from "node:path";
import { readConfig } from "../config";
import { experimental_readRawConfig, readConfig } from "../config";
import { normalizeAndValidateConfig } from "../config/validation";
import { run } from "../experimental-flags";
import { normalizeString } from "./helpers/normalize";
Expand Down Expand Up @@ -6037,6 +6038,57 @@ describe("normalizeAndValidateConfig()", () => {
});
});

describe("experimental_readRawConfig()", () => {
describe.each(["json", "jsonc", "toml"])(
`with %s config files`,
(configType) => {
runInTempDir();
it(`should find a ${configType} config file given a specific path`, () => {
fs.mkdirSync("../folder", { recursive: true });
writeWranglerConfig({}, `../folder/config.${configType}`);

const result = experimental_readRawConfig({
config: `../folder/config.${configType}`,
});
expect(result.rawConfig).toEqual({
compatibility_date: "2022-01-12",
name: "test-name",
});
});

it("should find a config file given a specific script", () => {
fs.mkdirSync("./path/to", { recursive: true });
writeWranglerConfig(
{ name: "config-one" },
`./path/wrangler.${configType}`
);

fs.mkdirSync("../folder", { recursive: true });
writeWranglerConfig(
{ name: "config-two" },
`../folder/wrangler.${configType}`
);

let result = experimental_readRawConfig({
script: "./path/to/index.js",
});
expect(result.rawConfig).toEqual({
Copy link
Contributor

@vicb vicb Dec 17, 2024

Choose a reason for hiding this comment

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

nit: should this test result.rawConfig.name only? (the date is not specified in the config, same on l 6083 and l 6054

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the writeWranglerConfig helper provides the default name (test-name) and compat date so I think this is okay :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, that's only a nit (= minor comment).

FYI my thinking is that writeWranglerConfig is already tested here and this test would break if it changes behavior while this test is for experimental_readRawConfig() (l6041).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fair enough :) will fix it in a followup PR!

compatibility_date: "2022-01-12",
name: "config-one",
});

result = experimental_readRawConfig({
script: "../folder/index.js",
});
expect(result.rawConfig).toEqual({
compatibility_date: "2022-01-12",
name: "config-two",
});
});
}
);
});

function normalizePath(text: string): string {
return text
.replace("project\\wrangler.toml", "project/wrangler.toml")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isLegacyEnv,
} from "../..";
import { getAssetsOptions, validateAssetsArgsAndConfig } from "../../assets";
import { printBindings, readConfig } from "../../config";
import { readConfig } from "../../config";
import { getEntry } from "../../deployment-bundle/entry";
import {
getBindings,
Expand All @@ -24,6 +24,7 @@ import { UserError } from "../../errors";
import { logger } from "../../logger";
import { requireApiToken, requireAuth } from "../../user";
import { memoizeGetPort } from "../../utils/memoizeGetPort";
import { printBindings } from "../../utils/print-bindings";
import { getZoneIdForPreview } from "../../zones";
import { Controller } from "./BaseController";
import { castErrorCause } from "./events";
Expand Down
1 change: 0 additions & 1 deletion packages/wrangler/src/config/config-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export function resolveWranglerConfigPath({
}

const leafPath = script !== undefined ? path.dirname(script) : process.cwd();

return findWranglerConfig(leafPath);
}

Expand Down
Loading
Loading