smartupdate/ts/smartupdate.classes.smartupdate.ts

108 lines
3.7 KiB
TypeScript
Raw Permalink Normal View History

2022-04-13 14:02:29 +00:00
import * as plugins from './smartupdate.plugins.js';
2019-09-06 11:58:17 +00:00
import { TimeStamp } from '@pushrocks/smarttime';
interface ICacheStatus {
lastCheck: number;
latestVersion: string;
performedUpgrade: boolean;
}
import { KeyValueStore } from '@pushrocks/npmextra';
export class SmartUpdate {
public npmRegistry: plugins.smartnpm.NpmRegistry;
public kvStore = new plugins.npmextra.KeyValueStore('custom', 'global_smartupdate');
constructor(optionsArg: plugins.smartnpm.INpmRegistryConstructorOptions = {}) {
this.npmRegistry = new plugins.smartnpm.NpmRegistry(optionsArg);
}
public async checkForCli(npmnameArg: string, compareVersion: string, changelogUrlArg?: string) {
// the newData to write
const timeStamp = new TimeStamp();
const newCacheData: ICacheStatus = {
lastCheck: timeStamp.milliSeconds,
latestVersion: 'x.x.x',
performedUpgrade: false
};
// the comparison data from the keyValue store
const retrievedCacheData: ICacheStatus = await this.kvStore.readKey(npmnameArg);
if (retrievedCacheData) {
const lastCheckTimeStamp = 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
}
}
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);
}
2020-06-25 20:10:43 +00:00
2019-09-06 11:58:17 +00:00
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;
}
public async check(
npmPackageName: string,
localVersionStringArg: string,
changelogUrlArg?: string
) {
const npmPackage = await this.getNpmPackageFromRegistry(npmPackageName);
if (!npmPackage) {
2020-06-25 20:08:47 +00:00
console.log('warn: failed to retrieve package information...');
console.log('info: is the registry down?');
2019-09-06 11:58:17 +00:00
return;
}
// 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 {
2020-06-25 20:10:43 +00:00
console.log(`warn: There is a newer version of ${npmPackage.name} available on npm.`);
2020-06-25 20:08:47 +00:00
console.log(
`warn: Your version: ${versionLocal.versionString} | version on npm: ${versionNpm.versionString}`
2019-09-06 11:58:17 +00:00
);
if (!process.env.CI && changelogUrlArg) {
console.log('trying to open changelog...');
plugins.smartopen.openUrl(changelogUrlArg);
}
return true;
}
}
}