aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/packages/turbo-codemod/src/commands/migrate/steps/getLatestVersion.ts
blob: a6ab7e646837dfdf0da1469f830fef6d83826a40 (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
import axios from "axios";

import type { MigrateCommandOptions } from "../types";

const REGISTRY = "https://registry.npmjs.org";

async function getPackageDetails({ packageName }: { packageName: string }) {
  try {
    const result = await axios.get(`${REGISTRY}/${packageName}`);
    return result.data;
  } catch (err) {
    throw new Error(`Unable to fetch the latest version of ${packageName}`);
  }
}

export default async function getLatestVersion({
  to,
}: MigrateCommandOptions): Promise<string | undefined> {
  const packageDetails = await getPackageDetails({ packageName: "turbo" });
  const { "dist-tags": tags, versions } = packageDetails;

  if (to) {
    if (tags[to] || versions[to]) {
      return to;
    } else {
      throw new Error(`turbo@${to} does not exist`);
    }
  }

  return tags.latest as string;
}