diff --git a/package.json b/package.json index e8aa00ce..e832b666 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "@sindresorhus/slugify": "^2.2.1", "@uploadthing/mime-types": "^0.2.9", "citty": "^0.1.6", + "confbox": "^0.1.7", "defu": "^6.1.4", "h3": "^1.11.1", "mime": "^4.0.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1aad8369..ead7f4bc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,6 +26,9 @@ importers: citty: specifier: ^0.1.6 version: 0.1.6 + confbox: + specifier: ^0.1.7 + version: 0.1.7 defu: specifier: ^6.1.4 version: 6.1.4 @@ -2776,9 +2779,6 @@ packages: concat-map@0.0.1: resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==} - confbox@0.1.3: - resolution: {integrity: sha512-eH3ZxAihl1PhKfpr4VfEN6/vUd87fmgb6JkldHgg/YR6aEBhW63qUDgzP2Y6WM0UumdsYp5H3kibalXAdHfbgg==} - confbox@0.1.7: resolution: {integrity: sha512-uJcB/FKZtBMCJpK8MQji6bJHgu1tixKPxRLeGkNzBoOZzpnZUJm0jm2/sBDWcuBx1dYgxV4JU+g5hmNxCyAmdA==} @@ -10018,7 +10018,7 @@ snapshots: c12@1.10.0: dependencies: chokidar: 3.6.0 - confbox: 0.1.3 + confbox: 0.1.7 defu: 6.1.4 dotenv: 16.4.5 giget: 1.2.1 @@ -10258,8 +10258,6 @@ snapshots: concat-map@0.0.1: {} - confbox@0.1.3: {} - confbox@0.1.7: {} consola@3.2.3: {} diff --git a/src/utils.ts b/src/utils.ts index bce3aa94..d6a3a0e5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,38 +1,49 @@ import { addCustomTab } from '@nuxt/devtools-kit' import type { Nuxt } from 'nuxt/schema' +import { stringifyTOML } from 'confbox' export function generateWrangler(hub: { kv: boolean, database: boolean, blob: boolean, cache: boolean, analytics: boolean }) { - return [ - hub.analytics - ? [ - 'analytics_engine_datasets = [', - ' { binding = "ANALYTICS", dataset = "default" }', - ']' - ] - : [], - hub.blob - ? [ - 'r2_buckets = [', - ' { binding = "BLOB", bucket_name = "default" }', - ']' - ] - : [], - hub.cache || hub.kv - ? [ - 'kv_namespaces = [', - hub.kv ? ' { binding = "KV", id = "kv_default" },' : '', - hub.cache ? ' { binding = "CACHE", id = "cache_default" },' : '', - ']' - ] - : [], - hub.database - ? [ - 'd1_databases = [', - ' { binding = "DB", database_name = "default", database_id = "default" }', - ']' - ] - : [] - ].flat().join('\n') + const wranger: { [key: string]: any } = {} + + if (hub.analytics) { + wranger['analytics_engine_datasets'] = [{ + binding: 'ANALYTICS', + dataset: 'default' + }] + } + + if (hub.blob) { + wranger['r2_buckets'] = [{ + binding: 'BLOB', + bucket_name: 'default' + }] + } + + if (hub.cache || hub.kv) { + wranger['kv_namespaces'] = [] + if (hub.kv) { + wranger['kv_namespaces'].push({ + binding: 'KV', + id: 'kv_default' + }) + } + if (hub.cache) { + wranger['kv_namespaces'].push({ + binding: 'CACHE', + id: 'cache_default' + }) + } + } + + if (hub.database) { + wranger['d1_databases'] = [{ + binding: 'DB', + database_name: 'default', + database_id: 'default' + }] + } + + return stringifyTOML(wranger) } export function addDevtoolsCustomTabs(nuxt: Nuxt, hub: { kv: boolean, database: boolean, blob: boolean, cache: boolean, analytics: boolean }) {