import * as jsonc from 'jsonc-parser' ; import { promises as fsp } from 'fs' ; import { findNearestFile , FindNearestFileOptions } from './utils' ; import { ResolveOptions as _ResolveOptions , resolvePath } from 'mlly' ; import type { PackageJson , TSConfig } from './types' ; import { isAbsolute } from 'pathe' ; export * from './types' ; export * from './utils' ; export type ResolveOptions = _ResolveOptions & FindNearestFileOptions ; export function definePackageJSON ( pkg ) { return pkg ; } export function defineTSConfig ( tsconfig ) { return tsconfig ; } export async function readPackageJSON ( id , opts = { } ) { const resolvedPath = await resolvePackageJSON ( id , opts ) ; const blob = await fsp . readFile ( resolvedPath , 'utf-8' ) ; return JSON . parse ( blob ) as PackageJson ; } export async function writePackageJSON ( path , pkg ) { await fsp . writeFile ( path , JSON . stringify ( pkg , null , 2 ) ) ; } export async function readTSConfig ( id , opts = { } ) { const resolvedPath = await resolveTSConfig ( id , opts ) ; const blob = await fsp . readFile ( resolvedPath , 'utf-8' ) ; return jsonc . parse ( blob ) as TSConfig ; } export async function writeTSConfig ( path , tsconfig ) { await fsp . writeFile ( path , JSON . stringify ( tsconfig , null , 2 ) ) ; } export async function resolvePackageJSON ( id = process . cwd ( ) , opts = { } ) { const resolvedPath = isAbsolute ( id ) ? id : await resolvePath ( id , opts ) ; return findNearestFile ( 'package.json' , { startingFrom : resolvedPath , ... opts } ) ; } export async function resolveTSConfig ( id = process . cwd ( ) , opts = { } ) { const resolvedPath = isAbsolute ( id ) ? id : await resolvePath ( id , opts ) ; return findNearestFile ( 'tsconfig.json' , { startingFrom : resolvedPath , ... opts } ) ; }