|
| 1 | +// ██████╗██████╗ ███████╗ █████╗ ████████╗███████╗ |
| 2 | +// ██╔════╝██╔══██╗██╔════╝██╔══██╗╚══██╔══╝██╔════╝ |
| 3 | +// ██║ ██████╔╝█████╗ ███████║ ██║ █████╗ |
| 4 | +// ██║ ██╔══██╗██╔══╝ ██╔══██║ ██║ ██╔══╝ |
| 5 | +// ╚██████╗██║ ██║███████╗██║ ██║ ██║ ███████╗ |
| 6 | +// ╚═════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═╝ ╚══════╝ |
| 7 | + |
| 8 | +import {spawn} from 'cross-spawn' |
| 9 | +import {bold, yellow, red} from '@colors/colors/safe' |
| 10 | + |
| 11 | +export default async function initializeGitRepository( |
| 12 | + projectPath: string, |
| 13 | + projectName: string |
| 14 | +) { |
| 15 | + const gitCommand = 'git' |
| 16 | + const gitArgs = ['init'] |
| 17 | + |
| 18 | + console.log(`🌲 - Initializing git repository for ${bold(projectName)}...`) |
| 19 | + |
| 20 | + try { |
| 21 | + const originalDirectory = process.cwd() |
| 22 | + |
| 23 | + // Change to the project directory |
| 24 | + process.chdir(projectPath) |
| 25 | + |
| 26 | + const stdio = |
| 27 | + process.env.EXTENSION_ENV === 'development' ? 'inherit' : 'ignore' |
| 28 | + const child = spawn(gitCommand, gitArgs, {stdio}) |
| 29 | + |
| 30 | + await new Promise<void>((resolve, reject) => { |
| 31 | + child.on('close', (code) => { |
| 32 | + // Change back to the original directory |
| 33 | + process.chdir(originalDirectory) |
| 34 | + |
| 35 | + if (code !== 0) { |
| 36 | + reject( |
| 37 | + new Error( |
| 38 | + `Command ${gitCommand} ${gitArgs.join(' ')} failed with exit code ${code}` |
| 39 | + ) |
| 40 | + ) |
| 41 | + } else { |
| 42 | + resolve() |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + child.on('error', (error) => { |
| 47 | + // Change back to the original directory |
| 48 | + process.chdir(originalDirectory) |
| 49 | + |
| 50 | + console.error( |
| 51 | + `🧩 ${bold(`Extension.js`)} ${red( |
| 52 | + `✖︎✖︎✖︎` |
| 53 | + )} Child process error: Can't initialize ${yellow('git')} for ${bold(projectName)}. ${error.message}` |
| 54 | + ) |
| 55 | + reject(error) |
| 56 | + }) |
| 57 | + }) |
| 58 | + } catch (error: any) { |
| 59 | + console.error( |
| 60 | + `🧩 ${bold(`Extension.js`)} ${red( |
| 61 | + `✖︎✖︎✖︎` |
| 62 | + )} Can't initialize ${yellow('git')} for ${bold(projectName)}. ${error.message || error.toString()}` |
| 63 | + ) |
| 64 | + |
| 65 | + process.exit(1) |
| 66 | + } |
| 67 | +} |
0 commit comments