aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/packages/turbo-codemod/src/utils/checkGitStatus.ts
blob: 68d39aea499280f37e1c651960d29c9278ae1307 (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
import chalk from "chalk";
import isGitClean from "is-git-clean";

export default function checkGitStatus({
  directory,
  force,
}: {
  directory?: string;
  force: boolean;
}) {
  let clean = false;
  let errorMessage = "Unable to determine if git directory is clean";
  try {
    clean = isGitClean.sync(directory || process.cwd());
    errorMessage = "Git directory is not clean";
  } catch (err: any) {
    if (err && err.stderr && err.stderr.indexOf("not a git repository") >= 0) {
      clean = true;
    }
  }

  if (!clean) {
    if (force) {
      console.log(
        `${chalk.yellow("WARNING")}: ${errorMessage}. Forcibly continuing...`
      );
    } else {
      console.log("Thank you for using @turbo/codemod!");
      console.log(
        chalk.yellow(
          "\nBut before we continue, please stash or commit your git changes."
        )
      );
      console.log(
        "\nYou may use the --force flag to override this safety check."
      );
      process.exit(1);
    }
  }
}