UNPKG

depguard

Version:

A powerful CLI tool to check and update npm/yarn dependencies in your projects

98 lines (97 loc) 4.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Check = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const semver_1 = __importDefault(require("semver")); const chalk_1 = __importDefault(require("chalk")); const config_1 = require("./config/config"); const factory_1 = require("./package-managers/factory"); class Check { constructor() { this.config = new config_1.Config(); // Initialize package manager synchronously this.packageManager = factory_1.PackageManagerFactory.createSync(); } async checkUpdates() { const { exclude } = this.config.loadConfig(); const pkgPath = path_1.default.resolve(process.cwd(), 'package.json'); const pkg = JSON.parse(fs_1.default.readFileSync(pkgPath, 'utf-8')); const deps = { ...pkg.dependencies, ...pkg.devDependencies }; console.log(chalk_1.default.blue(`\nChecking for outdated dependencies using ${this.packageManager.name}...\n`)); const updates = { major: [], minor: [], patch: [], compatible: [], failed: [] }; for (const [name, currentRange] of Object.entries(deps)) { if (exclude.includes(name)) continue; const latest = await this.packageManager.getLatestVersion(name); if (!latest) { updates.failed.push(name); continue; } const current = currentRange.replace(/^[~^]/, ''); const isCompatible = semver_1.default.satisfies(latest, currentRange); if (isCompatible) { updates.compatible.push({ name, current: currentRange, latest }); } else { const currentMajor = semver_1.default.major(current); const latestMajor = semver_1.default.major(latest); const currentMinor = semver_1.default.minor(current); const latestMinor = semver_1.default.minor(latest); const currentPatch = semver_1.default.patch(current); const latestPatch = semver_1.default.patch(latest); if (latestMajor > currentMajor) { updates.major.push({ name, current: currentRange, latest }); } else if (latestMinor > currentMinor) { updates.minor.push({ name, current: currentRange, latest }); } else if (latestPatch > currentPatch) { updates.patch.push({ name, current: currentRange, latest }); } } } // Print results if (updates.major.length > 0) { console.log(chalk_1.default.yellow('\n⚠ Major Updates:')); updates.major.forEach(({ name, current, latest }) => { console.log(`${chalk_1.default.bold(name.padEnd(20))} ${current.padEnd(10)}${chalk_1.default.bold(latest.padEnd(10))}`); }); } if (updates.minor.length > 0) { console.log(chalk_1.default.cyan('\n↑ Minor Updates:')); updates.minor.forEach(({ name, current, latest }) => { console.log(`${chalk_1.default.bold(name.padEnd(20))} ${current.padEnd(10)}${chalk_1.default.bold(latest.padEnd(10))}`); }); } if (updates.patch.length > 0) { console.log(chalk_1.default.green('\n↑ Patch Updates:')); updates.patch.forEach(({ name, current, latest }) => { console.log(`${chalk_1.default.bold(name.padEnd(20))} ${current.padEnd(10)}${chalk_1.default.bold(latest.padEnd(10))}`); }); } if (updates.compatible.length > 0) { console.log(chalk_1.default.gray('\n✓ Compatible:')); updates.compatible.forEach(({ name, current, latest }) => { console.log(`${chalk_1.default.bold(name.padEnd(20))} ${current.padEnd(10)}${chalk_1.default.bold(latest.padEnd(10))}`); }); } if (updates.failed.length > 0) { console.log(chalk_1.default.red('\n✗ Failed to fetch:')); updates.failed.forEach(name => { console.log(chalk_1.default.gray(name)); }); } console.log(''); } } exports.Check = Check;