Skip to content

Commit

Permalink
fix(preview): add other bindings support
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Sep 16, 2024
1 parent 48730a2 commit eaab77b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion src/commands/preview.mjs
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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')
Expand Down
18 changes: 16 additions & 2 deletions src/utils/wrangler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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) {
Expand Down

0 comments on commit eaab77b

Please sign in to comment.