@@ -3,24 +3,24 @@ import fs from 'node:fs';
3
3
import { deepMerge } from '../object/deepMerge' ;
4
4
import { promises as fsPromises } from 'node:fs' ;
5
5
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
8
8
if ( fs . existsSync ( jsonfile ) ) {
9
9
json = JSON . parse ( fs . readFileSync ( jsonfile , { encoding :'utf-8' } ) . toString ( ) )
10
10
}
11
- json = deepMerge ( json , data )
11
+ json = deepMerge ( json , data ) as T
12
12
fs . writeFileSync ( jsonfile , JSON . stringify ( json , null , 4 ) )
13
13
return json
14
14
15
15
}
16
16
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 ;
19
19
if ( await fsPromises . stat ( jsonfile ) . then ( ( ) => true ) . catch ( ( ) => false ) ) {
20
20
const fileContent = await fsPromises . readFile ( jsonfile , { encoding : 'utf-8' } ) ;
21
21
json = JSON . parse ( fileContent . toString ( ) ) ;
22
22
}
23
- json = deepMerge ( json , data ) ;
23
+ json = deepMerge ( json , data ) as T
24
24
await fsPromises . writeFile ( jsonfile , JSON . stringify ( json , null , 4 ) ) ;
25
25
return json ;
26
26
}
0 commit comments