depshield
Version:
Smart Dependency Analyzer & Optimizer - Find unused npm packages, reduce bundle size, and improve project health with AST-based detection.
37 lines (36 loc) • 1.1 kB
JavaScript
import fs from 'fs/promises';
import path from 'path';
import { walkFiles } from '../utils/file-walker.js';
import { loadConfig } from '../config/loader.js';
export class Scanner {
rootPath;
config;
constructor(options) {
this.rootPath = path.resolve(options.path);
this.config = options.config;
}
async readPackageJson() {
const packageJsonPath = path.join(this.rootPath, 'package.json');
try {
const content = await fs.readFile(packageJsonPath, 'utf-8');
return JSON.parse(content);
}
catch (error) {
throw new Error(`Could not read package.json at ${packageJsonPath}`);
}
}
async getSourceFiles() {
const config = this.config || await loadConfig(this.rootPath);
return walkFiles({
path: this.rootPath,
include: config.include,
exclude: config.exclude,
});
}
async getConfig() {
if (!this.config) {
this.config = await loadConfig(this.rootPath);
}
return this.config;
}
}