initial working version
This commit is contained in:
65
ts/index.ts
65
ts/index.ts
@ -1,25 +1,62 @@
|
||||
import * as plugins from './smartupdate.plugins'
|
||||
|
||||
import { TimeStamp } from 'smarttime'
|
||||
|
||||
interface ICacheStatus {
|
||||
lastCheck
|
||||
lastCheck: number
|
||||
latestVersion: string
|
||||
performedUpgrade: boolean
|
||||
}
|
||||
|
||||
class SmartUpdate {
|
||||
import { KeyValueStore } from 'npmextra'
|
||||
|
||||
export class SmartUpdate {
|
||||
kvStore = new plugins.npmextra.KeyValueStore('custom', 'global:smartupdate')
|
||||
|
||||
async checkCacheStatus () {
|
||||
let result: ICacheStatus = await this.kvStore.read(npmname)
|
||||
if(result && result) {
|
||||
|
||||
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
|
||||
}
|
||||
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) {
|
||||
|
||||
}
|
||||
this.kvStore.writeKey(npmnameArg, newData)
|
||||
}
|
||||
|
||||
async check (npmname: string) {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
let standardInstance = new SmartUpdate()
|
||||
|
||||
export let check = standardInstance.check
|
||||
|
||||
|
||||
export let standardExport = 'Hi there! :) This is a exported string'
|
||||
export let standardHandler = new SmartUpdate()
|
||||
|
@ -1,9 +1,13 @@
|
||||
import 'typings-global'
|
||||
|
||||
import * as beautylog from 'beautylog'
|
||||
import * as npmextra from 'npmextra'
|
||||
import * as smartnpm from 'smartnpm'
|
||||
import * as smarttime from 'smarttime'
|
||||
|
||||
export {
|
||||
beautylog,
|
||||
npmextra,
|
||||
smartnpm
|
||||
smartnpm,
|
||||
smarttime
|
||||
}
|
||||
|
Reference in New Issue
Block a user