scanpack
Version:
Dependency scanner to detect unknown or malicious packages in Node.js and Bun projects
26 lines • 1.09 kB
JavaScript
import { join } from 'node:path';
import { PackageJsonParseError, PackageJsonReadError } from '../../domain/errors.js';
import { extractDependencies } from '../../domain/utils.js';
export class PackageReaderAdapter {
fileSystem;
constructor(fileSystem) {
this.fileSystem = fileSystem;
}
async readPackageJson(projectPath) {
try {
const packageJsonPath = join(projectPath, 'package.json');
const content = this.fileSystem.readFileSync(packageJsonPath);
return JSON.parse(content);
}
catch (error) {
if (error instanceof SyntaxError) {
throw new PackageJsonParseError(`Invalid JSON in package.json: ${error.message}`, error);
}
throw new PackageJsonReadError(`Error reading package.json: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : undefined);
}
}
extractDependencies(packageJson) {
return extractDependencies(packageJson);
}
}
//# sourceMappingURL=package-reader.adapter.js.map