Skip to content

Commit

Permalink
Initialize git during creation (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto authored Jun 29, 2024
1 parent 39397bd commit 21ee28f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions programs/create/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import writeReadmeFile from './steps/writeReadmeFile'
import writeManifestJson from './steps/writeManifestJson'
import generateExtensionTypes from './steps/generateExtensionTypes'
import isTypeScriptTemplate from './helpers/isTypeScriptTemplate'
import initializeGitRepository from './steps/initializeGitRepository'

export interface CreateOptions {
template?: string
Expand Down Expand Up @@ -59,6 +60,7 @@ export default async function createExtension(
await installDependencies(projectPath, projectName)
await writeReadmeFile(projectPath, projectName, template)
await writeManifestJson(projectPath, projectName, template)
await initializeGitRepository(projectPath, projectName)

if (isTypeScriptTemplate(template)) {
await generateExtensionTypes(projectPath, projectName)
Expand Down
67 changes: 67 additions & 0 deletions programs/create/steps/initializeGitRepository.ts
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)
}
}

0 comments on commit 21ee28f

Please sign in to comment.