2018-05-22 13:49:26 +00:00
|
|
|
import * as plugins from './smartupdate.plugins';
|
2017-08-15 17:29:18 +00:00
|
|
|
|
2018-09-02 10:00:52 +00:00
|
|
|
import { TimeStamp } from '@pushrocks/smarttime';
|
2017-08-16 16:54:23 +00:00
|
|
|
|
2017-08-15 17:29:18 +00:00
|
|
|
interface ICacheStatus {
|
2018-05-22 13:49:26 +00:00
|
|
|
lastCheck: number;
|
|
|
|
latestVersion: string;
|
|
|
|
performedUpgrade: boolean;
|
2017-08-15 17:29:18 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 10:00:52 +00:00
|
|
|
import { KeyValueStore } from '@pushrocks/npmextra';
|
2017-08-16 16:54:23 +00:00
|
|
|
|
|
|
|
export class SmartUpdate {
|
2019-09-04 14:38:06 +00:00
|
|
|
public kvStore = new plugins.npmextra.KeyValueStore('custom', 'global_smartupdate');
|
2017-08-15 17:29:18 +00:00
|
|
|
|
2019-09-04 14:38:06 +00:00
|
|
|
public async check(npmnameArg: string, compareVersion: string, changelogUrlArg?: string) {
|
2017-08-16 16:54:23 +00:00
|
|
|
// the newData to write
|
2019-09-04 14:38:06 +00:00
|
|
|
const timeStamp = new TimeStamp();
|
|
|
|
const newData = {
|
2017-08-16 16:54:23 +00:00
|
|
|
lastCheck: timeStamp.milliSeconds,
|
|
|
|
latestVersion: 'x.x.x',
|
|
|
|
performedUpgrade: false
|
2018-05-22 13:49:26 +00:00
|
|
|
};
|
2017-08-19 22:09:26 +00:00
|
|
|
|
|
|
|
// the comparison data from the keyValue store
|
2019-09-04 14:38:06 +00:00
|
|
|
const result: ICacheStatus = await this.kvStore.readKey(npmnameArg);
|
2017-08-19 22:09:26 +00:00
|
|
|
|
2017-08-16 16:54:23 +00:00
|
|
|
if (result) {
|
2019-09-04 14:38:06 +00:00
|
|
|
const lastCheckTimeStamp = TimeStamp.fromMilliSeconds(result.lastCheck);
|
|
|
|
const tresholdTime = plugins.smarttime.getMilliSecondsFromUnits({ hours: 1 });
|
2017-08-19 22:45:45 +00:00
|
|
|
if (!lastCheckTimeStamp.isOlderThan(timeStamp, tresholdTime)) {
|
2018-05-22 13:49:26 +00:00
|
|
|
newData.lastCheck = lastCheckTimeStamp.milliSeconds;
|
2019-09-04 14:38:06 +00:00
|
|
|
const nextCheckInMinutes =
|
2018-05-22 13:49:26 +00:00
|
|
|
(tresholdTime - (timeStamp.milliSeconds - lastCheckTimeStamp.milliSeconds)) / 60000;
|
2018-09-02 10:00:52 +00:00
|
|
|
console.log(
|
2017-08-20 09:28:11 +00:00
|
|
|
`next update check in less than ${Math.floor(nextCheckInMinutes) + 1} minute(s): ` +
|
2018-09-02 10:00:52 +00:00
|
|
|
`${plugins.consolecolor.coloredString(
|
2018-05-22 13:49:26 +00:00
|
|
|
`${npmnameArg} has already been checked within the last hour.`,
|
|
|
|
'pink'
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
return;
|
2017-08-16 16:54:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-04 14:38:06 +00:00
|
|
|
const npmPackage = await this.getNpmPackageFromRegistry(npmnameArg);
|
2017-09-11 12:13:37 +00:00
|
|
|
if (!npmPackage) {
|
2018-11-08 08:54:06 +00:00
|
|
|
plugins.smartlog.defaultLogger.log('warn', 'failed to retrieve package information...');
|
|
|
|
plugins.smartlog.defaultLogger.log('info', 'npms.io might be down');
|
2018-05-22 13:49:26 +00:00
|
|
|
return;
|
2017-09-11 12:13:37 +00:00
|
|
|
}
|
2018-05-22 13:49:26 +00:00
|
|
|
newData.latestVersion = npmPackage.version;
|
2019-09-04 14:38:06 +00:00
|
|
|
const upgradeBool = await this.checkIfUpgrade(npmPackage, compareVersion, changelogUrlArg);
|
2017-08-19 10:32:24 +00:00
|
|
|
if (upgradeBool) {
|
2017-09-11 12:13:37 +00:00
|
|
|
// TODO:
|
2017-08-16 16:54:23 +00:00
|
|
|
}
|
2018-05-22 13:49:26 +00:00
|
|
|
this.kvStore.writeKey(npmnameArg, newData);
|
2017-08-16 16:54:23 +00:00
|
|
|
}
|
2017-08-15 17:29:18 +00:00
|
|
|
|
2018-05-22 13:49:26 +00:00
|
|
|
private async getNpmPackageFromRegistry(npmnameArg): Promise<plugins.smartnpm.NpmPackage> {
|
2018-09-02 10:00:52 +00:00
|
|
|
console.log(
|
|
|
|
`smartupdate: checking for newer version of ${plugins.consolecolor.coloredString(
|
2018-05-22 13:49:26 +00:00
|
|
|
npmnameArg,
|
|
|
|
'pink'
|
|
|
|
)}...`
|
|
|
|
);
|
2019-09-04 14:38:06 +00:00
|
|
|
const npmRegistry = new plugins.smartnpm.NpmRegistry();
|
2019-09-06 11:39:39 +00:00
|
|
|
const npmPackage = npmRegistry.getPackageInfo(npmnameArg);
|
2018-05-22 13:49:26 +00:00
|
|
|
return npmPackage;
|
2017-08-16 16:54:23 +00:00
|
|
|
}
|
2017-08-15 17:29:18 +00:00
|
|
|
|
2018-05-22 13:49:26 +00:00
|
|
|
private async checkIfUpgrade(
|
2017-08-17 09:56:38 +00:00
|
|
|
npmPackage: plugins.smartnpm.NpmPackage,
|
2017-08-18 10:52:11 +00:00
|
|
|
localVersionStringArg: string,
|
2017-08-17 09:56:38 +00:00
|
|
|
changelogUrlArg?: string
|
|
|
|
) {
|
2017-08-18 10:52:11 +00:00
|
|
|
// create Version objects
|
2019-09-04 14:38:06 +00:00
|
|
|
const versionNpm = new plugins.smartversion.SmartVersion(npmPackage.version);
|
|
|
|
const versionLocal = new plugins.smartversion.SmartVersion(localVersionStringArg);
|
2017-08-18 10:52:24 +00:00
|
|
|
if (!versionNpm.greaterThan(versionLocal)) {
|
2018-09-02 10:00:52 +00:00
|
|
|
console.log(
|
|
|
|
`smartupdate: You are running the latest version of ${plugins.consolecolor.coloredString(
|
2018-05-22 13:49:26 +00:00
|
|
|
npmPackage.name,
|
|
|
|
'pink'
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
return false;
|
2017-08-16 16:54:23 +00:00
|
|
|
} else {
|
2018-11-08 08:54:06 +00:00
|
|
|
plugins.smartlog.defaultLogger.log(
|
|
|
|
'warn',
|
2018-09-02 10:21:55 +00:00
|
|
|
`There is a newer version of ${npmPackage.name} available on npm.`
|
|
|
|
);
|
2018-11-08 08:54:06 +00:00
|
|
|
plugins.smartlog.defaultLogger.log(
|
|
|
|
'warn',
|
2018-05-22 13:49:26 +00:00
|
|
|
`Your version: ${versionLocal.versionString} | version on npm: ${versionNpm.versionString}`
|
|
|
|
);
|
2017-08-17 09:56:38 +00:00
|
|
|
if (!process.env.CI && changelogUrlArg) {
|
2018-09-02 10:00:52 +00:00
|
|
|
console.log('trying to open changelog...');
|
2018-05-22 13:49:26 +00:00
|
|
|
plugins.smartopen.openUrl(changelogUrlArg);
|
2017-08-17 09:56:38 +00:00
|
|
|
}
|
2018-05-22 13:49:26 +00:00
|
|
|
return true;
|
2017-08-16 16:54:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-22 13:49:26 +00:00
|
|
|
export let standardHandler = new SmartUpdate();
|