|
1 | 1 | import * as fs from "node:fs"; |
2 | 2 | import { basename, dirname, extname, join, relative, resolve } from "node:path"; |
| 3 | +import { hash as blake3hash } from "blake3-wasm"; |
3 | 4 | import chalk from "chalk"; |
4 | 5 | import { findUpSync } from "find-up"; |
5 | 6 | import { getNodeCompat } from "miniflare"; |
@@ -139,47 +140,10 @@ export const typesCommand = createCommand({ |
139 | 140 | const types = []; |
140 | 141 | if (args.includeEnv) { |
141 | 142 | logger.log(`Generating project types...\n`); |
142 | | - const secrets = getVarsForDev( |
143 | | - // We do not want `getVarsForDev()` to merge in the standard vars into the dev vars |
144 | | - // because we want to be able to work with secrets differently to vars. |
145 | | - // So we pass in a fake vars object here. |
146 | | - { ...config, vars: {} }, |
147 | | - args.env, |
148 | | - true |
149 | | - ) as Record<string, string>; |
150 | | - |
151 | | - const configBindingsWithSecrets = { |
152 | | - kv_namespaces: config.kv_namespaces ?? [], |
153 | | - vars: collectAllVars(args), |
154 | | - wasm_modules: config.wasm_modules, |
155 | | - text_blobs: { |
156 | | - ...config.text_blobs, |
157 | | - }, |
158 | | - data_blobs: config.data_blobs, |
159 | | - durable_objects: config.durable_objects, |
160 | | - r2_buckets: config.r2_buckets, |
161 | | - d1_databases: config.d1_databases, |
162 | | - services: config.services, |
163 | | - analytics_engine_datasets: config.analytics_engine_datasets, |
164 | | - dispatch_namespaces: config.dispatch_namespaces, |
165 | | - logfwdr: config.logfwdr, |
166 | | - unsafe: config.unsafe, |
167 | | - rules: config.rules, |
168 | | - queues: config.queues, |
169 | | - send_email: config.send_email, |
170 | | - vectorize: config.vectorize, |
171 | | - hyperdrive: config.hyperdrive, |
172 | | - mtls_certificates: config.mtls_certificates, |
173 | | - browser: config.browser, |
174 | | - ai: config.ai, |
175 | | - version_metadata: config.version_metadata, |
176 | | - secrets, |
177 | | - assets: config.assets, |
178 | | - workflows: config.workflows, |
179 | | - }; |
180 | 143 |
|
181 | 144 | const { envHeader, envTypes } = await generateEnvTypes( |
182 | | - configBindingsWithSecrets, |
| 145 | + config, |
| 146 | + args, |
183 | 147 | envInterface, |
184 | 148 | outputPath, |
185 | 149 | entrypoint |
@@ -286,12 +250,53 @@ type ConfigToDTS = Partial<Omit<Config, "vars">> & { vars: VarTypes } & { |
286 | 250 | secrets: Secrets; |
287 | 251 | }; |
288 | 252 |
|
289 | | -async function generateEnvTypes( |
290 | | - configToDTS: ConfigToDTS, |
| 253 | +export async function generateEnvTypes( |
| 254 | + config: Config, |
| 255 | + args: Partial<(typeof typesCommand)["args"]>, |
291 | 256 | envInterface: string, |
292 | 257 | outputPath: string, |
293 | | - entrypoint?: Entry |
| 258 | + entrypoint?: Entry, |
| 259 | + log = true |
294 | 260 | ): Promise<{ envHeader?: string; envTypes?: string }> { |
| 261 | + const secrets = getVarsForDev( |
| 262 | + // We do not want `getVarsForDev()` to merge in the standard vars into the dev vars |
| 263 | + // because we want to be able to work with secrets differently to vars. |
| 264 | + // So we pass in a fake vars object here. |
| 265 | + { ...config, vars: {} }, |
| 266 | + args.env, |
| 267 | + true |
| 268 | + ) as Record<string, string>; |
| 269 | + |
| 270 | + const configToDTS: ConfigToDTS = { |
| 271 | + kv_namespaces: config.kv_namespaces ?? [], |
| 272 | + vars: collectAllVars(args), |
| 273 | + wasm_modules: config.wasm_modules, |
| 274 | + text_blobs: { |
| 275 | + ...config.text_blobs, |
| 276 | + }, |
| 277 | + data_blobs: config.data_blobs, |
| 278 | + durable_objects: config.durable_objects, |
| 279 | + r2_buckets: config.r2_buckets, |
| 280 | + d1_databases: config.d1_databases, |
| 281 | + services: config.services, |
| 282 | + analytics_engine_datasets: config.analytics_engine_datasets, |
| 283 | + dispatch_namespaces: config.dispatch_namespaces, |
| 284 | + logfwdr: config.logfwdr, |
| 285 | + unsafe: config.unsafe, |
| 286 | + rules: config.rules, |
| 287 | + queues: config.queues, |
| 288 | + send_email: config.send_email, |
| 289 | + vectorize: config.vectorize, |
| 290 | + hyperdrive: config.hyperdrive, |
| 291 | + mtls_certificates: config.mtls_certificates, |
| 292 | + browser: config.browser, |
| 293 | + ai: config.ai, |
| 294 | + version_metadata: config.version_metadata, |
| 295 | + secrets, |
| 296 | + assets: config.assets, |
| 297 | + workflows: config.workflows, |
| 298 | + }; |
| 299 | + |
295 | 300 | const entrypointFormat = entrypoint?.format ?? "modules"; |
296 | 301 | const fullOutputPath = resolve(outputPath); |
297 | 302 |
|
@@ -530,13 +535,20 @@ async function generateEnvTypes( |
530 | 535 | envTypeStructure.map(([key, value]) => `${key}: ${value};`), |
531 | 536 | modulesTypeStructure |
532 | 537 | ); |
533 | | - const envHeader = `// Generated by Wrangler by running \`${wranglerCommandUsed}\``; |
| 538 | + // todo replace envInterface with generic |
| 539 | + const hash = blake3hash(consoleOutput).toString("hex").slice(0, 32); |
534 | 540 |
|
535 | | - logger.log(chalk.dim(consoleOutput)); |
| 541 | + const envHeader = `// Generated by Wrangler by running \`${wranglerCommandUsed}\` (hash: ${hash})`; |
| 542 | + |
| 543 | + if (log) { |
| 544 | + logger.log(chalk.dim(consoleOutput)); |
| 545 | + } |
536 | 546 |
|
537 | 547 | return { envHeader, envTypes: fileContent }; |
538 | 548 | } else { |
539 | | - logger.log(chalk.dim("No project types to add.\n")); |
| 549 | + if (log) { |
| 550 | + logger.log(chalk.dim("No project types to add.\n")); |
| 551 | + } |
540 | 552 | return { |
541 | 553 | envHeader: undefined, |
542 | 554 | envTypes: undefined, |
@@ -627,7 +639,7 @@ type VarTypes = Record<string, string[]>; |
627 | 639 | * @returns an object which keys are the variable names and values are arrays containing all the computed types for such variables |
628 | 640 | */ |
629 | 641 | function collectAllVars( |
630 | | - args: (typeof typesCommand)["args"] |
| 642 | + args: Partial<(typeof typesCommand)["args"]> |
631 | 643 | ): Record<string, string[]> { |
632 | 644 | const varsInfo: Record<string, Set<string>> = {}; |
633 | 645 |
|
|
0 commit comments