22 * Data Writer
33 *
44 * Writes synced data back to local filesystem.
5- * Supports both blob-based overwrite and per-item merge writes.
5+ * All categories use per-item writes.
66 */
77
88import { mkdir , writeFile , unlink } from 'node:fs/promises' ;
99import { join , dirname } from 'node:path' ;
1010import type { PathConfig , SyncCategory } from '../types/index.js' ;
1111import { getCategoryPaths } from '../types/paths.js' ;
1212import type { CategoryData , ItemCategoryData } from '../sync/operations/types.js' ;
13- import { isBlobCategoryData , isItemCategoryData } from '../sync/operations/types.js' ;
1413
1514/**
1615 * Write synced data back to local filesystem.
17- * - Blob categories: overwrites local data
18- * - Item categories: merges new items (does NOT overwrite existing)
16+ * All categories use per-item writes.
1917 */
2018export async function writeLocalData (
2119 pathConfig : PathConfig ,
@@ -27,28 +25,8 @@ export async function writeLocalData(
2725 const paths = categoryPaths [ catData . category ] ;
2826 if ( paths . length === 0 ) continue ;
2927
30- if ( isItemCategoryData ( catData ) ) {
31- // Per-item merge write
32- await writeItemCategoryData ( catData . category , paths , catData ) ;
33- } else if ( isBlobCategoryData ( catData ) ) {
34- // Blob overwrite
35- const parsed = JSON . parse ( catData . data ) as Record < string , unknown > ;
36- await writeBlobCategoryData ( paths , parsed ) ;
37- }
38- }
39- }
40-
41- /**
42- * Write blob-based category data to filesystem (overwrites).
43- */
44- async function writeBlobCategoryData (
45- paths : string [ ] ,
46- data : Record < string , unknown >
47- ) : Promise < void > {
48- for ( const [ key , value ] of Object . entries ( data ) ) {
49- const targetPath = findTargetPath ( paths , key ) ;
50- if ( targetPath === undefined ) continue ;
51- await writeEntry ( targetPath , value ) ;
28+ // Per-item write
29+ await writeItemCategoryData ( catData . category , paths , catData ) ;
5230 }
5331}
5432
@@ -99,67 +77,6 @@ async function writeItemFile(filePath: string, content: string): Promise<void> {
9977 }
10078}
10179
102- /**
103- * Find target path for a key.
104- */
105- function findTargetPath ( paths : string [ ] , key : string ) : string | undefined {
106- return paths . find ( ( p ) => p . endsWith ( key ) ) ?? paths [ 0 ] ;
107- }
108-
109- /**
110- * Write a single entry (file or directory).
111- */
112- async function writeEntry ( targetPath : string , value : unknown ) : Promise < void > {
113- try {
114- if ( isDirectoryValue ( value ) ) {
115- await mkdir ( targetPath , { recursive : true } ) ;
116- await writeDirectoryData ( targetPath , value as Record < string , unknown > ) ;
117- } else {
118- await ensureParentDir ( targetPath ) ;
119- const content = serializeContent ( targetPath , value ) ;
120- await writeFile ( targetPath , content , 'utf-8' ) ;
121- }
122- } catch ( error ) {
123- console . error ( `Failed to write ${ targetPath } :` , error ) ;
124- }
125- }
126-
127- /**
128- * Check if value represents a directory.
129- */
130- function isDirectoryValue ( value : unknown ) : boolean {
131- return typeof value === 'object' && value !== null && ! Array . isArray ( value ) ;
132- }
133-
134- /**
135- * Write directory contents recursively.
136- */
137- async function writeDirectoryData ( dirPath : string , data : Record < string , unknown > ) : Promise < void > {
138- for ( const [ name , value ] of Object . entries ( data ) ) {
139- const fullPath = join ( dirPath , name ) ;
140- await writeEntry ( fullPath , value ) ;
141- }
142- }
143-
144- /**
145- * Serialize content based on file type.
146- */
147- function serializeContent ( filePath : string , value : unknown ) : string {
148- if ( filePath . endsWith ( '.json' ) ) {
149- return JSON . stringify ( value , null , 2 ) ;
150- }
151-
152- if ( filePath . endsWith ( '.jsonl' ) && Array . isArray ( value ) ) {
153- return value . map ( ( item ) => JSON . stringify ( item ) ) . join ( '\n' ) ;
154- }
155-
156- if ( typeof value === 'string' ) {
157- return value ;
158- }
159-
160- return JSON . stringify ( value , null , 2 ) ;
161- }
162-
16380/**
16481 * Ensure parent directory exists.
16582 */
0 commit comments