Skip to content

Commit

Permalink
feat: add experimental_readRawConfig() (#7573)
Browse files Browse the repository at this point in the history
* add experimental_readRawConfig and tests

* move printBindings out of /config/index.ts

* fixups

* changeset

* actually export
  • Loading branch information
emily-shen authored Dec 18, 2024
1 parent 5124b5d commit fb819f9
Show file tree
Hide file tree
Showing 13 changed files with 573 additions and 507 deletions.
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({
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
3 changes: 2 additions & 1 deletion packages/wrangler/src/api/startDevWorker/ConfigController.ts
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
2 changes: 2 additions & 0 deletions packages/wrangler/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ const generateASSETSBinding: (
// eslint-disable-next-line @typescript-eslint/no-var-requires
require("./miniflare-cli/assets").default;
export { generateASSETSBinding as unstable_generateASSETSBinding };

export { experimental_readRawConfig } from "./config";
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

0 comments on commit fb819f9

Please sign in to comment.