@@ -3,24 +3,24 @@ import fs from 'node:fs';
33import { deepMerge } from '../object/deepMerge' ;
44import { promises as fsPromises } from 'node:fs' ;
55
6- export function writeJsonFile ( jsonfile : string , data : Record < string , any > ) {
7- let json = { }
6+ export function writeJsonFile < T extends Record < string , any > = Record < string , any > > ( jsonfile : string , data : T ) {
7+ let json = { } as T
88 if ( fs . existsSync ( jsonfile ) ) {
99 json = JSON . parse ( fs . readFileSync ( jsonfile , { encoding :'utf-8' } ) . toString ( ) )
1010 }
11- json = deepMerge ( json , data )
11+ json = deepMerge ( json , data ) as T
1212 fs . writeFileSync ( jsonfile , JSON . stringify ( json , null , 4 ) )
1313 return json
1414
1515}
1616
17- export async function writeJsonFileAsync ( jsonfile : string , data : Record < string , any > ) {
18- let json = { } ;
17+ export async function writeJsonFileAsync < T extends Record < string , any > = Record < string , any > > ( jsonfile : string , data : Record < string , any > ) : Promise < T | undefined > {
18+ let json = { } as T ;
1919 if ( await fsPromises . stat ( jsonfile ) . then ( ( ) => true ) . catch ( ( ) => false ) ) {
2020 const fileContent = await fsPromises . readFile ( jsonfile , { encoding : 'utf-8' } ) ;
2121 json = JSON . parse ( fileContent . toString ( ) ) ;
2222 }
23- json = deepMerge ( json , data ) ;
23+ json = deepMerge ( json , data ) as T
2424 await fsPromises . writeFile ( jsonfile , JSON . stringify ( json , null , 4 ) ) ;
2525 return json ;
2626}
0 commit comments