aboutsummaryrefslogtreecommitdiff
path: root/src/npm-data.ts
blob: 89f40fd51421af90ca3a969d78fbc07638bd6183 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */

import { name as pkgName, version as pkgVer } from "../package.json";
import { getFetch } from "./utils";

const IS_NPX_REG = /_npx(\/|\\)\d+\1/;
const NPM_REGISTRY = "https://registry.npmjs.org";

export function isNpx(): boolean {
    // file is in a npx cache dir
    // TODO: installed locally?
    return __dirname.match(IS_NPX_REG) !== null;
}

export function getSelfVer(): string {
    return pkgVer;
}

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;
}

export async function getVerInfo() {
    const installed = getSelfVer();
    const latest = await getLatestVer();
    return {
        installed,
        latest,
        isLatest: installed === latest,
    };
}