aboutsummaryrefslogtreecommitdiffstats
path: root/packages/monorepo-scripts/src/utils/npm_utils.ts
blob: 363e31fbbd9fe66401162fdb8831a6e92ff102f9 (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
import 'isomorphic-fetch';
import * as _ from 'lodash';

import { PackageRegistryJson } from '../types';

import { configs } from './configs';

const SUCCESS_STATUS = 200;
const NOT_FOUND_STATUS = 404;

export const npmUtils = {
    async getPackageRegistryJsonIfExistsAsync(packageName: string): Promise<PackageRegistryJson | undefined> {
        const url = `${configs.NPM_REGISTRY_URL}/${packageName}`;
        const response = await fetch(url);

        if (response.status === NOT_FOUND_STATUS) {
            return undefined;
        } else if (response.status !== SUCCESS_STATUS) {
            throw new Error(`Request to ${url} failed. Check your internet connection and that npm registry is up.`);
        }
        const packageRegistryJson = await response.json();
        return packageRegistryJson;
    },
    getPreviouslyPublishedVersions(packageRegistryJson: PackageRegistryJson): string[] {
        const timeWithOnlyVersions = _.omit(packageRegistryJson.time, ['modified', 'created']);
        const versions = _.keys(timeWithOnlyVersions);
        return versions;
    },
};