Skip to content

Commit

Permalink
fix(source): do not edit source.include
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Nov 8, 2024
1 parent 2548b35 commit fa591ff
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { parseContent } from './utils/content'
import { installMDCModule } from './utils/mdc'
import { findPreset } from './presets'
import type { Manifest } from './types/manifest'
import { parseSourceBase } from './utils/source'

// Export public utils
export * from './utils'
Expand Down Expand Up @@ -239,6 +240,7 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
await collection.source.prepare(nuxt)
}

const { fixed } = parseSourceBase(collection.source)
const cwd = collection.source.cwd
const _keys = await fastGlob(collection.source.include, { cwd, ignore: collection.source!.exclude || [], dot: true })
.catch(() => [])
Expand All @@ -248,8 +250,10 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
const list: Array<Array<string>> = []
for await (const chunk of chunks(_keys, 25)) {
await Promise.all(chunk.map(async (key) => {
key = key.substring(fixed.length)
const keyInCollection = join(collection.name, collection.source?.prefix || '', key)
const content = await readFile(join(cwd, key), 'utf8')

const content = await readFile(join(cwd, fixed, key), 'utf8')
const checksum = getContentChecksum(configHash + collectionHash + content)
const cache = databaseContents[keyInCollection]

Expand Down
7 changes: 4 additions & 3 deletions src/runtime/internal/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export function useContentWebSocket() {

const db = await loadDatabaseAdapter(data.collection)

for (const s of data.queries) {
await db.exec(s).catch((err: unknown) => console.log(err))
}
await data.queries.reduce(async (prev: Promise<void>, sql: string) => {
await prev
await db.exec(sql).catch((err: unknown) => console.log(err))
}, Promise.resolve())

refreshNuxtData()
}
Expand Down
7 changes: 5 additions & 2 deletions src/utils/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { Manifest } from '../types/manifest'
import { generateCollectionInsert } from './collection'
import { parseContent } from './content'
import { moduleTemplates } from './templates'
import { parseSourceBase } from './source'

export const logger: ConsolaInstance = useLogger('@nuxt/content')

Expand Down Expand Up @@ -87,8 +88,9 @@ export async function watchContents(nuxt: Nuxt, options: ModuleOptions, manifest
const collection = localCollections.find(({ source }) => micromatch.isMatch(path, source!.include, { ignore: source!.exclude || [], dot: true }))
if (collection) {
logger.info(`File \`${path}\` changed on \`${collection.name}\` collection`)
const { fixed } = parseSourceBase(collection.source!)

const filePath = join(cwd, path).replace(collection.source!.cwd, '')
const filePath = path.substring(fixed.length)
const keyInCollection = join(collection.name, collection.source?.prefix || '', filePath)

const content = await readFile(join(nuxt.options.rootDir, 'content', path), 'utf8')
Expand All @@ -114,8 +116,9 @@ export async function watchContents(nuxt: Nuxt, options: ModuleOptions, manifest
const collection = localCollections.find(({ source }) => micromatch.isMatch(path, source!.include, { ignore: source!.exclude || [], dot: true }))
if (collection) {
logger.info(`File \`${path}\` removed from \`${collection.name}\` collection`)
const { fixed } = parseSourceBase(collection.source!)

const filePath = join(cwd, path).replace(collection.source!.cwd, '')
const filePath = path.substring(fixed.length)
const keyInCollection = join(collection.name, collection.source?.prefix || '', filePath)

await db.deleteDevelopmentCache(keyInCollection)
Expand Down
8 changes: 4 additions & 4 deletions src/utils/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import type { CollectionSource, ResolvedCollectionSource } from '../types/collec
import { downloadRepository, parseGitHubUrl } from './git'

export function defineLocalSource(source: CollectionSource): ResolvedCollectionSource {
const { fixed, dynamic } = parseSourceBase(source)
const { fixed } = parseSourceBase(source)
const resolvedSource: ResolvedCollectionSource = {
_resolved: true,
prefix: withoutTrailingSlash(withLeadingSlash(fixed)),
...source,
include: dynamic,
cwd: withoutTrailingSlash(join('~~/content/', fixed)),
include: source.include,
cwd: '',
prepare: async (nuxt) => {
resolvedSource.cwd = source.cwd
? String(source.cwd).replace(/^~~\//, nuxt.options.rootDir)
: join(nuxt.options.rootDir, 'content', fixed)
: join(nuxt.options.rootDir, 'content')
},
}
return resolvedSource
Expand Down

0 comments on commit fa591ff

Please sign in to comment.