From dd84b9d64fb98746a230cd24233ff50a562c39c9 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Fri, 28 Apr 2023 01:36:44 +0800 Subject: --- packages/turbo-codemod/src/utils/checkGitStatus.ts | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 packages/turbo-codemod/src/utils/checkGitStatus.ts (limited to 'packages/turbo-codemod/src/utils/checkGitStatus.ts') diff --git a/packages/turbo-codemod/src/utils/checkGitStatus.ts b/packages/turbo-codemod/src/utils/checkGitStatus.ts new file mode 100644 index 0000000..68d39ae --- /dev/null +++ b/packages/turbo-codemod/src/utils/checkGitStatus.ts @@ -0,0 +1,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); + } + } +} -- cgit v1.2.3-70-g09d2