Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize git during creation #121

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading