1+ import { defineNuxtModule , createResolver } from 'nuxt/kit'
2+ import { join } from 'pathe'
3+ import { defu } from 'defu'
4+ import { mkdir , writeFile , readFile } from 'node:fs/promises'
5+
6+ export default defineNuxtModule ( {
7+ meta : {
8+ name : 'hub'
9+ } ,
10+ async setup ( _options , nuxt ) {
11+ const { resolve } = createResolver ( import . meta. url )
12+
13+ // Add Server utils based on environment
14+ // nuxt.options.nitro.imports = nuxt.options.nitro.imports || {}
15+ // nuxt.options.nitro.imports.dirs = nuxt.options.nitro.imports.dirs || []
16+ // nuxt.options.nitro.imports.dirs.push(resolve(`../server/_utils/${nuxt.options.dev ? 'dev' : 'prod'}/`))
17+
18+ // Production mode
19+ if ( ! nuxt . options . dev ) {
20+ return
21+ }
22+
23+ if ( process . env . NUXT_HUB_URL ) {
24+ // TODO: check on hub.nuxt.com if the project is connected
25+ // return
26+ // return once we support Proxy for all providers (R2 missing now)
27+ }
28+
29+ // Local development without remote connection
30+ // Create the .hub/ directory
31+ const hubDir = join ( nuxt . options . rootDir , './.hub' )
32+ try {
33+ await mkdir ( hubDir )
34+ } catch ( e : any ) {
35+ if ( e . errno === - 17 ) {
36+ // File already exists
37+ } else {
38+ throw e
39+ }
40+ }
41+ // Add it to .gitignore
42+ const gitignorePath = join ( nuxt . options . rootDir , './.gitignore' )
43+ const gitignore = await readFile ( gitignorePath , 'utf-8' )
44+ if ( ! gitignore . includes ( '.hub' ) ) {
45+ await writeFile ( gitignorePath , gitignore + '\n.hub' , 'utf-8' )
46+ }
47+
48+ // Generate the wrangler.toml file
49+ const wranglerPath = join ( hubDir , './wrangler.toml' )
50+ await writeFile ( wranglerPath , DEFAULT_WRANGLER , 'utf-8' )
51+ nuxt . options . runtimeConfig . wrangler = defu ( nuxt . options . runtimeConfig . wrangler , {
52+ configPath : wranglerPath ,
53+ persistDir : hubDir
54+ } )
55+ // Make sure runtime is transpiled
56+ // nuxt.options.nitro.externals.inline = nuxt.options.nitro.externals.inline || []
57+ // nuxt.options.nitro.externals.inline.push(resolve('./runtime/server'))
58+ // Add server plugin
59+ nuxt . options . nitro . plugins = nuxt . options . nitro . plugins || [ ]
60+ nuxt . options . nitro . plugins . push ( resolve ( './runtime/server/plugins/cloudflare.dev' ) )
61+ }
62+ } )
63+
64+ const DEFAULT_WRANGLER = `d1_databases = [
65+ { binding = "DB", database_name = "default", database_id = "default" },
66+ ]
67+ kv_namespaces = [
68+ { binding = "KV", id = "default" },
69+ ]
70+ r2_buckets = [
71+ { binding = "BUCKET", bucket_name = "default" },
72+ ]`
0 commit comments