@aquaori/deplens
Version:
A precise dependency analysis tool for npm and pnpm projects
109 lines • 5.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.analyzeProject = analyzeProject;
exports.displayResults = displayResults;
const cli_utils_1 = require("../utils/cli-utils");
const chalk_1 = __importDefault(require("chalk"));
const fs_1 = __importDefault(require("fs"));
const scanner_1 = require("../analyzer/scanner");
const parser_1 = require("../analyzer/parser");
const dependency_1 = require("../analyzer/dependency");
const cli_progress_1 = __importDefault(require("cli-progress"));
const logQueue_1 = require("../utils/logQueue");
/**
* 分析项目依赖的主要函数
* @param args 命令行参数对象
*/
async function analyzeProject(args) {
let bar1 = null;
if (!args.silence) {
bar1 = new cli_progress_1.default.SingleBar({
clearOnComplete: true,
hideCursor: true,
format: ' {bar} | {stepname} | {value}/{total}',
}, cli_progress_1.default.Presets.shades_classic);
bar1.start(4, 0, { stepname: 'Initializing...' });
}
const fileContentList = await (0, scanner_1.scan)(args);
if (!args.silence && bar1)
bar1.increment({ stepname: 'Scanning files' });
const astList = await (0, parser_1.parseAST)(fileContentList);
if (!args.silence && bar1)
bar1.increment({ stepname: 'Parsing AST' });
const [systemDeps, checkCount] = await (0, dependency_1.getDependencies)(args, 0);
await (0, dependency_1.parseDependencies)(astList, systemDeps);
if (!args.silence && bar1)
bar1.increment({ stepname: 'Analyzing dependencies' });
const summary = (0, dependency_1.summaryData)(systemDeps, checkCount);
if (!args.silence && bar1)
bar1.increment({ stepname: 'Summarizing results' });
if (!args.silence && bar1) {
bar1.stop();
(0, logQueue_1.spitOutQueue)();
}
(0, cli_utils_1.logSuccess)(` Dependencies checking Successfully`);
displayResults(summary, args);
}
/**
* 显示分析结果
* @param result 分析结果对象
* @param options 命令行选项
*/
function displayResults(result, options) {
console.log('\n' + chalk_1.default.bold.green('✨ Check Results:'));
console.log(chalk_1.default.gray('═'.repeat(50)));
(0, cli_utils_1.logSuccess)(` Dependency check completed successfully`);
(0, cli_utils_1.logSuccess)(` Analyzed ${result.totalDependencies} packages`);
if (result.ununsedDependenciesCount > 0) {
(0, cli_utils_1.logInfo)(` Found ${result.ununsedDependenciesCount} unused dependencies : `);
}
else {
(0, cli_utils_1.logSuccess)(` No unused dependencies found`);
}
let hasDynamic = false;
result.unusedDependencies.forEach(dep => {
if (dep.type !== 'dynamic' && !dep.args)
(0, cli_utils_1.logSecondary)(`\t- ${dep.name}${dep.version ? ` @${dep.version.join(' & @')}` : ''}`);
else
hasDynamic = true;
});
if (hasDynamic) {
(0, cli_utils_1.logInfo)(` Found ${result.unusedDependencies.filter(dep => dep.type === 'dynamic').length} dynamic imports that deplens cannot analyze: `);
result.unusedDependencies.filter(dep => dep.type === 'dynamic').forEach(dep => {
(0, cli_utils_1.logSecondary)(`\t- ${dep.args}`);
});
}
// 如果启用了详细输出,显示开发依赖信息
if (options.verbose) {
if (result.devDependencies.length > 0) {
(0, cli_utils_1.logInfo)(` Found ${result.devDependencies.length} dev dependencies that you maybe don't need them in stable environment : `);
}
else {
(0, cli_utils_1.logSuccess)(` No dev dependencies found`);
}
result.devDependencies.forEach(dep => {
(0, cli_utils_1.logSecondary)(`\t- ${dep.name}`);
});
}
console.log('\n' + chalk_1.default.bold.green('⚠️ Some extra info:'));
console.log(chalk_1.default.gray('═'.repeat(50)));
// 提示用户如何处理误报
if (result.ununsedDependenciesCount > 0 && options["config"] === "" && (options["ignoreDep"] === "" || options["ignorePath"] === "" || options["ignoreFile"] === "") && !fs_1.default.existsSync(`${options.path}/deplens.config.json`) && !options.silence) {
(0, cli_utils_1.logWarning)(` Due to workload reasons, Deplens cannot fully support all frameworks and plugins.`);
(0, cli_utils_1.logWarning)(` If there are false positives, please record them in [ deplens.config.json ] or \'--ignore\' option .`);
}
// 显示启用的选项信息
if (options.pnpm) {
(0, cli_utils_1.logInfo)(` PNPM support enabled`);
}
if (options.verbose) {
(0, cli_utils_1.logInfo)(` Verbose output enabled`);
}
else if (!options.silence) {
(0, cli_utils_1.logInfo)(` Run with --verbose for detailed output`);
}
}
//# sourceMappingURL=index.js.map