Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Apr 10, 2024
1 parent 8c994b8 commit e76dbb9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 54 deletions.
13 changes: 4 additions & 9 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { defineNuxtModule, createResolver, logger, addServerScanDir, installModule, addServerImportsDir } from '@nuxt/kit'
import { join } from 'pathe'
import { defu } from 'defu'
import { mkdir, writeFile, readFile, copyFile } from 'node:fs/promises'
import { mkdir, writeFile, readFile } from 'node:fs/promises'
import { findWorkspaceDir } from 'pkg-types'
import { $fetch } from 'ofetch'
import { joinURL } from 'ufo'
import { parseArgs } from 'citty'
import { addDevtoolsCustomTabs, generateWrangler, generateWranglerForPages, type DeployConfig } from './utils'
import { addDevtoolsCustomTabs, generateWrangler } from './utils'
import { version } from '../package.json'
import { execSync } from 'node:child_process'
import { argv } from 'node:process'
Expand Down Expand Up @@ -157,7 +157,7 @@ export default defineNuxtModule<ModuleOptions>({
// 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) {
nuxt.hook('build:before', async () => {
const { deployConfig } = await $fetch<{ deployConfig: DeployConfig }>(`/api/projects/${process.env.NUXT_HUB_PROJECT_KEY}/build/${process.env.NUXT_HUB_ENV}/before`, {
const { wrangler } = await $fetch<{ wrangler: string }>(`/api/projects/${process.env.NUXT_HUB_PROJECT_KEY}/build/${process.env.NUXT_HUB_ENV}/before`, {
baseURL: hub.url,
method: 'POST',
headers: {
Expand All @@ -180,8 +180,7 @@ export default defineNuxtModule<ModuleOptions>({
process.exit(1)
})

const wranglerPath = join(rootDir, './wrangler.toml')
await writeFile(wranglerPath, generateWranglerForPages(process.env.NUXT_HUB_ENV as any, deployConfig), 'utf-8')
await writeFile(join(rootDir, './wrangler.toml'), wrangler, 'utf-8')
})

nuxt.hook('build:done', async () => {
Expand All @@ -202,10 +201,6 @@ export default defineNuxtModule<ModuleOptions>({
process.exit(1)
})
})

nuxt.hook('nitro:build:public-assets', async (nitro) => {
await copyFile(join(rootDir, 'wrangler.toml'), join(nitro.options.output.publicDir, 'wrangler.toml'))
})
} else {
// Write `dist/hub.config.json` after public assets are built
nuxt.hook('nitro:build:public-assets', async (nitro) => {
Expand Down
45 changes: 0 additions & 45 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,51 +37,6 @@ export function generateWrangler(hub: { kv: boolean, database: boolean, blob: bo
].flat().join('\n')
}

export function generateWranglerForPages(env: 'preview' | 'production', deployConfig: DeployConfig) {
return [
`name = "${deployConfig.name}"`,
'compatibility_date = "2024-04-05"',
'pages_build_output_dir = "./dist"',
'compatibility_flags = [ "nodejs_compat" ]',

deployConfig.vars && Object.keys(deployConfig.vars).length > 0 ? [
`[env.${env}.vars]`,
...Object.entries(deployConfig.vars).map(([key, value]) => `${key} = "${value}"`),
'',
] : [],
deployConfig.analytics ? [
`[[env.${env}.analytics_engine_datasets]]`,
'binding = "ANALYTICS"',
`dataset = "${deployConfig.analytics}"`
] : [],
'',
deployConfig.blob ? [
`[[env.${env}.r2_buckets]]`,
'binding = "BLOB"',
`bucket_name = "${deployConfig.blob}"`,
] : [],
'',
deployConfig.cache ? [
`[[env.${env}.kv_namespaces]]`,
'binding = "CACHE"',
`id = "${deployConfig.cache}"`,
]: [],
'',
deployConfig.database ? [
`[[env.${env}.d1_databases]]`,
`database_id = "${deployConfig.database}"`,
'binding = "DB"',
'database_name = "DB"',
] : [],
'',
deployConfig.kv ? [
`[[env.${env}.kv_namespaces]]`,
'binding = "KV"',
`id = "${deployConfig.kv}"`,
]: [],
].flat().join('\n')
}

export function addDevtoolsCustomTabs(nuxt: Nuxt, hub: { kv: boolean, database: boolean, blob: boolean }) {
nuxt.hook('listen', (_, { url }) => {
hub.database && addCustomTab({
Expand Down

0 comments on commit e76dbb9

Please sign in to comment.