diff --git a/src/index.ts b/src/index.ts index 0a95e5c..dca6eb0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ export * from './types' export * from './utils' export type ResolveOptions = _ResolveOptions & FindFileOptions +export type ReadOptions = { cache?: boolean | Map> } export function definePackageJSON (pkg: PackageJson): PackageJson { return pkg @@ -18,21 +19,35 @@ export function defineTSConfig (tsconfig: TSConfig): TSConfig { return tsconfig } -export async function readPackageJSON (id?: string, opts: ResolveOptions = {}): Promise { +const FileCache = new Map>() + +export async function readPackageJSON (id?: string, opts: ResolveOptions & ReadOptions = {}): Promise { const resolvedPath = await resolvePackageJSON(id, opts) + const cache = opts.cache && typeof opts.cache !== 'boolean' ? opts.cache : FileCache + if (opts.cache && cache.has(resolvedPath)) { + return cache.get(resolvedPath)! + } const blob = await fsp.readFile(resolvedPath, 'utf-8') - return JSON.parse(blob) as PackageJson + const parsed = JSON.parse(blob) as PackageJson + cache.set(resolvedPath, parsed) + return parsed } export async function writePackageJSON (path: string, pkg: PackageJson): Promise { await fsp.writeFile(path, JSON.stringify(pkg, null, 2)) } -export async function readTSConfig (id?: string, opts: ResolveOptions = {}): Promise { +export async function readTSConfig (id?: string, opts: ResolveOptions & ReadOptions = {}): Promise { const resolvedPath = await resolveTSConfig(id, opts) + const cache = opts.cache && typeof opts.cache !== 'boolean' ? opts.cache : FileCache + if (opts.cache && cache.has(resolvedPath)) { + return cache.get(resolvedPath)! + } const blob = await fsp.readFile(resolvedPath, 'utf-8') const jsonc = await import('jsonc-parser') - return jsonc.parse(blob) as TSConfig + const parsed = jsonc.parse(blob) as TSConfig + cache.set(resolvedPath, parsed) + return parsed } export async function writeTSConfig (path: string, tsconfig: TSConfig): Promise {