Skip to content

Commit

Permalink
include the top level Worker name in the parsed config structure (clo…
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin authored Jan 8, 2025
1 parent f5bd6a7 commit e771fe9
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-llamas-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

include the top level Worker name in the parsed config structure
14 changes: 12 additions & 2 deletions packages/wrangler/src/__tests__/config/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,11 @@ describe("normalizeAndValidateConfig()", () => {

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect({ ...config, tsconfig: normalizePath(config.tsconfig!) }).toEqual(
expect.objectContaining({ ...expectedConfig, main: resolvedMain })
expect.objectContaining({
...expectedConfig,
main: resolvedMain,
topLevelName: expectedConfig.name,
})
);
expect(diagnostics.hasErrors()).toBe(false);
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -4152,6 +4156,7 @@ describe("normalizeAndValidateConfig()", () => {
...rawConfig,
main: resolvedMain,
name: "mock-name-dev",
topLevelName: "mock-name",
})
);
expect(diagnostics.hasErrors()).toBe(false);
Expand Down Expand Up @@ -4229,7 +4234,11 @@ describe("normalizeAndValidateConfig()", () => {
);

expect(config).toEqual(
expect.objectContaining({ ...rawEnv, main: resolvedMain })
expect.objectContaining({
...rawEnv,
main: resolvedMain,
topLevelName: "mock-name",
})
);
expect(diagnostics.hasErrors()).toBe(false);
expect(diagnostics.hasWarnings()).toBe(false);
Expand All @@ -4251,6 +4260,7 @@ describe("normalizeAndValidateConfig()", () => {
);

expect(config.name).toEqual("mock-name");
expect(config.topLevelName).toEqual("mock-name");
expect(diagnostics.hasErrors()).toBe(false);
expect(diagnostics.hasWarnings()).toBe(true);
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
Expand Down
23 changes: 19 additions & 4 deletions packages/wrangler/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,32 @@ import type { CamelCaseKey } from "yargs";
* - `@breaking`: the deprecation/optionality is a breaking change from Wrangler v1.
* - `@todo`: there's more work to be done (with details attached).
*/
export type Config = ConfigFields<DevConfig> & PagesConfigFields & Environment;
export type Config = ComputedFields &
ConfigFields<DevConfig> &
PagesConfigFields &
Environment;

export type RawConfig = Partial<ConfigFields<RawDevConfig>> &
PagesConfigFields &
RawEnvironment &
DeprecatedConfigFields &
EnvironmentMap & { $schema?: string };

export interface ConfigFields<Dev extends RawDevConfig> {
export interface ComputedFields {
/** The path to the Wrangler configuration file (if any, and possibly redirected from the user Wrangler configuration) used to create this configuration. */
configPath: string | undefined;
/** The path to the user's Wrangler configuration file (if any), which may have been redirected to another file that used to create this configuration. */
userConfigPath: string | undefined;
/**
* The original top level name for the Worker in the raw configuration.
*
* When a raw configuration has been flattened to a single environment the worker name may have been replaced or transformed.
* It can be useful to know what the top-level name was before the flattening.
*/
topLevelName: string | undefined;
}

export interface ConfigFields<Dev extends RawDevConfig> {
/**
* A boolean to enable "legacy" style wrangler environments (from Wrangler v1).
* These have been superseded by Services, but there may be projects that won't
Expand Down Expand Up @@ -285,6 +297,11 @@ export type OnlyCamelCase<T = Record<string, never>> = {
};

export const defaultWranglerConfig: Config = {
/* COMPUTED_FIELDS */
configPath: undefined,
userConfigPath: undefined,
topLevelName: undefined,

/*====================================================*/
/* Fields supported by both Workers & Pages */
/*====================================================*/
Expand Down Expand Up @@ -329,8 +346,6 @@ export const defaultWranglerConfig: Config = {
/* Fields supported by Workers only */
/*====================================================*/
/* TOP-LEVEL ONLY FIELDS */
configPath: undefined,
userConfigPath: undefined,
legacy_env: true,
site: undefined,
legacy_assets: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/config/validation-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const supportedPagesConfigFields = [
// normalizeAndValidateConfig() sets these values
"configPath",
"userConfigPath",
"topLevelName",
] as const;

export function validatePagesConfig(
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ export function normalizeAndValidateConfig(
const config: Config = {
configPath,
userConfigPath,
topLevelName: rawConfig.name,
pages_build_output_dir: normalizeAndValidatePagesBuildOutputDir(
configPath,
rawConfig.pages_build_output_dir
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/d1/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { requireAuth } from "../user";
import * as options from "./options";
import splitSqlQuery from "./splitter";
import { getDatabaseByNameOrBinding, getDatabaseInfoFromConfig } from "./utils";
import type { Config, ConfigFields, DevConfig, Environment } from "../config";
import type { Config } from "../config";
import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
Expand Down Expand Up @@ -197,7 +197,7 @@ export async function executeSql({
}: {
local: boolean | undefined;
remote: boolean | undefined;
config: ConfigFields<DevConfig> & Environment;
config: Config;
name: string;
shouldPrompt: boolean | undefined;
persistTo: string | undefined;
Expand Down
8 changes: 4 additions & 4 deletions packages/wrangler/src/d1/migrations/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isNonInteractiveOrCI } from "../../is-interactive";
import { logger } from "../../logger";
import { DEFAULT_MIGRATION_PATH } from "../constants";
import { executeSql } from "../execute";
import type { ConfigFields, DevConfig, Environment } from "../../config";
import type { Config } from "../../config";
import type { QueryResult } from "../execute";
import type { Migration } from "../types";

Expand Down Expand Up @@ -57,7 +57,7 @@ export async function getUnappliedMigrations({
migrationsPath: string;
local: boolean | undefined;
remote: boolean | undefined;
config: ConfigFields<DevConfig> & Environment;
config: Config;
name: string;
persistTo: string | undefined;
preview: boolean | undefined;
Expand Down Expand Up @@ -92,7 +92,7 @@ type ListAppliedMigrationsProps = {
migrationsTableName: string;
local: boolean | undefined;
remote: boolean | undefined;
config: ConfigFields<DevConfig> & Environment;
config: Config;
name: string;
persistTo: string | undefined;
preview: boolean | undefined;
Expand Down Expand Up @@ -170,7 +170,7 @@ export const initMigrationsTable = async ({
migrationsTableName: string;
local: boolean | undefined;
remote: boolean | undefined;
config: ConfigFields<DevConfig> & Environment;
config: Config;
name: string;
persistTo: string | undefined;
preview: boolean | undefined;
Expand Down

0 comments on commit e771fe9

Please sign in to comment.