diff --git a/src/module.ts b/src/module.ts index 9686416e..d0510993 100644 --- a/src/module.ts +++ b/src/module.ts @@ -8,7 +8,9 @@ import { findWorkspaceDir } from 'pkg-types' import { $fetch } from 'ofetch' import { joinURL } from 'ufo' import { parseArgs } from 'citty' +import { parseTOML, stringifyTOML } from 'confbox' import { version } from '../package.json' +import type { WranglerConfiguration } from './utils' import { addDevtoolsCustomTabs, generateWrangler } from './utils' const log = logger.withTag('nuxt:hub') @@ -171,6 +173,11 @@ export default defineNuxtModule({ return } + // Read user wrangler.toml configuration to merge with the generated one. Be careful, the local configuration will takes precedence. + const localWranglerConfiguration: WranglerConfiguration = await readFile(join(rootDir, './wrangler.toml'), 'utf-8') + .then(file => parseTOML(file)) + .catch(() => { return {} }) + // Within CF Pages CI/CD to notice NuxtHub about the build and hub config if (!nuxt.options.dev && process.env.CF_PAGES && process.env.NUXT_HUB_PROJECT_DEPLOY_TOKEN && process.env.NUXT_HUB_PROJECT_KEY && process.env.NUXT_HUB_ENV) { // Disable remote option (if set also for prod) @@ -417,7 +424,9 @@ export default defineNuxtModule({ // Generate the wrangler.toml file const wranglerPath = join(hubDir, './wrangler.toml') - await writeFile(wranglerPath, generateWrangler(hub), 'utf-8') + + const wranglerConfiguration = defu(generateWrangler(hub), localWranglerConfiguration) + await writeFile(wranglerPath, stringifyTOML(wranglerConfiguration), 'utf-8') // @ts-expect-error cloudflareDev is not typed here nuxt.options.nitro.cloudflareDev = { persistDir: hubDir, diff --git a/src/utils.ts b/src/utils.ts index 55bb0a24..d922cd42 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,9 +1,10 @@ import { addCustomTab } from '@nuxt/devtools-kit' import type { Nuxt } from 'nuxt/schema' -import { stringifyTOML } from 'confbox' -export function generateWrangler(hub: { kv: boolean, database: boolean, blob: boolean, cache: boolean, analytics: boolean }) { - const wrangler: { [key: string]: any } = {} +export type WranglerConfiguration = { [key: string]: any } + +export function generateWrangler(hub: { kv: boolean, database: boolean, blob: boolean, cache: boolean, analytics: boolean }): WranglerConfiguration { + const wrangler: WranglerConfiguration = {} if (hub.analytics) { wrangler['analytics_engine_datasets'] = [{ @@ -43,7 +44,7 @@ export function generateWrangler(hub: { kv: boolean, database: boolean, blob: bo }] } - return stringifyTOML(wrangler) + return wrangler } export function addDevtoolsCustomTabs(nuxt: Nuxt, hub: { kv: boolean, database: boolean, blob: boolean, cache: boolean, analytics: boolean }) {