smartupdate/ts/index.ts

79 lines
2.8 KiB
TypeScript
Raw Normal View History

2017-08-15 17:29:18 +00:00
import * as plugins from './smartupdate.plugins'
2017-08-16 16:54:23 +00:00
import { TimeStamp } from 'smarttime'
2017-08-15 17:29:18 +00:00
interface ICacheStatus {
2017-08-16 16:54:23 +00:00
lastCheck: number
latestVersion: string
performedUpgrade: boolean
2017-08-15 17:29:18 +00:00
}
2017-08-16 16:54:23 +00:00
import { KeyValueStore } from 'npmextra'
export class SmartUpdate {
2017-08-15 17:29:18 +00:00
kvStore = new plugins.npmextra.KeyValueStore('custom', 'global:smartupdate')
2017-08-17 09:56:38 +00:00
async check (npmnameArg: string, compareVersion: string, changelogUrlArg?: string) {
2017-08-16 16:54:23 +00:00
let result: ICacheStatus = await this.kvStore.readKey(npmnameArg)
let timeStamp = new TimeStamp()
// the newData to write
let newData = {
lastCheck: timeStamp.milliSeconds,
latestVersion: 'x.x.x',
performedUpgrade: false
2017-08-15 17:29:18 +00:00
}
2017-08-16 16:54:23 +00:00
if (result) {
let lastCheckTimeStamp = TimeStamp.fromMilliSeconds(result.lastCheck)
let compareTime = plugins.smarttime.getMilliSecondsFromUnits({ hours: 1 })
2017-08-16 16:54:23 +00:00
if (!lastCheckTimeStamp.isOlderThan(timeStamp, compareTime)) {
plugins.beautylog.log(
`smartupdate: next check in : ` +
`${plugins.beautycolor.coloredString(
`${npmnameArg} has already been checked within the last hour.`
, 'pink'
)}`
)
2017-08-16 16:54:23 +00:00
return
}
}
let npmPackage = await this.getNpmPackageFromRegistry(npmnameArg)
newData.latestVersion = npmPackage.version
2017-08-17 09:56:38 +00:00
let upgradeBool = await this.checkIfUpgrade(npmPackage, compareVersion, changelogUrlArg)
2017-08-16 16:54:23 +00:00
if(upgradeBool) {
2017-08-16 16:55:16 +00:00
2017-08-16 16:54:23 +00:00
}
this.kvStore.writeKey(npmnameArg, newData)
}
2017-08-15 17:29:18 +00:00
2017-08-16 16:54:23 +00:00
private async getNpmPackageFromRegistry (npmnameArg) {
2017-08-16 21:28:12 +00:00
plugins.beautylog.log(`smartupdate: checking for newer version of ${plugins.beautycolor.coloredString(npmnameArg, 'pink')}...`)
2017-08-16 16:54:23 +00:00
let npmRegistry = new plugins.smartnpm.NpmRegistry()
let npmPackage = (await npmRegistry.search({ name: npmnameArg, boostExact: true }))[0]
return npmPackage
}
2017-08-15 17:29:18 +00:00
2017-08-17 09:56:38 +00:00
private async checkIfUpgrade (
npmPackage: plugins.smartnpm.NpmPackage,
localVersionStringArg: string,
2017-08-17 09:56:38 +00:00
changelogUrlArg?: string
) {
// create Version objects
let versionNpm = new plugins.smartversion.SmartVersion(npmPackage.version)
let versionLocal = new plugins.smartversion.SmartVersion(localVersionStringArg)
2017-08-18 10:52:24 +00:00
if (!versionNpm.greaterThan(versionLocal)) {
2017-08-16 21:28:12 +00:00
plugins.beautylog.ok(`smartupdate: You are running the latest version of ${plugins.beautycolor.coloredString(npmPackage.name, 'pink')}`)
2017-08-16 16:54:23 +00:00
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}`)
2017-08-17 09:56:38 +00:00
if (!process.env.CI && changelogUrlArg) {
plugins.beautylog.log('trying to open changelog...')
plugins.smartopen.openUrl(changelogUrlArg)
}
2017-08-16 16:54:23 +00:00
return true
}
}
}
export let standardHandler = new SmartUpdate()