Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support local wrangler.toml #113

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -171,6 +173,11 @@ export default defineNuxtModule<ModuleOptions>({
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<WranglerConfiguration>(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)
Expand Down Expand Up @@ -417,7 +424,9 @@ export default defineNuxtModule<ModuleOptions>({

// 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,
Expand Down
9 changes: 5 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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'] = [{
Expand Down Expand Up @@ -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 }) {
Expand Down
Loading