patch-pulse
Version:
Check for outdated npm dependencies
64 lines • 2.28 kB
JavaScript
import chalk from 'chalk';
export function parseVersion(version) {
// Handle version ranges like ^3.3.1, ~3.3.1, >=3.3.1, etc.
const cleanVersion = version.replace(/^[\^~>=<]+/, '');
const match = cleanVersion.match(/^(\d+)\.(\d+)\.(\d+)/);
if (!match) {
// Instead of returning zeros, throw an error for invalid versions
throw new Error(`Invalid version format: ${version}`);
}
return {
major: parseInt(match[1], 10),
minor: parseInt(match[2], 10),
patch: parseInt(match[3], 10),
};
}
export function getUpdateType(current, latest) {
try {
const currentVersion = parseVersion(current);
const latestVersion = parseVersion(latest);
if (latestVersion.major > currentVersion.major) {
return 'major';
}
if (latestVersion.minor > currentVersion.minor) {
return 'minor';
}
if (latestVersion.patch > currentVersion.patch) {
return 'patch';
}
return 'patch';
}
catch (error) {
// Handle invalid version formats gracefully
console.warn(chalk.yellow(`⚠️ Invalid version format: ${error}`));
return 'patch'; // Default fallback
}
}
export function isVersionOutdated(current, latest) {
try {
const currentVersion = parseVersion(current);
const latestVersion = parseVersion(latest);
// Compare major versions first
if (latestVersion.major > currentVersion.major) {
return true;
}
if (latestVersion.major < currentVersion.major) {
return false;
}
// If major versions are equal, compare minor versions
if (latestVersion.minor > currentVersion.minor) {
return true;
}
if (latestVersion.minor < currentVersion.minor) {
return false;
}
// If minor versions are equal, compare patch versions
return latestVersion.patch > currentVersion.patch;
}
catch (error) {
// Handle invalid version formats gracefully
console.warn(chalk.yellow(`⚠️ Invalid version format: ${error}`));
return false; // Default to not outdated if we can't parse
}
}
//# sourceMappingURL=version.js.map