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

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

const lernaJson = JSON.parse(fs.readFileSync('lerna.json').toString());
const NPM_REGISTRY_BASE_URL = lernaJson.registry;
const SUCCESS_STATUS = 200;
const NOT_FOUND_STATUS = 404;

export const npmUtils = {
    async getPackageRegistryJsonIfExistsAsync(packageName: string): Promise<PackageRegistryJson | undefined> {
        const url = `${NPM_REGISTRY_BASE_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;
    },
};