aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/packages/turbo-codemod/src/commands/migrate/steps/getCurrentVersion.ts
blob: 3644f8b2e15acfd778f95bad369eb852c50468da (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
35
36
37
38
39
40
41
42
43
44
45
import path from "path";
import { existsSync } from "fs-extra";

import getPackageManager from "../../../utils/getPackageManager";
import { exec } from "../utils";
import type { MigrateCommandOptions } from "../types";

function getCurrentVersion(
  directory: string,
  opts: MigrateCommandOptions
): string | undefined {
  const { from } = opts;
  if (from) {
    return from;
  }

  // try global first
  const turboVersionFromGlobal = exec(`turbo --version`, { cwd: directory });

  if (turboVersionFromGlobal) {
    return turboVersionFromGlobal;
  }

  // try to use the package manager to find the version
  const packageManager = getPackageManager({ directory });
  if (packageManager) {
    if (packageManager === "yarn") {
      return exec(`yarn turbo --version`, { cwd: directory });
    }
    if (packageManager === "pnpm") {
      return exec(`pnpm turbo --version`, { cwd: directory });
    } else {
      // this doesn't work for npm, so manually build the binary path
      const turboBin = path.join(directory, "node_modules", ".bin", "turbo");
      if (existsSync(turboBin)) {
        return exec(`${turboBin} --version`, { cwd: directory });
      }
    }
  }

  // unable to determine local version,
  return undefined;
}

export default getCurrentVersion;