fix(package): update smartopen to @pushrocks/smartopen

This commit is contained in:
2018-05-22 15:49:26 +02:00
parent 56d2c3f33f
commit bf3815c74d
12 changed files with 316 additions and 1138 deletions

View File

@ -1,88 +1,101 @@
import * as plugins from './smartupdate.plugins'
import * as plugins from './smartupdate.plugins';
import { TimeStamp } from 'smarttime'
import { TimeStamp } from 'smarttime';
interface ICacheStatus {
lastCheck: number
latestVersion: string
performedUpgrade: boolean
lastCheck: number;
latestVersion: string;
performedUpgrade: boolean;
}
import { KeyValueStore } from 'npmextra'
import { KeyValueStore } from 'npmextra';
export class SmartUpdate {
kvStore = new plugins.npmextra.KeyValueStore('custom', 'global_smartupdate')
kvStore = new plugins.npmextra.KeyValueStore('custom', 'global_smartupdate');
async check (npmnameArg: string, compareVersion: string, changelogUrlArg?: string) {
async check(npmnameArg: string, compareVersion: string, changelogUrlArg?: string) {
// the newData to write
let timeStamp = new TimeStamp()
let timeStamp = new TimeStamp();
let newData = {
lastCheck: timeStamp.milliSeconds,
latestVersion: 'x.x.x',
performedUpgrade: false
}
};
// the comparison data from the keyValue store
let result: ICacheStatus = await this.kvStore.readKey(npmnameArg)
let result: ICacheStatus = await this.kvStore.readKey(npmnameArg);
if (result) {
let lastCheckTimeStamp = TimeStamp.fromMilliSeconds(result.lastCheck)
let tresholdTime = plugins.smarttime.getMilliSecondsFromUnits({ hours: 1 })
let lastCheckTimeStamp = TimeStamp.fromMilliSeconds(result.lastCheck);
let tresholdTime = plugins.smarttime.getMilliSecondsFromUnits({ hours: 1 });
if (!lastCheckTimeStamp.isOlderThan(timeStamp, tresholdTime)) {
newData.lastCheck = lastCheckTimeStamp.milliSeconds
let nextCheckInMinutes = (tresholdTime - (timeStamp.milliSeconds - lastCheckTimeStamp.milliSeconds)) / 60000
newData.lastCheck = lastCheckTimeStamp.milliSeconds;
let nextCheckInMinutes =
(tresholdTime - (timeStamp.milliSeconds - lastCheckTimeStamp.milliSeconds)) / 60000;
plugins.beautylog.log(
`next update check in less than ${Math.floor(nextCheckInMinutes) + 1} minute(s): ` +
`${plugins.beautycolor.coloredString(
`${npmnameArg} has already been checked within the last hour.`
, 'pink'
)}`
)
return
`${plugins.beautycolor.coloredString(
`${npmnameArg} has already been checked within the last hour.`,
'pink'
)}`
);
return;
}
}
let npmPackage = await this.getNpmPackageFromRegistry(npmnameArg)
let npmPackage = await this.getNpmPackageFromRegistry(npmnameArg);
if (!npmPackage) {
plugins.beautylog.warn('failed to retrieve package information...')
plugins.beautylog.info('npms.io might be down')
return
plugins.beautylog.warn('failed to retrieve package information...');
plugins.beautylog.info('npms.io might be down');
return;
}
newData.latestVersion = npmPackage.version
let upgradeBool = await this.checkIfUpgrade(npmPackage, compareVersion, changelogUrlArg)
newData.latestVersion = npmPackage.version;
let upgradeBool = await this.checkIfUpgrade(npmPackage, compareVersion, changelogUrlArg);
if (upgradeBool) {
// TODO:
}
this.kvStore.writeKey(npmnameArg, newData)
this.kvStore.writeKey(npmnameArg, newData);
}
private async getNpmPackageFromRegistry (npmnameArg): Promise<plugins.smartnpm.NpmPackage> {
plugins.beautylog.log(`smartupdate: checking for newer version of ${plugins.beautycolor.coloredString(npmnameArg, 'pink')}...`)
let npmRegistry = new plugins.smartnpm.NpmRegistry()
let npmPackage: plugins.smartnpm.NpmPackage
npmPackage = (await npmRegistry.search({ name: npmnameArg, boostExact: true }))[ 0 ]
return npmPackage
private async getNpmPackageFromRegistry(npmnameArg): Promise<plugins.smartnpm.NpmPackage> {
plugins.beautylog.log(
`smartupdate: checking for newer version of ${plugins.beautycolor.coloredString(
npmnameArg,
'pink'
)}...`
);
let npmRegistry = new plugins.smartnpm.NpmRegistry();
let npmPackage: plugins.smartnpm.NpmPackage;
npmPackage = (await npmRegistry.search({ name: npmnameArg, boostExact: true }))[0];
return npmPackage;
}
private async checkIfUpgrade (
private async checkIfUpgrade(
npmPackage: plugins.smartnpm.NpmPackage,
localVersionStringArg: string,
changelogUrlArg?: string
) {
// create Version objects
let versionNpm = new plugins.smartversion.SmartVersion(npmPackage.version)
let versionLocal = new plugins.smartversion.SmartVersion(localVersionStringArg)
let versionNpm = new plugins.smartversion.SmartVersion(npmPackage.version);
let versionLocal = new plugins.smartversion.SmartVersion(localVersionStringArg);
if (!versionNpm.greaterThan(versionLocal)) {
plugins.beautylog.ok(`smartupdate: You are running the latest version of ${plugins.beautycolor.coloredString(npmPackage.name, 'pink')}`)
return false
plugins.beautylog.ok(
`smartupdate: You are running the latest version of ${plugins.beautycolor.coloredString(
npmPackage.name,
'pink'
)}`
);
return false;
} else {
plugins.beautylog.warn(`There is a newer version of ${npmPackage.name} available on npm.`)
plugins.beautylog.warn(`Your version: ${versionLocal.versionString} | version on npm: ${versionNpm.versionString}`)
plugins.beautylog.warn(`There is a newer version of ${npmPackage.name} available on npm.`);
plugins.beautylog.warn(
`Your version: ${versionLocal.versionString} | version on npm: ${versionNpm.versionString}`
);
if (!process.env.CI && changelogUrlArg) {
plugins.beautylog.log('trying to open changelog...')
plugins.smartopen.openUrl(changelogUrlArg)
plugins.beautylog.log('trying to open changelog...');
plugins.smartopen.openUrl(changelogUrlArg);
}
return true
return true;
}
}
}
export let standardHandler = new SmartUpdate()
export let standardHandler = new SmartUpdate();

View File

@ -1,17 +1,9 @@
import * as beautylog from 'beautylog'
import * as beautycolor from 'beautycolor'
import * as npmextra from 'npmextra'
import * as smartnpm from 'smartnpm'
import * as smartopen from 'smartopen'
import * as smarttime from 'smarttime'
import * as smartversion from 'smartversion'
import * as beautylog from 'beautylog';
import * as beautycolor from 'beautycolor';
import * as npmextra from 'npmextra';
import * as smartnpm from 'smartnpm';
import * as smartopen from '@pushrocks/smartopen';
import * as smarttime from 'smarttime';
import * as smartversion from 'smartversion';
export {
beautylog,
beautycolor,
npmextra,
smartnpm,
smartopen,
smarttime,
smartversion
}
export { beautylog, beautycolor, npmextra, smartnpm, smartopen, smarttime, smartversion };