From f2a4f7dd33e967bdb6db35da8258a9850d34751b Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Sat, 18 May 2024 14:09:52 +0200 Subject: [PATCH 1/3] feat: support custom wrangler config --- src/module.ts | 11 ++++++++++- src/utils.ts | 9 +++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/module.ts b/src/module.ts index c0b72d55..7a2ef986 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 { stringifyTOML } from 'confbox' import { version } from '../package.json' +import type { WranglerConfiguration } from './utils' import { addDevtoolsCustomTabs, generateWrangler } from './utils' const log = logger.withTag('nuxt:hub') @@ -80,6 +82,11 @@ export interface ModuleOptions { * @default process.env.NUXT_HUB_PROJECT_SECRET_KEY */ projectSecretKey?: string + /** + * A custom wrangler configuration that will be merged to the generated wrangler.toml. Be careful to not overwrite the default configuration. + * This is useful to add custom bindings in development mode to access to uncovered features. + */ + wranglerConfiguration?: WranglerConfiguration } export default defineNuxtModule({ @@ -401,7 +408,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(options.wranglerConfiguration, generateWrangler(hub)) + 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 }) { From 81d329028697f67446166c69342a1953161b9899 Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Fri, 24 May 2024 06:42:45 +0200 Subject: [PATCH 2/3] feat: read the local wrangler configuration --- src/module.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/module.ts b/src/module.ts index 7a2ef986..3b24af4f 100644 --- a/src/module.ts +++ b/src/module.ts @@ -8,7 +8,7 @@ import { findWorkspaceDir } from 'pkg-types' import { $fetch } from 'ofetch' import { joinURL } from 'ufo' import { parseArgs } from 'citty' -import { stringifyTOML } from 'confbox' +import { parseTOML, stringifyTOML } from 'confbox' import { version } from '../package.json' import type { WranglerConfiguration } from './utils' import { addDevtoolsCustomTabs, generateWrangler } from './utils' @@ -82,11 +82,6 @@ export interface ModuleOptions { * @default process.env.NUXT_HUB_PROJECT_SECRET_KEY */ projectSecretKey?: string - /** - * A custom wrangler configuration that will be merged to the generated wrangler.toml. Be careful to not overwrite the default configuration. - * This is useful to add custom bindings in development mode to access to uncovered features. - */ - wranglerConfiguration?: WranglerConfiguration } export default defineNuxtModule({ @@ -168,6 +163,8 @@ export default defineNuxtModule({ return } + const localWranglerConfiguration: WranglerConfiguration = await readFile(join(rootDir, './wrangler.toml'), 'utf-8').then(file => parseTOML(file)).catch(() => {}) + // 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) @@ -409,7 +406,7 @@ export default defineNuxtModule({ // Generate the wrangler.toml file const wranglerPath = join(hubDir, './wrangler.toml') - const wranglerConfiguration = defu(options.wranglerConfiguration, generateWrangler(hub)) + 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 = { From 09273ce4bb0d4379f9e94e544d60d937c821f24d Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Fri, 24 May 2024 06:45:41 +0200 Subject: [PATCH 3/3] chore: improve internal typing and add comments --- src/module.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/module.ts b/src/module.ts index 3b24af4f..51819b03 100644 --- a/src/module.ts +++ b/src/module.ts @@ -163,7 +163,10 @@ export default defineNuxtModule({ return } - const localWranglerConfiguration: WranglerConfiguration = await readFile(join(rootDir, './wrangler.toml'), 'utf-8').then(file => parseTOML(file)).catch(() => {}) + // 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) {