This commit is contained in:
2025-11-03 16:02:12 +00:00
parent 4823eb9082
commit 1f2937b387
11 changed files with 15601 additions and 4350 deletions

View File

@@ -1,105 +1,279 @@
import * as plugins from './smartupdate.plugins.js';
import { UpdateCacheManager } from './smartupdate.classes.cachemanager.js';
import { UpdateNotifier } from './smartupdate.classes.notifier.js';
import { DEFAULT_CACHE_DURATION_HOURS, MILLISECONDS_PER_MINUTE, MINUTES_PER_HOUR } from './smartupdate.constants.js';
import type {
ISmartUpdateOptions,
IUpdateCheckOptions,
IUpdateCheckResult,
} from './smartupdate.interfaces.js';
import {
RegistryUnavailableError,
PackageNotFoundError,
} from './smartupdate.errors.js';
interface ICacheStatus {
lastCheck: number;
latestVersion: string;
performedUpgrade: boolean;
}
/**
* SmartUpdate - Elegant update checking for Node.js packages
*
* @example
* ```typescript
* const smartUpdate = new SmartUpdate();
* const result = await smartUpdate.checkForUpdate({
* packageName: 'lodash',
* currentVersion: '1.0.0',
* changelogUrl: 'https://example.com/changelog'
* });
*
* if (result.status === 'update-available') {
* console.log(`Update available: ${result.latestVersion}`);
* }
* ```
*/
export class SmartUpdate {
public npmRegistry: plugins.smartnpm.NpmRegistry;
public kvStore = new plugins.npmextra.KeyValueStore('custom', 'global_smartupdate');
public readonly npmRegistry: plugins.smartnpm.NpmRegistry;
private cacheManager: UpdateCacheManager;
private notifier: UpdateNotifier;
private options: ISmartUpdateOptions;
constructor(optionsArg: plugins.smartnpm.INpmRegistryConstructorOptions = {}) {
this.npmRegistry = new plugins.smartnpm.NpmRegistry(optionsArg);
/**
* @deprecated Use the options parameter instead of kvStore property
*/
public kvStore: plugins.npmextra.KeyValueStore;
constructor(options: ISmartUpdateOptions = {}) {
this.options = options;
// Initialize npm registry
this.npmRegistry = new plugins.smartnpm.NpmRegistry(options.npmRegistryOptions || {});
// Calculate cache duration in milliseconds
const cacheDuration = options.cacheDuration || { hours: DEFAULT_CACHE_DURATION_HOURS };
const cacheDurationMs =
(cacheDuration.hours || 0) * MINUTES_PER_HOUR * MILLISECONDS_PER_MINUTE +
(cacheDuration.minutes || 0) * MILLISECONDS_PER_MINUTE +
(cacheDuration.seconds || 0) * 1000;
// Initialize cache manager
this.cacheManager = new UpdateCacheManager({
durationMs: cacheDurationMs || DEFAULT_CACHE_DURATION_HOURS * MINUTES_PER_HOUR * MILLISECONDS_PER_MINUTE,
});
// Initialize notifier
this.notifier = new UpdateNotifier({
logLevel: options.logLevel || 'INFO',
useColors: !options.noColor,
customLogger: options.customLogger,
});
// Backward compatibility: expose kvStore
this.kvStore = (this.cacheManager as any).kvStore;
}
public async checkForCli(npmnameArg: string, compareVersion: string, changelogUrlArg?: string) {
// the newData to write
const timeStamp = new plugins.smarttime.TimeStamp();
const newCacheData: ICacheStatus = {
lastCheck: timeStamp.milliSeconds,
latestVersion: 'x.x.x',
performedUpgrade: false,
};
/**
* Check for updates with caching (primarily for CLI use)
*
* @deprecated Use checkForUpdate with cacheStrategy: 'always' instead
* @param packageName - The npm package name to check
* @param currentVersion - The current version to compare against
* @param changelogUrl - Optional URL to open if update is available
* @returns Promise resolving to true if update available, false otherwise
*
* @example
* ```typescript
* const hasUpdate = await smartUpdate.checkForCli('my-cli', '1.0.0', 'https://changelog.url');
* ```
*/
public async checkForCli(
packageName: string,
currentVersion: string,
changelogUrl?: string
): Promise<boolean> {
const result = await this.checkForUpdate({
packageName,
currentVersion,
changelogUrl,
cacheStrategy: 'always',
});
// the comparison data from the keyValue store
const retrievedCacheData: ICacheStatus = await this.kvStore.readKey(npmnameArg);
return result.status === 'update-available';
}
if (retrievedCacheData) {
const lastCheckTimeStamp = plugins.smarttime.TimeStamp.fromMilliSeconds(
retrievedCacheData.lastCheck
);
const tresholdTime = plugins.smarttime.getMilliSecondsFromUnits({ hours: 1 });
if (!lastCheckTimeStamp.isOlderThan(timeStamp, tresholdTime)) {
newCacheData.lastCheck = lastCheckTimeStamp.milliSeconds;
const nextCheckInMinutes =
(tresholdTime - (timeStamp.milliSeconds - lastCheckTimeStamp.milliSeconds)) / 60000;
console.log(
`next update check in less than ${Math.floor(nextCheckInMinutes) + 1} minute(s): ` +
`${plugins.consolecolor.coloredString(
`${npmnameArg} has already been checked within the last hour.`,
'pink'
)}`
);
return false; // don't upgrade if checked within reasonable time
/**
* Check for updates (modern API)
*
* @param options - Update check options
* @returns Promise resolving to detailed update check result
*
* @example
* ```typescript
* const result = await smartUpdate.checkForUpdate({
* packageName: 'lodash',
* currentVersion: '1.0.0',
* changelogUrl: 'https://changelog.url',
* openChangelog: true,
* cacheStrategy: 'time-based'
* });
*
* console.log(result.status); // 'up-to-date' | 'update-available' | 'check-skipped' | 'error'
* ```
*/
public async checkForUpdate(options: IUpdateCheckOptions): Promise<IUpdateCheckResult> {
const {
packageName,
currentVersion,
changelogUrl,
openChangelog = true,
cacheStrategy = 'time-based',
} = options;
const checkTime = new Date();
try {
// Check if we should use cache or check registry
const cacheCheck = await this.cacheManager.shouldCheckRegistry(packageName, cacheStrategy);
// If we should skip the check due to cache
if (!cacheCheck.shouldCheck) {
return {
status: 'check-skipped',
packageName,
currentVersion,
latestVersion: cacheCheck.cacheStatus?.latestVersion,
checkTime,
cacheHit: true,
nextCheckTime: cacheCheck.nextCheckTime,
reason: 'Rate limited - checked recently',
};
}
}
const upgradeBool = await this.check(npmnameArg, compareVersion, changelogUrlArg);
if (upgradeBool) {
const npmPackage = await this.npmRegistry.getPackageInfo(npmnameArg);
newCacheData.latestVersion = npmPackage.version;
this.kvStore.writeKey(npmnameArg, newCacheData);
}
// Fetch package info from registry
const npmPackage = await this.getNpmPackageFromRegistry(packageName);
return upgradeBool;
}
private async getNpmPackageFromRegistry(npmnameArg): Promise<plugins.smartnpm.NpmPackage> {
console.log(
`smartupdate: checking for newer version of ${plugins.consolecolor.coloredString(
npmnameArg,
'pink'
)}...`
);
const npmPackage = this.npmRegistry.getPackageInfo(npmnameArg);
return npmPackage;
if (!npmPackage) {
throw new RegistryUnavailableError();
}
// Compare versions
const versionNpm = new plugins.smartversion.SmartVersion(npmPackage.version);
const versionLocal = new plugins.smartversion.SmartVersion(currentVersion);
const result: IUpdateCheckResult = {
status: versionNpm.greaterThan(versionLocal) ? 'update-available' : 'up-to-date',
packageName: npmPackage.name,
currentVersion,
latestVersion: npmPackage.version,
checkTime,
cacheHit: false,
};
// Notify user
this.notifier.notifyUpdateCheckResult(result);
// If update is available, handle changelog
if (result.status === 'update-available' && changelogUrl && openChangelog && !process.env.CI) {
this.notifier.notifyOpeningChangelog();
plugins.smartopen.openUrl(changelogUrl);
}
// Update cache if there's an update
if (result.status === 'update-available') {
const cacheStatus = this.cacheManager.createCacheStatus(npmPackage.version, false);
await this.cacheManager.setCached(packageName, cacheStatus);
}
return result;
} catch (error) {
if (error instanceof RegistryUnavailableError) {
this.notifier.notifyRegistryError();
} else {
this.notifier.error(error instanceof Error ? error.message : String(error));
}
return {
status: 'error',
packageName,
currentVersion,
checkTime,
cacheHit: false,
error: error instanceof Error ? error : new Error(String(error)),
};
}
}
/**
* Simple check for updates without caching
*
* @deprecated Use checkForUpdate with cacheStrategy: 'never' instead
* @param packageName - The npm package name to check
* @param currentVersion - The current version to compare against
* @param changelogUrl - Optional URL to open if update is available
* @returns Promise resolving to true if update available, false otherwise
*
* @example
* ```typescript
* const hasUpdate = await smartUpdate.check('lodash', '1.0.0');
* ```
*/
public async check(
npmPackageName: string,
localVersionStringArg: string,
changelogUrlArg?: string
) {
const npmPackage = await this.getNpmPackageFromRegistry(npmPackageName);
if (!npmPackage) {
console.log('warn: failed to retrieve package information...');
console.log('info: is the registry down?');
return;
}
packageName: string,
currentVersion: string,
changelogUrl?: string
): Promise<boolean> {
const result = await this.checkForUpdate({
packageName,
currentVersion,
changelogUrl,
cacheStrategy: 'never',
});
// create Version objects
const versionNpm = new plugins.smartversion.SmartVersion(npmPackage.version);
const versionLocal = new plugins.smartversion.SmartVersion(localVersionStringArg);
if (!versionNpm.greaterThan(versionLocal)) {
console.log(
`smartupdate: You are running the latest version of ${plugins.consolecolor.coloredString(
npmPackage.name,
'pink'
)}`
);
return false;
} else {
console.log(`warn: There is a newer version of ${npmPackage.name} available on npm.`);
console.log(
`warn: Your version: ${versionLocal.versionString} | version on npm: ${versionNpm.versionString}`
);
if (!process.env.CI && changelogUrlArg) {
console.log('trying to open changelog...');
plugins.smartopen.openUrl(changelogUrlArg);
}
return true;
return result.status === 'update-available';
}
/**
* Get the latest version of a package
*
* @param packageName - The npm package name
* @returns Promise resolving to the latest version string
*
* @example
* ```typescript
* const latestVersion = await smartUpdate.getLatestVersion('lodash');
* console.log(latestVersion); // e.g., "4.17.21"
* ```
*/
public async getLatestVersion(packageName: string): Promise<string> {
const npmPackage = await this.getNpmPackageFromRegistry(packageName);
if (!npmPackage) {
throw new PackageNotFoundError(packageName);
}
return npmPackage.version;
}
/**
* Clear the cache for a specific package
*
* @param packageName - The package name to clear cache for
*
* @example
* ```typescript
* await smartUpdate.clearCache('lodash');
* ```
*/
public async clearCache(packageName: string): Promise<void> {
await this.cacheManager.clearCache(packageName);
}
/**
* Fetch package information from the npm registry
* @private
*/
private async getNpmPackageFromRegistry(packageName: string): Promise<plugins.smartnpm.NpmPackage | null> {
this.notifier.notifyCheckingForUpdate(packageName);
try {
const npmPackage = await this.npmRegistry.getPackageInfo(packageName);
return npmPackage;
} catch (error) {
return null;
}
}
}