From eaab77b96a13ff146afe4715af4980373c84533a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Mon, 16 Sep 2024 22:28:26 +0200 Subject: [PATCH] fix(preview): add other bindings support --- README.md | 4 ++++ src/commands/preview.mjs | 9 ++++++++- src/utils/wrangler.mjs | 18 ++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9fba5fe..3fc1553 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,10 @@ To preview your project locally, you can use the `nuxthub preview` command. This nuxthub preview ``` +Current limitations: +- `hubAI()` will ask you connect within the terminal with wrangler +- `hubBrowser()` is not supported as not supported by `wrangler pages dev` + ## Open in browser To open your project in the browser, you can use the `nuxthub open` command. This will open the URL of your project in the default browser. diff --git a/src/commands/preview.mjs b/src/commands/preview.mjs index 53b9b6b..a247632 100644 --- a/src/commands/preview.mjs +++ b/src/commands/preview.mjs @@ -1,6 +1,6 @@ import { consola } from 'consola' import { defineCommand } from 'citty' -import { writeFile, unlink } from 'node:fs/promises' +import { readFile, writeFile, unlink } from 'node:fs/promises' import { join } from 'pathe' import { execa } from 'execa' import { existsSync } from 'fs' @@ -22,6 +22,13 @@ export default defineCommand({ process.exit(1) } + // Add .wrangler to .gitignore + const gitignorePath = join(process.cwd(), '.gitignore') + const gitignore = await readFile(gitignorePath, 'utf-8').catch(() => '') + if (gitignore && !gitignore.includes('.wrangler')) { + await writeFile(gitignorePath, `${gitignore ? gitignore + '\n' : gitignore}.wrangler`, 'utf-8') + } + consola.info('Generating wrangler.toml...') const wrangler = generateWrangler(hubConfig) const wranglerPath = join(distDir, 'wrangler.toml') diff --git a/src/utils/wrangler.mjs b/src/utils/wrangler.mjs index a74720f..a094d79 100644 --- a/src/utils/wrangler.mjs +++ b/src/utils/wrangler.mjs @@ -14,12 +14,17 @@ export function generateWrangler(hub) { if (hub.bindings?.compatibilityDate) { wrangler['compatibility_date'] = hub.bindings.compatibilityDate } + if (hub.ai) { wrangler['ai'] = { binding: 'AI' } } + if (hub.browser) { + wrangler['browser'] = { binding: 'BROWSER' } + } + if (hub.analytics) { wrangler['analytics_engine_datasets'] = [{ binding: 'ANALYTICS', @@ -34,11 +39,20 @@ export function generateWrangler(hub) { }] } + if (hub.kv || hub.cache) { + wrangler['kv_namespaces'] = [] + } if (hub.kv) { - wrangler['kv_namespaces'] = [{ + wrangler['kv_namespaces'].push({ binding: 'KV', id: 'kv_default' - }] + }) + } + if (hub.cache) { + wrangler['kv_namespaces'].push({ + binding: 'CACHE', + id: 'cache_default' + }) } if (hub.database) {