From e76dbb9210890ab9739f01e9c7b214a72f973b73 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Wed, 10 Apr 2024 18:36:34 +0200 Subject: [PATCH] up --- src/module.ts | 13 ++++--------- src/utils.ts | 45 --------------------------------------------- 2 files changed, 4 insertions(+), 54 deletions(-) diff --git a/src/module.ts b/src/module.ts index ab459f2d..87f1fe69 100644 --- a/src/module.ts +++ b/src/module.ts @@ -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' @@ -157,7 +157,7 @@ export default defineNuxtModule({ // 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: { @@ -180,8 +180,7 @@ export default defineNuxtModule({ 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 () => { @@ -202,10 +201,6 @@ export default defineNuxtModule({ 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) => { diff --git a/src/utils.ts b/src/utils.ts index 987c0b00..c8bf73d5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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({