@@ -227,3 +227,40 @@ export function getNewLineChars(source: string) {
227227 }
228228 return "\n" ;
229229}
230+
231+ export async function readJson < T > ( file : string ) : Promise < T > {
232+ const content = await fs . promises . readFile ( file , "utf-8" ) ;
233+ return JSON . parse ( content ) ;
234+ }
235+
236+ export interface PkgJson {
237+ name ?: string ;
238+ version ?: string ;
239+ license ?: string ;
240+
241+ dependencies ?: Record < string , string > ;
242+ devDependencies ?: Record < string , string > ;
243+ optionalDependencies ?: Record < string , string > ;
244+ exports ?: string | Record < string , string | Record < string , string > > ;
245+ scripts ?: Record < string , string > ;
246+ }
247+
248+ export async function writeJson < T > ( file : string , data : T ) : Promise < void > {
249+ try {
250+ await fs . promises . mkdir ( path . dirname ( file ) , { recursive : true } ) ;
251+ } catch ( _ ) { }
252+ await fs . promises . writeFile ( file , JSON . stringify ( data , null , 2 ) , "utf-8" ) ;
253+ }
254+
255+ export async function readTextFile ( file : string ) : Promise < string > {
256+ return fs . promises . readFile ( file , "utf-8" ) ;
257+ }
258+ export async function writeTextFile (
259+ file : string ,
260+ content : string ,
261+ ) : Promise < void > {
262+ try {
263+ await fs . promises . mkdir ( path . dirname ( file ) , { recursive : true } ) ;
264+ } catch ( _ ) { }
265+ await fs . promises . writeFile ( file , content , "utf-8" ) ;
266+ }
0 commit comments