webpack-license-plugin
Version:
Extracts OSS license information of the npm packages in your webpack output
24 lines (18 loc) • 628 B
text/typescript
import type IFileSystem from './types/IFileSystem'
import type IPackageJson from './types/IPackageJson'
import { join } from 'node:path'
interface IPackageJsonCache {
[moduleDir: string]: IPackageJson
}
export default class PackageJsonReader {
private cache: IPackageJsonCache = {}
constructor(private fileSystem: IFileSystem) {}
public readPackageJson(moduleDir: string): IPackageJson {
if (!this.cache[moduleDir]) {
const path = join(moduleDir, 'package.json')
const meta = JSON.parse(this.fileSystem.readFile(path))
this.cache[moduleDir] = meta
}
return this.cache[moduleDir]
}
}