Skip to content

Commit

Permalink
fix: readJsonFile/writeJsonFile types
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Feb 26, 2025
1 parent 5e5aba1 commit cb6be81
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
14 changes: 7 additions & 7 deletions src/fs/readJsonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ import fs from 'node:fs'
import { deepMerge } from '../object/deepMerge';
import { promises as fsPromises } from 'node:fs';

export function readJsonFile(jsonfile: string,defaultValue?:Record<string,any>) {
let json = {}
export function readJsonFile<T extends Record<string,any> = Record<string,any>>(jsonfile: string,defaultValue?:Partial<T>):T | undefined {
let json:T = {} as T
if(fs.existsSync(jsonfile)){
json = JSON.parse(fs.readFileSync(jsonfile,{encoding:'utf-8'}).toString())
}
if(defaultValue){
json = deepMerge({},defaultValue,json)
json = deepMerge({},defaultValue,json as any) as T
}
return json
}

export async function readJsonFileAsync(jsonfile: string, defaultValue?: Record<string, any>) {
let json = {};
export async function readJsonFileAsync<T extends Record<string,any>= Record<string,any>>(jsonfile: string, defaultValue?: Partial<T>):Promise<T | undefined> {
let json = {} as T
if (await fsPromises.access(jsonfile).then(() => true).catch(() => false)) {
json = JSON.parse((await fsPromises.readFile(jsonfile, { encoding: 'utf-8' })).toString());
}
if (defaultValue) {
json = deepMerge({}, defaultValue, json);
json = deepMerge({}, defaultValue, json) as T
}
return json;
}
}
12 changes: 6 additions & 6 deletions src/fs/writeJsonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ import fs from 'node:fs';
import { deepMerge } from '../object/deepMerge';
import { promises as fsPromises } from 'node:fs';

export function writeJsonFile(jsonfile: string, data:Record<string,any>) {
let json = {}
export function writeJsonFile<T extends Record<string,any> = Record<string,any>>(jsonfile: string, data:T) {
let json = {} as T
if(fs.existsSync(jsonfile)){
json = JSON.parse(fs.readFileSync(jsonfile,{encoding:'utf-8'}).toString())
}
json = deepMerge(json,data)
json = deepMerge(json,data) as T
fs.writeFileSync(jsonfile,JSON.stringify(json,null,4))
return json

}

export async function writeJsonFileAsync(jsonfile: string, data: Record<string, any>) {
let json = {};
export async function writeJsonFileAsync<T extends Record<string,any> = Record<string,any>>(jsonfile: string, data: Record<string, any>):Promise<T | undefined> {
let json = {} as T;
if (await fsPromises.stat(jsonfile).then(() => true).catch(() => false)) {
const fileContent = await fsPromises.readFile(jsonfile, { encoding: 'utf-8' });
json = JSON.parse(fileContent.toString());
}
json = deepMerge(json, data);
json = deepMerge(json, data) as T
await fsPromises.writeFile(jsonfile, JSON.stringify(json, null, 4));
return json;
}

0 comments on commit cb6be81

Please sign in to comment.