From a8716a32ff6f23191a9945f233ab82c6ea46a151 Mon Sep 17 00:00:00 2001 From: Carmen Popoviciu Date: Wed, 17 Jul 2024 14:36:10 +0200 Subject: [PATCH] feat(wrangler): Add support for wrangler.jsonc This commit adds support for `wrangler.jsonc` config file, behind the same `--experimental-json-config` flag as `wrangler.json` support. --- .changeset/rare-forks-fly.md | 11 + .../wrangler/src/__tests__/d1/execute.test.ts | 2 +- .../wrangler/src/__tests__/d1/export.test.ts | 2 +- .../wrangler/src/__tests__/d1/info.test.ts | 2 +- .../wrangler/src/__tests__/d1/migrate.test.ts | 2 +- .../src/__tests__/d1/timeTravel.test.ts | 2 +- .../wrangler/src/__tests__/delete.test.ts | 2 +- .../wrangler/src/__tests__/deploy.test.ts | 285 ++++++++++++------ .../src/__tests__/deployments.test.ts | 2 +- .../__tests__/deprecated-usage-model.test.ts | 2 +- packages/wrangler/src/__tests__/dev.test.tsx | 53 +++- .../__tests__/helpers/write-wrangler-toml.ts | 17 +- packages/wrangler/src/__tests__/index.test.ts | 2 +- .../__tests__/pages/pages-build-env.test.ts | 2 +- .../pages/pages-download-config.test.ts | 2 +- .../deployments/deployments.list.test.ts | 2 +- .../deployments/deployments.status.test.ts | 2 +- .../__tests__/versions/secrets/delete.test.ts | 2 +- .../__tests__/versions/secrets/list.test.ts | 2 +- .../__tests__/versions/secrets/put.test.ts | 2 +- .../versions/versions.deploy.test.ts | 2 +- .../__tests__/versions/versions.list.test.ts | 2 +- .../__tests__/versions/versions.view.test.ts | 2 +- .../src/api/integrations/platform/index.ts | 2 +- packages/wrangler/src/config/index.ts | 10 +- 25 files changed, 292 insertions(+), 124 deletions(-) create mode 100644 .changeset/rare-forks-fly.md diff --git a/.changeset/rare-forks-fly.md b/.changeset/rare-forks-fly.md new file mode 100644 index 000000000000..58e8ef46c8cd --- /dev/null +++ b/.changeset/rare-forks-fly.md @@ -0,0 +1,11 @@ +--- +"wrangler": minor +--- + +feat: Add support for `wrangler.jsonc` + +This commit adds support for `wrangler.jsonc` config file for Workers. This feature is available behind the `--experimental-json-config` flag (just like `wrangler.json`). + +To use the new configuration file, add a `wrangler.jsonc` file to your Worker project and run `wrangler dev --experimental-json-config` or `wrangler deploy --experimental-json-config`. + +Please note that this work does NOT add `wrangler.json` or `wrangler.jsonc` support for Pages projects! diff --git a/packages/wrangler/src/__tests__/d1/execute.test.ts b/packages/wrangler/src/__tests__/d1/execute.test.ts index 48284561a074..b3acb161b594 100644 --- a/packages/wrangler/src/__tests__/d1/execute.test.ts +++ b/packages/wrangler/src/__tests__/d1/execute.test.ts @@ -4,7 +4,7 @@ import { mockConsoleMethods } from "../helpers/mock-console"; import { useMockIsTTY } from "../helpers/mock-istty"; import { runInTempDir } from "../helpers/run-in-tmp"; import { runWrangler } from "../helpers/run-wrangler"; -import writeWranglerToml from "../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../helpers/write-wrangler-toml"; describe("execute", () => { const std = mockConsoleMethods(); diff --git a/packages/wrangler/src/__tests__/d1/export.test.ts b/packages/wrangler/src/__tests__/d1/export.test.ts index eb43abf01813..b5c9901c1e09 100644 --- a/packages/wrangler/src/__tests__/d1/export.test.ts +++ b/packages/wrangler/src/__tests__/d1/export.test.ts @@ -7,7 +7,7 @@ import { mockGetMemberships } from "../helpers/mock-oauth-flow"; import { msw } from "../helpers/msw"; import { runInTempDir } from "../helpers/run-in-tmp"; import { runWrangler } from "../helpers/run-wrangler"; -import writeWranglerToml from "../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../helpers/write-wrangler-toml"; describe("execute", () => { mockAccountId({ accountId: null }); diff --git a/packages/wrangler/src/__tests__/d1/info.test.ts b/packages/wrangler/src/__tests__/d1/info.test.ts index 6cd52b48d4f4..e96ff26d080c 100644 --- a/packages/wrangler/src/__tests__/d1/info.test.ts +++ b/packages/wrangler/src/__tests__/d1/info.test.ts @@ -6,7 +6,7 @@ import { mockGetMemberships } from "../helpers/mock-oauth-flow"; import { msw } from "../helpers/msw"; import { runInTempDir } from "../helpers/run-in-tmp"; import { runWrangler } from "../helpers/run-wrangler"; -import writeWranglerToml from "../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../helpers/write-wrangler-toml"; describe("info", () => { mockAccountId({ accountId: null }); diff --git a/packages/wrangler/src/__tests__/d1/migrate.test.ts b/packages/wrangler/src/__tests__/d1/migrate.test.ts index 1b8b5a558cb4..7a3e0188da4b 100644 --- a/packages/wrangler/src/__tests__/d1/migrate.test.ts +++ b/packages/wrangler/src/__tests__/d1/migrate.test.ts @@ -10,7 +10,7 @@ import { mockSetTimeout } from "../helpers/mock-set-timeout"; import { msw } from "../helpers/msw"; import { runInTempDir } from "../helpers/run-in-tmp"; import { runWrangler } from "../helpers/run-wrangler"; -import writeWranglerToml from "../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../helpers/write-wrangler-toml"; describe("migrate", () => { runInTempDir(); diff --git a/packages/wrangler/src/__tests__/d1/timeTravel.test.ts b/packages/wrangler/src/__tests__/d1/timeTravel.test.ts index 1d741d62931a..cc064bf33ffb 100644 --- a/packages/wrangler/src/__tests__/d1/timeTravel.test.ts +++ b/packages/wrangler/src/__tests__/d1/timeTravel.test.ts @@ -7,7 +7,7 @@ import { mockGetMemberships } from "../helpers/mock-oauth-flow"; import { msw } from "../helpers/msw"; import { runInTempDir } from "../helpers/run-in-tmp"; import { runWrangler } from "../helpers/run-wrangler"; -import writeWranglerToml from "../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../helpers/write-wrangler-toml"; describe("time-travel", () => { mockConsoleMethods(); diff --git a/packages/wrangler/src/__tests__/delete.test.ts b/packages/wrangler/src/__tests__/delete.test.ts index e66c52b9a168..90a124a43de7 100644 --- a/packages/wrangler/src/__tests__/delete.test.ts +++ b/packages/wrangler/src/__tests__/delete.test.ts @@ -6,7 +6,7 @@ import { useMockIsTTY } from "./helpers/mock-istty"; import { msw } from "./helpers/msw"; import { runInTempDir } from "./helpers/run-in-tmp"; import { runWrangler } from "./helpers/run-wrangler"; -import writeWranglerToml from "./helpers/write-wrangler-toml"; +import { writeWranglerToml } from "./helpers/write-wrangler-toml"; import type { ServiceReferenceResponse, Tail } from "../delete"; import type { KVNamespaceInfo } from "../kv/helpers"; diff --git a/packages/wrangler/src/__tests__/deploy.test.ts b/packages/wrangler/src/__tests__/deploy.test.ts index 4c39d9b9459e..1f4f96e7a5b1 100644 --- a/packages/wrangler/src/__tests__/deploy.test.ts +++ b/packages/wrangler/src/__tests__/deploy.test.ts @@ -47,7 +47,7 @@ import { mswListNewDeploymentsLatestFull } from "./helpers/msw/handlers/versions import { runInTempDir } from "./helpers/run-in-tmp"; import { runWrangler } from "./helpers/run-wrangler"; import { writeWorkerSource } from "./helpers/write-worker-source"; -import writeWranglerToml from "./helpers/write-wrangler-toml"; +import { writeWranglerToml } from "./helpers/write-wrangler-toml"; import type { Config } from "../config"; import type { CustomDomain, CustomDomainChangeset } from "../deploy/deploy"; import type { KVNamespaceInfo } from "../kv/helpers"; @@ -88,6 +88,190 @@ describe("deploy", () => { clearDialogs(); }); + it("should resolve wrangler.toml relative to the entrypoint", async () => { + fs.mkdirSync("./some-path/worker", { recursive: true }); + fs.writeFileSync( + "./some-path/wrangler.toml", + TOML.stringify({ + name: "test-name", + compatibility_date: "2022-01-12", + vars: { xyz: 123 }, + }), + "utf-8" + ); + writeWorkerSource({ basePath: "./some-path/worker" }); + mockUploadWorkerRequest({ + expectedBindings: [ + { + json: 123, + name: "xyz", + type: "json", + }, + ], + expectedCompatibilityDate: "2022-01-12", + }); + mockSubDomainRequest(); + await runWrangler("deploy ./some-path/worker/index.js"); + expect(std.out).toMatchInlineSnapshot(` + "Total Upload: xx KiB / gzip: xx KiB + Your worker has access to the following bindings: + - Vars: + - xyz: 123 + Uploaded test-name (TIMINGS) + Published test-name (TIMINGS) + https://test-name.test-sub-domain.workers.dev + Current Deployment ID: Galaxy-Class + Current Version ID: Galaxy-Class + + + Note: Deployment ID has been renamed to Version ID. Deployment ID is present to maintain compatibility with the previous behavior of this command. This output will change in a future version of Wrangler. To learn more visit: https://developers.cloudflare.com/workers/configuration/versions-and-deployments" + `); + expect(std.err).toMatchInlineSnapshot(`""`); + }); + + it("should support wrangler.json", async () => { + fs.mkdirSync("./my-worker", { recursive: true }); + fs.writeFileSync( + "./wrangler.json", + JSON.stringify({ + name: "test-worker", + compatibility_date: "2024-01-01", + vars: { xyz: 123 }, + }), + "utf-8" + ); + writeWorkerSource({ basePath: "./my-worker" }); + mockUploadWorkerRequest({ + expectedScriptName: "test-worker", + expectedBindings: [ + { + json: 123, + name: "xyz", + type: "json", + }, + ], + expectedCompatibilityDate: "2024-01-01", + }); + mockSubDomainRequest(); + + await runWrangler("deploy ./my-worker/index.js --experimental-json-config"); + expect(std.out).toMatchInlineSnapshot(` + "Total Upload: xx KiB / gzip: xx KiB + Your worker has access to the following bindings: + - Vars: + - xyz: 123 + Uploaded test-worker (TIMINGS) + Published test-worker (TIMINGS) + https://test-worker.test-sub-domain.workers.dev + Current Deployment ID: Galaxy-Class + Current Version ID: Galaxy-Class + + + Note: Deployment ID has been renamed to Version ID. Deployment ID is present to maintain compatibility with the previous behavior of this command. This output will change in a future version of Wrangler. To learn more visit: https://developers.cloudflare.com/workers/configuration/versions-and-deployments" + `); + expect(std.err).toMatchInlineSnapshot(`""`); + }); + + it("should support wrangler.jsonc", async () => { + fs.mkdirSync("./my-worker", { recursive: true }); + fs.writeFileSync( + "./wrangler.jsonc", + JSON.stringify({ + name: "test-worker-jsonc", + compatibility_date: "2024-01-01", + vars: { xyz: 123 }, + }), + "utf-8" + ); + writeWorkerSource({ basePath: "./my-worker" }); + mockUploadWorkerRequest({ + expectedScriptName: "test-worker-jsonc", + expectedBindings: [ + { + json: 123, + name: "xyz", + type: "json", + }, + ], + expectedCompatibilityDate: "2024-01-01", + }); + mockSubDomainRequest(); + + await runWrangler("deploy ./my-worker/index.js --experimental-json-config"); + expect(std.out).toMatchInlineSnapshot(` + "Total Upload: xx KiB / gzip: xx KiB + Your worker has access to the following bindings: + - Vars: + - xyz: 123 + Uploaded test-worker-jsonc (TIMINGS) + Published test-worker-jsonc (TIMINGS) + https://test-worker-jsonc.test-sub-domain.workers.dev + Current Deployment ID: Galaxy-Class + Current Version ID: Galaxy-Class + + + Note: Deployment ID has been renamed to Version ID. Deployment ID is present to maintain compatibility with the previous behavior of this command. This output will change in a future version of Wrangler. To learn more visit: https://developers.cloudflare.com/workers/configuration/versions-and-deployments" + `); + expect(std.err).toMatchInlineSnapshot(`""`); + }); + + it("should not deploy if there's any other kind of error when checking deployment source", async () => { + writeWorkerSource(); + writeWranglerToml(); + mockSubDomainRequest(); + mockUploadWorkerRequest(); + msw.use(...mswSuccessOauthHandlers, ...mswSuccessUserHandlers); + msw.use( + http.get("*/accounts/:accountId/workers/services/:scriptName", () => { + return HttpResponse.json( + createFetchResult(null, false, [ + { code: 10000, message: "Authentication error" }, + ]) + ); + }), + http.get( + "*/accounts/:accountId/workers/deployments/by-script/:scriptTag", + () => { + return HttpResponse.json( + createFetchResult({ + latest: { number: "2" }, + }) + ); + } + ) + ); + + await expect( + runWrangler("deploy index.js") + ).rejects.toThrowErrorMatchingInlineSnapshot( + `[APIError: A request to the Cloudflare API (/accounts/some-account-id/workers/services/test-name) failed.]` + ); + expect(std.out).toMatchInlineSnapshot(` + " + X [ERROR] A request to the Cloudflare API (/accounts/some-account-id/workers/services/test-name) failed. + + Authentication error [code: 10000] + + + 📎 It looks like you are authenticating Wrangler via a custom API token set in an environment variable. + Please ensure it has the correct permissions for this operation. + + Getting User settings... + 👋 You are logged in with an API Token, associated with the email user@example.com! + ℹī¸ The API Token is read from the CLOUDFLARE_API_TOKEN in your environment. + ┌───────────────â”Ŧ────────────┐ + │ Account Name │ Account ID │ + ├───────────────â”ŧ────────────┤ + │ Account One │ account-1 │ + ├───────────────â”ŧ────────────┤ + │ Account Two │ account-2 │ + ├───────────────â”ŧ────────────┤ + │ Account Three │ account-3 │ + └───────────────┴────────────┘ + 🔓 To see token permissions visit https://dash.cloudflare.com/profile/api-tokens" + `); + }); + describe("output additional script information", () => { it("for first party workers, it should print worker information at log level", async () => { setIsTTY(false); @@ -672,47 +856,6 @@ describe("deploy", () => { }); }); - it("should resolve wrangler.toml relative to the entrypoint", async () => { - fs.mkdirSync("./some-path/worker", { recursive: true }); - fs.writeFileSync( - "./some-path/wrangler.toml", - TOML.stringify({ - name: "test-name", - compatibility_date: "2022-01-12", - vars: { xyz: 123 }, - }), - "utf-8" - ); - writeWorkerSource({ basePath: "./some-path/worker" }); - mockUploadWorkerRequest({ - expectedBindings: [ - { - json: 123, - name: "xyz", - type: "json", - }, - ], - expectedCompatibilityDate: "2022-01-12", - }); - mockSubDomainRequest(); - await runWrangler("deploy ./some-path/worker/index.js"); - expect(std.out).toMatchInlineSnapshot(` - "Total Upload: xx KiB / gzip: xx KiB - Your worker has access to the following bindings: - - Vars: - - xyz: 123 - Uploaded test-name (TIMINGS) - Published test-name (TIMINGS) - https://test-name.test-sub-domain.workers.dev - Current Deployment ID: Galaxy-Class - Current Version ID: Galaxy-Class - - - Note: Deployment ID has been renamed to Version ID. Deployment ID is present to maintain compatibility with the previous behavior of this command. This output will change in a future version of Wrangler. To learn more visit: https://developers.cloudflare.com/workers/configuration/versions-and-deployments" - `); - expect(std.err).toMatchInlineSnapshot(`""`); - }); - describe("routes", () => { it("should deploy the worker to a route", async () => { writeWranglerToml({ @@ -4947,6 +5090,7 @@ addEventListener('fetch', event => {});` ); }); }); + describe("custom builds", () => { beforeEach(() => { vi.unstubAllGlobals(); @@ -9130,63 +9274,6 @@ export default{ }); }); - it("should not deploy if there's any other kind of error when checking deployment source", async () => { - writeWorkerSource(); - writeWranglerToml(); - mockSubDomainRequest(); - mockUploadWorkerRequest(); - msw.use(...mswSuccessOauthHandlers, ...mswSuccessUserHandlers); - msw.use( - http.get("*/accounts/:accountId/workers/services/:scriptName", () => { - return HttpResponse.json( - createFetchResult(null, false, [ - { code: 10000, message: "Authentication error" }, - ]) - ); - }), - http.get( - "*/accounts/:accountId/workers/deployments/by-script/:scriptTag", - () => { - return HttpResponse.json( - createFetchResult({ - latest: { number: "2" }, - }) - ); - } - ) - ); - - await expect( - runWrangler("deploy index.js") - ).rejects.toThrowErrorMatchingInlineSnapshot( - `[APIError: A request to the Cloudflare API (/accounts/some-account-id/workers/services/test-name) failed.]` - ); - expect(std.out).toMatchInlineSnapshot(` - " - X [ERROR] A request to the Cloudflare API (/accounts/some-account-id/workers/services/test-name) failed. - - Authentication error [code: 10000] - - - 📎 It looks like you are authenticating Wrangler via a custom API token set in an environment variable. - Please ensure it has the correct permissions for this operation. - - Getting User settings... - 👋 You are logged in with an API Token, associated with the email user@example.com! - ℹī¸ The API Token is read from the CLOUDFLARE_API_TOKEN in your environment. - ┌───────────────â”Ŧ────────────┐ - │ Account Name │ Account ID │ - ├───────────────â”ŧ────────────┤ - │ Account One │ account-1 │ - ├───────────────â”ŧ────────────┤ - │ Account Two │ account-2 │ - ├───────────────â”ŧ────────────┤ - │ Account Three │ account-3 │ - └───────────────┴────────────┘ - 🔓 To see token permissions visit https://dash.cloudflare.com/profile/api-tokens" - `); - }); - describe("queues", () => { const queueId = "queue-id"; const queueName = "queue1"; diff --git a/packages/wrangler/src/__tests__/deployments.test.ts b/packages/wrangler/src/__tests__/deployments.test.ts index 2eee504dc625..c32a26cb63d8 100644 --- a/packages/wrangler/src/__tests__/deployments.test.ts +++ b/packages/wrangler/src/__tests__/deployments.test.ts @@ -15,7 +15,7 @@ import { } from "./helpers/msw"; import { runInTempDir } from "./helpers/run-in-tmp"; import { runWrangler } from "./helpers/run-wrangler"; -import writeWranglerToml from "./helpers/write-wrangler-toml"; +import { writeWranglerToml } from "./helpers/write-wrangler-toml"; function isFileNotFound(e: unknown) { return ( diff --git a/packages/wrangler/src/__tests__/deprecated-usage-model.test.ts b/packages/wrangler/src/__tests__/deprecated-usage-model.test.ts index 3e5ef2472277..aea44c9e1bd9 100644 --- a/packages/wrangler/src/__tests__/deprecated-usage-model.test.ts +++ b/packages/wrangler/src/__tests__/deprecated-usage-model.test.ts @@ -8,7 +8,7 @@ import { mswListNewDeploymentsLatestFull } from "./helpers/msw/handlers/versions import { runInTempDir } from "./helpers/run-in-tmp"; import { runWrangler } from "./helpers/run-wrangler"; import { writeWorkerSource } from "./helpers/write-worker-source"; -import writeWranglerToml from "./helpers/write-wrangler-toml"; +import { writeWranglerToml } from "./helpers/write-wrangler-toml"; describe("deprecated-usage-model", () => { mockAccountId(); diff --git a/packages/wrangler/src/__tests__/dev.test.tsx b/packages/wrangler/src/__tests__/dev.test.tsx index 20452d820caa..648d0188a9f2 100644 --- a/packages/wrangler/src/__tests__/dev.test.tsx +++ b/packages/wrangler/src/__tests__/dev.test.tsx @@ -18,7 +18,10 @@ import { } from "./helpers/msw"; import { runInTempDir } from "./helpers/run-in-tmp"; import { runWrangler } from "./helpers/run-wrangler"; -import writeWranglerToml from "./helpers/write-wrangler-toml"; +import { + writeWranglerJson, + writeWranglerToml, +} from "./helpers/write-wrangler-toml"; import type { Mock } from "vitest"; async function expectedHostAndZone( @@ -70,6 +73,53 @@ describe("wrangler dev", () => { msw.resetHandlers(); }); + describe("config file support", () => { + it("should support wrangler.toml", async () => { + writeWranglerToml({ + name: "test-worker-toml", + main: "index.js", + compatibility_date: "2024-01-01", + }); + fs.writeFileSync("index.js", `export default {};`); + + await runWrangler("dev"); + expect(std.out).toMatchInlineSnapshot(`""`); + expect(std.err).toMatchInlineSnapshot(`""`); + expect(std.warn).toMatchInlineSnapshot(`""`); + }); + + it("should support wrangler.json", async () => { + writeWranglerJson({ + name: "test-worker-json", + main: "index.js", + compatibility_date: "2024-01-01", + }); + fs.writeFileSync("index.js", `export default {};`); + + await runWrangler("dev --experimental-json-config"); + expect(std.out).toMatchInlineSnapshot(`""`); + expect(std.err).toMatchInlineSnapshot(`""`); + expect(std.warn).toMatchInlineSnapshot(`""`); + }); + + it("should support wrangler.jsonc", async () => { + writeWranglerJson( + { + name: "test-worker-jsonc", + main: "index.js", + compatibility_date: "2024-01-01", + }, + "wrangler.jsonc" + ); + fs.writeFileSync("index.js", `export default {};`); + + await runWrangler("dev --experimental-json-config"); + expect(std.out).toMatchInlineSnapshot(`""`); + expect(std.err).toMatchInlineSnapshot(`""`); + expect(std.warn).toMatchInlineSnapshot(`""`); + }); + }); + describe("authorization", () => { mockApiToken({ apiToken: null }); const isCISpy = vi.spyOn(CI, "isCI").mockReturnValue(true); @@ -248,6 +298,7 @@ describe("wrangler dev", () => { ); }); }); + describe("host", () => { it("should resolve a host to its zone", async () => { writeWranglerToml({ diff --git a/packages/wrangler/src/__tests__/helpers/write-wrangler-toml.ts b/packages/wrangler/src/__tests__/helpers/write-wrangler-toml.ts index d70adb3a02b6..8f22904dd46c 100644 --- a/packages/wrangler/src/__tests__/helpers/write-wrangler-toml.ts +++ b/packages/wrangler/src/__tests__/helpers/write-wrangler-toml.ts @@ -3,7 +3,7 @@ import TOML from "@iarna/toml"; import type { RawConfig } from "../../config"; /** Write a mock wrangler.toml file to disk. */ -export default function writeWranglerToml( +export function writeWranglerToml( config: RawConfig = {}, path = "./wrangler.toml" ) { @@ -14,6 +14,21 @@ export default function writeWranglerToml( name: "test-name", ...(config as TOML.JsonMap), }), + "utf-8" + ); +} + +export function writeWranglerJson( + config: RawConfig = {}, + path = "./wrangler.json" +) { + fs.writeFileSync( + path, + JSON.stringify({ + compatibility_date: "2022-01-12", + name: "test-name", + ...config, + }), "utf-8" ); diff --git a/packages/wrangler/src/__tests__/index.test.ts b/packages/wrangler/src/__tests__/index.test.ts index 8b4870417042..e3d2f2735ee0 100644 --- a/packages/wrangler/src/__tests__/index.test.ts +++ b/packages/wrangler/src/__tests__/index.test.ts @@ -7,7 +7,7 @@ import { mockConsoleMethods } from "./helpers/mock-console"; import { runInTempDir } from "./helpers/run-in-tmp"; import { runWrangler } from "./helpers/run-wrangler"; import { writeWorkerSource } from "./helpers/write-worker-source"; -import writeWranglerToml from "./helpers/write-wrangler-toml"; +import { writeWranglerToml } from "./helpers/write-wrangler-toml"; import type { PackageManager } from "../package-manager"; import type { Mock } from "vitest"; diff --git a/packages/wrangler/src/__tests__/pages/pages-build-env.test.ts b/packages/wrangler/src/__tests__/pages/pages-build-env.test.ts index 6f58c119b22c..449e699ec0c2 100644 --- a/packages/wrangler/src/__tests__/pages/pages-build-env.test.ts +++ b/packages/wrangler/src/__tests__/pages/pages-build-env.test.ts @@ -8,7 +8,7 @@ import { 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"; +import { writeWranglerToml } from "../helpers/write-wrangler-toml"; describe("pages build env", () => { const std = mockConsoleMethods(); diff --git a/packages/wrangler/src/__tests__/pages/pages-download-config.test.ts b/packages/wrangler/src/__tests__/pages/pages-download-config.test.ts index f940873030b1..0e7405f65983 100644 --- a/packages/wrangler/src/__tests__/pages/pages-download-config.test.ts +++ b/packages/wrangler/src/__tests__/pages/pages-download-config.test.ts @@ -10,7 +10,7 @@ import { useMockIsTTY } from "../helpers/mock-istty"; import { msw } from "../helpers/msw"; import { runInTempDir } from "../helpers/run-in-tmp"; import { runWrangler } from "../helpers/run-wrangler"; -import writeWranglerToml from "../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../helpers/write-wrangler-toml"; async function readNormalizedWranglerToml() { return (await readFile("wrangler.toml", "utf8")) diff --git a/packages/wrangler/src/__tests__/versions/deployments/deployments.list.test.ts b/packages/wrangler/src/__tests__/versions/deployments/deployments.list.test.ts index f2c5e3e6b5fd..9cbace8c088c 100644 --- a/packages/wrangler/src/__tests__/versions/deployments/deployments.list.test.ts +++ b/packages/wrangler/src/__tests__/versions/deployments/deployments.list.test.ts @@ -5,7 +5,7 @@ import { mockConsoleMethods } from "../../helpers/mock-console"; import { msw, mswGetVersion, mswListNewDeployments } from "../../helpers/msw"; import { runInTempDir } from "../../helpers/run-in-tmp"; import { runWrangler } from "../../helpers/run-wrangler"; -import writeWranglerToml from "../../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../../helpers/write-wrangler-toml"; describe("deployments list", () => { mockAccountId(); diff --git a/packages/wrangler/src/__tests__/versions/deployments/deployments.status.test.ts b/packages/wrangler/src/__tests__/versions/deployments/deployments.status.test.ts index 99e3ed8d75e0..f55f199dcff3 100644 --- a/packages/wrangler/src/__tests__/versions/deployments/deployments.status.test.ts +++ b/packages/wrangler/src/__tests__/versions/deployments/deployments.status.test.ts @@ -5,7 +5,7 @@ import { mockConsoleMethods } from "../../helpers/mock-console"; import { msw, mswGetVersion, mswListNewDeployments } from "../../helpers/msw"; import { runInTempDir } from "../../helpers/run-in-tmp"; import { runWrangler } from "../../helpers/run-wrangler"; -import writeWranglerToml from "../../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../../helpers/write-wrangler-toml"; describe("deployments list", () => { mockAccountId(); diff --git a/packages/wrangler/src/__tests__/versions/secrets/delete.test.ts b/packages/wrangler/src/__tests__/versions/secrets/delete.test.ts index b9466286c8de..2f7e03c0fe8a 100644 --- a/packages/wrangler/src/__tests__/versions/secrets/delete.test.ts +++ b/packages/wrangler/src/__tests__/versions/secrets/delete.test.ts @@ -5,7 +5,7 @@ import { clearDialogs, mockConfirm } from "../../helpers/mock-dialogs"; import { useMockIsTTY } from "../../helpers/mock-istty"; import { runInTempDir } from "../../helpers/run-in-tmp"; import { runWrangler } from "../../helpers/run-wrangler"; -import writeWranglerToml from "../../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../../helpers/write-wrangler-toml"; import { mockGetVersion, mockPostVersion, mockSetupApiCalls } from "./utils"; describe("versions secret put", () => { diff --git a/packages/wrangler/src/__tests__/versions/secrets/list.test.ts b/packages/wrangler/src/__tests__/versions/secrets/list.test.ts index 146eebc8006b..d298416657dd 100644 --- a/packages/wrangler/src/__tests__/versions/secrets/list.test.ts +++ b/packages/wrangler/src/__tests__/versions/secrets/list.test.ts @@ -4,7 +4,7 @@ import { mockAccountId, mockApiToken } from "../../helpers/mock-account-id"; import { mockConsoleMethods } from "../../helpers/mock-console"; import { createFetchResult, msw } from "../../helpers/msw"; import { runWrangler } from "../../helpers/run-wrangler"; -import writeWranglerToml from "../../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../../helpers/write-wrangler-toml"; import type { ApiDeployment, ApiVersion } from "../../../versions/types"; describe("versions secret list", () => { diff --git a/packages/wrangler/src/__tests__/versions/secrets/put.test.ts b/packages/wrangler/src/__tests__/versions/secrets/put.test.ts index 91d29e45cb11..e057a4b40ed8 100644 --- a/packages/wrangler/src/__tests__/versions/secrets/put.test.ts +++ b/packages/wrangler/src/__tests__/versions/secrets/put.test.ts @@ -6,7 +6,7 @@ import { useMockIsTTY } from "../../helpers/mock-istty"; import { useMockStdin } from "../../helpers/mock-stdin"; import { runInTempDir } from "../../helpers/run-in-tmp"; import { runWrangler } from "../../helpers/run-wrangler"; -import writeWranglerToml from "../../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../../helpers/write-wrangler-toml"; import { mockPostVersion, mockSetupApiCalls } from "./utils"; describe("versions secret put", () => { diff --git a/packages/wrangler/src/__tests__/versions/versions.deploy.test.ts b/packages/wrangler/src/__tests__/versions/versions.deploy.test.ts index 1155d46ddfa7..14f88121f6cb 100644 --- a/packages/wrangler/src/__tests__/versions/versions.deploy.test.ts +++ b/packages/wrangler/src/__tests__/versions/versions.deploy.test.ts @@ -26,7 +26,7 @@ import { mswListNewDeploymentsLatestFiftyFifty } from "../helpers/msw/handlers/v import { runInTempDir } from "../helpers/run-in-tmp"; import { runWrangler } from "../helpers/run-wrangler"; import { writeWorkerSource } from "../helpers/write-worker-source"; -import writeWranglerToml from "../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../helpers/write-wrangler-toml"; import type { VersionsDeployArgs } from "../../versions/deploy"; describe("versions deploy", () => { diff --git a/packages/wrangler/src/__tests__/versions/versions.list.test.ts b/packages/wrangler/src/__tests__/versions/versions.list.test.ts index ead39e938fd3..87b9424a019b 100644 --- a/packages/wrangler/src/__tests__/versions/versions.list.test.ts +++ b/packages/wrangler/src/__tests__/versions/versions.list.test.ts @@ -5,7 +5,7 @@ import { mockConsoleMethods } from "../helpers/mock-console"; import { msw, mswListVersions } from "../helpers/msw"; import { runInTempDir } from "../helpers/run-in-tmp"; import { runWrangler } from "../helpers/run-wrangler"; -import writeWranglerToml from "../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../helpers/write-wrangler-toml"; describe("versions list", () => { mockAccountId(); diff --git a/packages/wrangler/src/__tests__/versions/versions.view.test.ts b/packages/wrangler/src/__tests__/versions/versions.view.test.ts index 59dd98b63e10..360b0d2e703c 100644 --- a/packages/wrangler/src/__tests__/versions/versions.view.test.ts +++ b/packages/wrangler/src/__tests__/versions/versions.view.test.ts @@ -5,7 +5,7 @@ import { mockConsoleMethods } from "../helpers/mock-console"; import { msw, mswGetVersion } from "../helpers/msw"; import { runInTempDir } from "../helpers/run-in-tmp"; import { runWrangler } from "../helpers/run-wrangler"; -import writeWranglerToml from "../helpers/write-wrangler-toml"; +import { writeWranglerToml } from "../helpers/write-wrangler-toml"; describe("versions view", () => { mockAccountId(); diff --git a/packages/wrangler/src/api/integrations/platform/index.ts b/packages/wrangler/src/api/integrations/platform/index.ts index 66f5e73f4287..89ed2cc564a8 100644 --- a/packages/wrangler/src/api/integrations/platform/index.ts +++ b/packages/wrangler/src/api/integrations/platform/index.ts @@ -35,7 +35,7 @@ export type GetPlatformProxyOptions = { */ configPath?: string; /** - * Flag to indicate the utility to read a json config file (`wrangler.json`) + * Flag to indicate the utility to read a json config file (`wrangler.json`/`wrangler.jsonc`) * instead of the toml one (`wrangler.toml`) * * Note: this feature is experimental diff --git a/packages/wrangler/src/config/index.ts b/packages/wrangler/src/config/index.ts index d1da1d564b10..e23dd4383723 100644 --- a/packages/wrangler/src/config/index.ts +++ b/packages/wrangler/src/config/index.ts @@ -65,7 +65,7 @@ export function readConfig( // Load the configuration from disk if available if (configPath?.endsWith("toml")) { rawConfig = parseTOML(readFileSync(configPath), configPath); - } else if (configPath?.endsWith("json")) { + } else if (configPath?.endsWith("json") || configPath?.endsWith("jsonc")) { rawConfig = parseJSONC(readFileSync(configPath), configPath); } } catch (e) { @@ -90,7 +90,8 @@ export function readConfig( * configuration file belongs to a Workers or Pages project. This key * should always be set for Pages but never for Workers. Furthermore, * Pages projects currently have support for `wrangler.toml` only, - * so we should error if `wrangler.json` is detected in a Pages project + * so we should error if `wrangler.json` || `wrangler.jsonc` is detected + * in a Pages project */ const isPagesConfigFile = isPagesConfig(rawConfig); if (!isPagesConfigFile && requirePagesConfig) { @@ -101,7 +102,9 @@ export function readConfig( } if ( isPagesConfigFile && - (configPath?.endsWith("json") || isJsonConfigEnabled) + (configPath?.endsWith("json") || + configPath?.endsWith("jsonc") || + isJsonConfigEnabled) ) { throw new UserError( `Pages doesn't currently support JSON formatted config \`${ @@ -169,6 +172,7 @@ export function findWranglerToml( if (preferJson) { return ( findUpSync(`wrangler.json`, { cwd: referencePath }) ?? + findUpSync(`wrangler.jsonc`, { cwd: referencePath }) ?? findUpSync(`wrangler.toml`, { cwd: referencePath }) ); }