|
| 1 | +import { z } from 'zod' |
| 2 | +import type { Nuxt } from '@nuxt/schema' |
| 3 | +import type { DefinedCollection } from '../types' |
| 4 | +import { defineCollection } from './collection' |
| 5 | + |
| 6 | +interface StudioAIConfig { |
| 7 | + apiKey?: string |
| 8 | + context?: { |
| 9 | + title?: string |
| 10 | + description?: string |
| 11 | + tone?: string |
| 12 | + style?: string |
| 13 | + collection?: { |
| 14 | + name?: string |
| 15 | + folder?: string |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +const DEFAULT_STUDIO_COLLECTION_NAME = 'studio' |
| 21 | +const DEFAULT_STUDIO_COLLECTION_FOLDER = '.studio' |
| 22 | + |
| 23 | +/** |
| 24 | + * Resolves studio collection configuration when nuxt-studio is installed. |
| 25 | + * Automatically creates a studio collection and adds exclude patterns to other collections. |
| 26 | + */ |
| 27 | +export function resolveStudioCollection( |
| 28 | + nuxt: Nuxt, |
| 29 | + collectionsConfig: Record<string, DefinedCollection>, |
| 30 | +): void { |
| 31 | + /* @ts-expect-error - studio is not typed */ |
| 32 | + const studioAIConfig: StudioAIConfig = nuxt.options.studio?.ai || {} |
| 33 | + if (!studioAIConfig.apiKey) { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + const studioCollectionName = studioAIConfig.context?.collection?.name || DEFAULT_STUDIO_COLLECTION_NAME |
| 38 | + const studioFolder = studioAIConfig.context?.collection?.folder || DEFAULT_STUDIO_COLLECTION_FOLDER |
| 39 | + const studioPattern = `${studioFolder}/**` |
| 40 | + |
| 41 | + // Add studio collection if it doesn't exist |
| 42 | + if (!collectionsConfig[studioCollectionName]) { |
| 43 | + collectionsConfig[studioCollectionName] = defineCollection({ |
| 44 | + type: 'data', |
| 45 | + source: studioPattern, |
| 46 | + schema: z.object({ |
| 47 | + rawbody: z.string(), |
| 48 | + }), |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + // Add exclude pattern to all existing collections except studio |
| 53 | + for (const [name, collection] of Object.entries(collectionsConfig)) { |
| 54 | + if (name === studioCollectionName || !collection.source) { |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + // Add exclude pattern to each source |
| 59 | + for (const source of collection.source) { |
| 60 | + if (!source.exclude) { |
| 61 | + source.exclude = [] |
| 62 | + } |
| 63 | + if (!source.exclude.includes(studioPattern)) { |
| 64 | + source.exclude.push(studioPattern) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments