smartupdate/ts/index.ts

63 lines
2.2 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-16 16:54:23 +00:00
async check (npmnameArg: string, compareVersion: string) {
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({ days: 1 })
if (!lastCheckTimeStamp.isOlderThan(timeStamp, compareTime)) {
plugins.beautylog.log('not checking for new version since this has been done already for today')
return
}
}
let npmPackage = await this.getNpmPackageFromRegistry(npmnameArg)
newData.latestVersion = npmPackage.version
let upgradeBool = await this.checkIfUpgrade(npmPackage, compareVersion)
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) {
plugins.beautylog.log(`checking for newer version of ${npmnameArg}...`)
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-16 16:54:23 +00:00
private async checkIfUpgrade (npmPackage: plugins.smartnpm.NpmPackage, versionArg: string) {
if (npmPackage.version === versionArg) {
plugins.beautylog.ok(`You are running the latest version of ${npmPackage.name}`)
return false
} else {
plugins.beautylog.warn(`There is a newer version of ${npmPackage.name} available on npm.`)
plugins.beautylog.info(`Your version: ${versionArg} | version on npm: ${npmPackage.version}`)
plugins.beautylog.warn(`!!! You should upgrade!!!`)
return true
}
}
}
export let standardHandler = new SmartUpdate()