-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize git during creation (#121)
- Loading branch information
1 parent
39397bd
commit 21ee28f
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// ββββββββββββββ ββββββββ ββββββ βββββββββββββββββ | ||
// βββββββββββββββββββββββββββββββββββββββββββββββββ | ||
// βββ ββββββββββββββ ββββββββ βββ ββββββ | ||
// βββ ββββββββββββββ ββββββββ βββ ββββββ | ||
// βββββββββββ ββββββββββββββ βββ βββ ββββββββ | ||
// ββββββββββ ββββββββββββββ βββ βββ ββββββββ | ||
|
||
import {spawn} from 'cross-spawn' | ||
import {bold, yellow, red} from '@colors/colors/safe' | ||
|
||
export default async function initializeGitRepository( | ||
projectPath: string, | ||
projectName: string | ||
) { | ||
const gitCommand = 'git' | ||
const gitArgs = ['init'] | ||
|
||
console.log(`π² - Initializing git repository for ${bold(projectName)}...`) | ||
|
||
try { | ||
const originalDirectory = process.cwd() | ||
|
||
// Change to the project directory | ||
process.chdir(projectPath) | ||
|
||
const stdio = | ||
process.env.EXTENSION_ENV === 'development' ? 'inherit' : 'ignore' | ||
const child = spawn(gitCommand, gitArgs, {stdio}) | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
child.on('close', (code) => { | ||
// Change back to the original directory | ||
process.chdir(originalDirectory) | ||
|
||
if (code !== 0) { | ||
reject( | ||
new Error( | ||
`Command ${gitCommand} ${gitArgs.join(' ')} failed with exit code ${code}` | ||
) | ||
) | ||
} else { | ||
resolve() | ||
} | ||
}) | ||
|
||
child.on('error', (error) => { | ||
// Change back to the original directory | ||
process.chdir(originalDirectory) | ||
|
||
console.error( | ||
`𧩠${bold(`Extension.js`)} ${red( | ||
`βοΈβοΈβοΈ` | ||
)} Child process error: Can't initialize ${yellow('git')} for ${bold(projectName)}. ${error.message}` | ||
) | ||
reject(error) | ||
}) | ||
}) | ||
} catch (error: any) { | ||
console.error( | ||
`𧩠${bold(`Extension.js`)} ${red( | ||
`βοΈβοΈβοΈ` | ||
)} Can't initialize ${yellow('git')} for ${bold(projectName)}. ${error.message || error.toString()}` | ||
) | ||
|
||
process.exit(1) | ||
} | ||
} |