@@ -2,7 +2,7 @@ import * as fs from "node:fs";
22import { basename , dirname , extname , join , relative , resolve } from "node:path" ;
33import { findUpSync } from "find-up" ;
44import { getNodeCompat } from "miniflare" ;
5- import { readConfig } from "../config" ;
5+ import { experimental_readRawConfig , readConfig } from "../config" ;
66import { resolveWranglerConfigPath } from "../config/config-helpers" ;
77import { getEntry } from "../deployment-bundle/entry" ;
88import { getVarsForDev } from "../dev/dev-vars" ;
@@ -12,7 +12,7 @@ import { parseJSONC } from "../parse";
1212import { printWranglerBanner } from "../wrangler-banner" ;
1313import { generateRuntimeTypes } from "./runtime" ;
1414import { logRuntimeTypesMessage } from "./runtime/log-runtime-types-message" ;
15- import type { Config } from "../config" ;
15+ import type { Config , RawEnvironment } from "../config" ;
1616import type { Entry } from "../deployment-bundle/entry" ;
1717import type { CfScriptFormat } from "../deployment-bundle/worker" ;
1818import type {
@@ -113,11 +113,9 @@ export async function typesHandler(
113113 true
114114 ) as Record < string , string > ;
115115
116- const configBindingsWithSecrets : Partial < Config > & {
117- secrets : Record < string , string > ;
118- } = {
116+ const configBindingsWithSecrets = {
119117 kv_namespaces : config . kv_namespaces ?? [ ] ,
120- vars : { ... config . vars } ,
118+ vars : getVarsInfo ( args ) ,
121119 wasm_modules : config . wasm_modules ,
122120 text_blobs : {
123121 ...config . text_blobs ,
@@ -207,15 +205,29 @@ export function generateImportSpecifier(from: string, to: string) {
207205 */
208206export function constructType (
209207 key : string ,
210- value : string | number | boolean ,
208+ value : string | number | boolean | string [ ] ,
211209 useRawVal = true
212210) {
213211 const typeKey = constructTypeKey ( key ) ;
214- if ( typeof value === "string" ) {
212+
213+ const stringValue =
214+ typeof value === "string"
215+ ? value
216+ : Array . isArray ( value ) && value . length === 1
217+ ? value [ 0 ]
218+ : null ;
219+
220+ if ( stringValue ) {
221+ if ( useRawVal ) {
222+ return `${ typeKey } : ${ stringValue } ;` ;
223+ }
224+ return `${ typeKey } : ${ JSON . stringify ( stringValue ) } ;` ;
225+ }
226+ if ( Array . isArray ( value ) ) {
215227 if ( useRawVal ) {
216- return `${ typeKey } : ${ value } ;` ;
228+ return `${ typeKey } : ${ value . join ( " | " ) } ;` ;
217229 }
218- return `${ typeKey } : ${ JSON . stringify ( value ) } ;` ;
230+ return `${ typeKey } : ${ value . map ( ( str ) => JSON . stringify ( str ) ) . join ( "|" ) } ;` ;
219231 }
220232 if ( typeof value === "number" || typeof value === "boolean" ) {
221233 return `${ typeKey } : ${ value } ;` ;
@@ -225,8 +237,12 @@ export function constructType(
225237
226238type Secrets = Record < string , string > ;
227239
240+ type ConfigToDTS = Partial < Omit < Config , "vars" > > & { vars : VarsInfo } & {
241+ secrets : Secrets ;
242+ } ;
243+
228244async function generateTypes (
229- configToDTS : Partial < Config > & { secrets : Secrets } ,
245+ configToDTS : ConfigToDTS ,
230246 config : Config ,
231247 envInterface : string ,
232248 outputPath : string
@@ -275,19 +291,25 @@ async function generateTypes(
275291 const vars = Object . entries ( configToDTS . vars ) . filter (
276292 ( [ key ] ) => ! ( key in configToDTS . secrets )
277293 ) ;
278- for ( const [ varName , varValue ] of vars ) {
279- if (
280- typeof varValue === "string" ||
281- typeof varValue === "number" ||
282- typeof varValue === "boolean"
283- ) {
284- envTypeStructure . push ( constructType ( varName , varValue , false ) ) ;
285- }
286- if ( typeof varValue === "object" && varValue !== null ) {
287- envTypeStructure . push (
288- constructType ( varName , JSON . stringify ( varValue ) , true )
289- ) ;
290- }
294+ for ( const [ varName , varInfo ] of vars ) {
295+ const varValueTypes = new Set (
296+ varInfo
297+ . map ( ( { value } ) => value )
298+ . map ( ( varValue ) => {
299+ if (
300+ typeof varValue === "string" ||
301+ typeof varValue === "number" ||
302+ typeof varValue === "boolean"
303+ ) {
304+ return `"${ varValue } "` ;
305+ }
306+ if ( typeof varValue === "object" && varValue !== null ) {
307+ return `${ JSON . stringify ( varValue ) } ` ;
308+ }
309+ } )
310+ . filter ( Boolean )
311+ ) as Set < string > ;
312+ envTypeStructure . push ( constructType ( varName , [ ...varValueTypes ] , true ) ) ;
291313 }
292314 }
293315
@@ -578,3 +600,30 @@ type TSConfig = {
578600 types : string [ ] ;
579601 } ;
580602} ;
603+
604+ type VarValue = Config [ "vars" ] [ string ] ;
605+
606+ type VarInfoValue = { value : VarValue ; env ?: string } ;
607+
608+ type VarsInfo = Record < string , VarInfoValue [ ] > ;
609+
610+ function getVarsInfo (
611+ args : StrictYargsOptionsToInterface < typeof typesOptions >
612+ ) : VarsInfo {
613+ const varsInfo : VarsInfo = { } ;
614+ const { rawConfig } = experimental_readRawConfig ( args ) ;
615+
616+ function collectVars ( vars : RawEnvironment [ "vars" ] , envName ?: string ) {
617+ Object . entries ( vars ?? { } ) . forEach ( ( [ key , value ] ) => {
618+ varsInfo [ key ] ??= [ ] ;
619+ varsInfo [ key ] . push ( { value, env : envName } ) ;
620+ } ) ;
621+ }
622+
623+ collectVars ( rawConfig . vars ) ;
624+ Object . entries ( rawConfig . env ?? { } ) . forEach ( ( [ envName , env ] ) => {
625+ collectVars ( env . vars , envName ) ;
626+ } ) ;
627+
628+ return varsInfo ;
629+ }
0 commit comments