2021-01-20 16:05:56 +00:00
|
|
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
|
|
|
2021-01-20 16:29:49 +00:00
|
|
|
import { name as pkgName, version as pkgVer } from '../package.json'
|
|
|
|
import { getFetch } from './utils'
|
2021-01-15 07:54:01 +00:00
|
|
|
|
2021-01-20 16:29:49 +00:00
|
|
|
const IS_NPX_REG = /_npx(\/|\\)\d+\1/
|
|
|
|
const NPM_REGISTRY = 'https://registry.npmjs.org'
|
2021-01-15 07:54:01 +00:00
|
|
|
|
2021-01-20 16:29:49 +00:00
|
|
|
export function isNpx (): boolean {
|
|
|
|
// file is in a npx cache dir
|
|
|
|
// TODO: installed locally?
|
|
|
|
return __dirname.match(IS_NPX_REG) !== null
|
2021-01-15 07:54:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 15:52:08 +00:00
|
|
|
export function getInstalledVer (): string {
|
2021-01-20 16:29:49 +00:00
|
|
|
return pkgVer
|
2021-01-15 08:01:11 +00:00
|
|
|
}
|
2021-01-15 07:54:01 +00:00
|
|
|
|
2021-01-20 16:29:49 +00:00
|
|
|
export async function getLatestVer (_fetch = getFetch()): Promise<string> {
|
|
|
|
// fetch pkg info from the npm registry
|
|
|
|
const r = await _fetch(`${NPM_REGISTRY}/${pkgName}`)
|
|
|
|
const json = await r.json()
|
|
|
|
return json['dist-tags'].latest as string
|
2021-01-15 08:01:11 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 16:05:56 +00:00
|
|
|
export async function getVerInfo () {
|
|
|
|
const installed = getInstalledVer()
|
|
|
|
const latest = await getLatestVer()
|
|
|
|
return {
|
|
|
|
installed,
|
|
|
|
latest,
|
|
|
|
isLatest: installed === latest,
|
|
|
|
}
|
2021-01-20 15:42:33 +00:00
|
|
|
}
|