Skip to content

Commit 21ee28f

Browse files
authored
Initialize git during creation (#121)
1 parent 39397bd commit 21ee28f

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

programs/create/module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import writeReadmeFile from './steps/writeReadmeFile'
1818
import writeManifestJson from './steps/writeManifestJson'
1919
import generateExtensionTypes from './steps/generateExtensionTypes'
2020
import isTypeScriptTemplate from './helpers/isTypeScriptTemplate'
21+
import initializeGitRepository from './steps/initializeGitRepository'
2122

2223
export interface CreateOptions {
2324
template?: string
@@ -59,6 +60,7 @@ export default async function createExtension(
5960
await installDependencies(projectPath, projectName)
6061
await writeReadmeFile(projectPath, projectName, template)
6162
await writeManifestJson(projectPath, projectName, template)
63+
await initializeGitRepository(projectPath, projectName)
6264

6365
if (isTypeScriptTemplate(template)) {
6466
await generateExtensionTypes(projectPath, projectName)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)