(projectDir: string)
| 53 | |
| 54 | // This initializes the Git-repository for the project |
| 55 | export const initializeGit = async (projectDir: string) => { |
| 56 | logger.info("Initializing Git..."); |
| 57 | |
| 58 | if (!isGitInstalled(projectDir)) { |
| 59 | logger.warn("Git is not installed. Skipping Git initialization."); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | const spinner = ora("Creating a new git repo...\n").start(); |
| 64 | |
| 65 | const isRoot = await isRootGitRepo(projectDir); |
| 66 | const isInside = await isInsideGitRepo(projectDir); |
| 67 | const dirName = path.parse(projectDir).name; // skip full path for logging |
| 68 | |
| 69 | if (isInside && isRoot) { |
| 70 | // Dir is a root git repo |
| 71 | spinner.stop(); |
| 72 | const { overwriteGit } = await inquirer.prompt<{ |
| 73 | overwriteGit: boolean; |
| 74 | }>({ |
| 75 | name: "overwriteGit", |
| 76 | type: "confirm", |
| 77 | message: `${chalk.redBright.bold( |
| 78 | "Warning:" |
| 79 | )} Git is already initialized in "${dirName}". Initializing a new git repository would delete the previous history. Would you like to continue anyways?`, |
| 80 | default: false, |
| 81 | }); |
| 82 | if (!overwriteGit) { |
| 83 | spinner.info("Skipping Git initialization."); |
| 84 | return; |
| 85 | } |
| 86 | // Deleting the .git folder |
| 87 | await removeFile(path.join(projectDir, ".git")); |
| 88 | } else if (isInside && !isRoot) { |
| 89 | // Dir is inside a git worktree |
| 90 | spinner.stop(); |
| 91 | const { initializeChildGitRepo } = await inquirer.prompt<{ |
| 92 | initializeChildGitRepo: boolean; |
| 93 | }>({ |
| 94 | name: "initializeChildGitRepo", |
| 95 | type: "confirm", |
| 96 | message: `${chalk.redBright.bold( |
| 97 | "Warning:" |
| 98 | )} "${dirName}" is already in a git worktree. Would you still like to initialize a new git repository in this directory?`, |
| 99 | default: false, |
| 100 | }); |
| 101 | if (!initializeChildGitRepo) { |
| 102 | spinner.info("Skipping Git initialization."); |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // We're good to go, initializing the git repo |
| 108 | try { |
| 109 | const branchName = getDefaultBranch(); |
| 110 | |
| 111 | // --initial-branch flag was added in git v2.28.0 |
| 112 | const { major, minor } = getGitVersion(); |
nothing calls this directly
no test coverage detected
searching dependent graphs…