@@ -12,24 +12,22 @@ import { resolve } from 'pathe'
1212import type { WebSocket } from 'ws'
1313import { WebSocketServer } from 'ws'
1414import { listen , type Listener } from 'listhen'
15- import type { ModuleOptions } from '../types'
15+ import type { ModuleOptions , ResolvedCollection } from '../types'
1616import type { Manifest } from '../types/manifest'
17- import { generateCollectionInsert , parseSourceBase } from './collection'
17+ import { generateCollectionInsert } from './collection'
1818import { parseContent } from './content'
1919import { moduleTemplates } from './templates'
2020
2121export const logger : ConsolaInstance = useLogger ( '@nuxt/content' )
2222
2323export async function watchContents ( nuxt : Nuxt , options : ModuleOptions , manifest : Manifest ) {
24+ const cwd = join ( nuxt . options . rootDir , 'content' )
2425 const db = localDatabase ( options . _localDatabase ! . filename )
2526 const collections = manifest . collections
2627
2728 const localCollections = collections . filter ( c => c . source && ! c . source . repository )
2829
29- const watcher = chokidar . watch ( '.' , {
30- ignoreInitial : true ,
31- cwd : join ( nuxt . options . rootDir , 'content' ) ,
32- } )
30+ const watcher = chokidar . watch ( '.' , { ignoreInitial : true , cwd } )
3331
3432 watcher . on ( 'add' , onChange )
3533 watcher . on ( 'change' , onChange )
@@ -52,13 +50,46 @@ export async function watchContents(nuxt: Nuxt, options: ModuleOptions, manifest
5250 } )
5351 }
5452
53+ async function broadcast ( collection : ResolvedCollection , key : string , insertQuery ?: string ) {
54+ const removeQuery = `DELETE FROM ${ collection . tableName } WHERE _id = '${ key } '`
55+ await db . exec ( removeQuery )
56+ if ( insertQuery ) {
57+ await db . exec ( insertQuery )
58+ }
59+
60+ const index = manifest . dump [ collection . name ] ?. findIndex ( item => item . includes ( `'${ key } '` ) )
61+ if ( index && index !== - 1 ) {
62+ // Update templates to have valid dump for client-side navigation
63+ if ( insertQuery ) {
64+ manifest . dump [ collection . name ] ?. splice ( index , 1 , insertQuery )
65+ }
66+ else {
67+ manifest . dump [ collection . name ] ?. splice ( index , 1 )
68+ }
69+
70+ await updateTemplates ( {
71+ filter : template => [
72+ moduleTemplates . manifest ,
73+ moduleTemplates . fullCompressedDump ,
74+ // moduleTemplates.raw,
75+ ] . includes ( template . filename ) ,
76+ } )
77+ }
78+
79+ websocket ?. broadcast ( {
80+ key,
81+ collection : collection . name ,
82+ queries : insertQuery ? [ removeQuery , insertQuery ] : [ removeQuery ] ,
83+ } )
84+ }
85+
5586 async function onChange ( path : string ) {
56- const collection = localCollections . find ( ( { source } ) => micromatch . isMatch ( path , source ! . path , { ignore : source ! . ignore || [ ] , dot : true } ) )
87+ const collection = localCollections . find ( ( { source } ) => micromatch . isMatch ( path , source ! . include , { ignore : source ! . exclude || [ ] , dot : true } ) )
5788 if ( collection ) {
5889 logger . info ( `File \`${ path } \` changed on \`${ collection . name } \` collection` )
5990
60- const { fixed } = parseSourceBase ( collection . source ! )
61- const keyInCollection = join ( collection . name , collection . source ?. prefix || '' , path . replace ( fixed , '' ) )
91+ const filePath = join ( cwd , path ) . replace ( collection . source ! . cwd , '' )
92+ const keyInCollection = join ( collection . name , collection . source ?. prefix || '' , filePath )
6293
6394 const content = await readFile ( join ( nuxt . options . rootDir , 'content' , path ) , 'utf8' )
6495 const checksum = getContentChecksum ( content )
@@ -72,53 +103,24 @@ export async function watchContents(nuxt: Nuxt, options: ModuleOptions, manifest
72103
73104 const parsedContent = await parseContent ( keyInCollection , content , collection , nuxt )
74105
75- const insertQuery = generateCollectionInsert ( collection , parsedContent )
76- await db . exec ( `DELETE FROM ${ collection . tableName } WHERE _id = '${ keyInCollection } '` )
77- await db . exec ( insertQuery )
78-
79106 db . insertDevelopmentCache ( keyInCollection , checksum , JSON . stringify ( parsedContent ) )
80107
81- const index = manifest . dump [ collection . name ] . findIndex ( item => item . includes ( `'${ keyInCollection } '` ) )
82- if ( index !== - 1 ) {
83- // Update templates to have valid dump for client-side navigation
84- manifest . dump [ collection . name ] . splice ( index , 1 , insertQuery )
85- await updateTemplates ( {
86- filter : template => [
87- moduleTemplates . manifest ,
88- moduleTemplates . fullCompressedDump ,
89- // moduleTemplates.raw,
90- ] . includes ( template . filename ) ,
91- } )
92- }
93-
94- websocket ?. broadcast ( {
95- path,
96- collection : collection . name ,
97- queries : [
98- 'DELETE FROM ' + collection . tableName + ' WHERE _id = \'' + keyInCollection + '\'' ,
99- insertQuery ,
100- ] ,
101- } )
108+ const insertQuery = generateCollectionInsert ( collection , parsedContent )
109+ await broadcast ( collection , keyInCollection , insertQuery )
102110 }
103111 }
104112
105113 async function onRemove ( path : string ) {
106- const collection = localCollections . find ( ( { source } ) => micromatch . isMatch ( path , source ! . path , { ignore : source ! . ignore || [ ] , dot : true } ) )
114+ const collection = localCollections . find ( ( { source } ) => micromatch . isMatch ( path , source ! . include , { ignore : source ! . exclude || [ ] , dot : true } ) )
107115 if ( collection ) {
108116 logger . info ( `File \`${ path } \` removed from \`${ collection . name } \` collection` )
109117
110- const { fixed } = parseSourceBase ( collection . source ! )
111- const keyInCollection = join ( collection . name , collection . source ?. prefix || '' , path . replace ( fixed , '' ) )
118+ const filePath = join ( cwd , path ) . replace ( collection . source ! . cwd , '' )
119+ const keyInCollection = join ( collection . name , collection . source ?. prefix || '' , filePath )
112120
113- const updateQuery = `DELETE FROM ${ collection . tableName } WHERE _id = '${ keyInCollection } '`
114- await db . exec ( updateQuery )
115121 await db . deleteDevelopmentCache ( keyInCollection )
116122
117- websocket ?. broadcast ( {
118- path,
119- collection : collection . name ,
120- query : updateQuery ,
121- } )
123+ await broadcast ( collection , keyInCollection )
122124 }
123125 }
124126
0 commit comments