1- import { question , select } from '@topcli/prompts' ;
1+ import { question , select , confirm } from '@topcli/prompts' ;
22import { consola } from 'consola' ;
33import { downloadTemplate } from 'giget' ;
44import { readdir , rename } from 'node:fs/promises' ;
55import { pathExists } from './internal-utils/fs-utils' ;
6+ import { normalizePath } from './internal-utils/path-utils' ;
67import path from 'node:path' ;
78import { styleText } from 'node:util' ;
89import { TextStyle } from './internal-utils/type-utils' ;
910import { createSpinner } from './internal/spinner' ;
11+ import { x as spawn } from 'tinyexec' ;
1012
1113export async function initialize ( options : {
1214 directory ?: string ;
1315 template ?: string ;
1416 packageManager ?: string ;
17+ requireGit ?: boolean ;
1518} ) {
1619 consola . info ( 'Initializing new project' ) ;
1720
@@ -51,6 +54,11 @@ export async function initialize(options: {
5154 ] ,
5255 } ) ) ;
5356
57+ // Asks whether require git initialized or not?
58+ const requireGitInitialized =
59+ options . requireGit ??
60+ ( await confirm ( 'Initialize a new git repository?' , { initial : true } ) ) ;
61+
5462 const isExists = await pathExists ( directory ) ;
5563 if ( isExists ) {
5664 const isEmpty =
@@ -62,7 +70,7 @@ export async function initialize(options: {
6270 process . exit ( 1 ) ;
6371 }
6472 }
65- await cloneProject ( { directory, template } ) ;
73+ await cloneProject ( { directory, template, requireGitInitialized } ) ;
6674
6775 const cdPath = path . relative ( process . cwd ( ) , path . resolve ( directory ) ) ;
6876 console . log ( ) ;
@@ -151,14 +159,16 @@ async function listTemplatesGithub(): Promise<Template[]> {
151159async function cloneProject ( {
152160 directory,
153161 template,
162+ requireGitInitialized,
154163} : {
155164 directory : string ;
156165 template : Template ;
166+ requireGitInitialized : boolean ;
157167} ) {
158168 const spinner = createSpinner ( 'Downloading template' ) . start ( ) ;
159169 try {
160170 // 1. Clone repo
161- await downloadTemplate ( `gh:${ REPO } /${ template . path } ` , {
171+ const response = await downloadTemplate ( `gh:${ REPO } /${ template . path } ` , {
162172 dir : directory ,
163173 force : true ,
164174 } ) ;
@@ -171,6 +181,19 @@ async function cloneProject({
171181 consola . warn ( 'Failed to move _gitignore to .gitignore:' , err ) ,
172182 ) ;
173183
184+ // 3. Initializing Git
185+ if ( requireGitInitialized ) {
186+ try {
187+ const dirPath = normalizePath ( response . dir ) ;
188+ await spawn ( 'git' , [ 'init' ] , {
189+ throwOnError : true ,
190+ nodeOptions : { cwd : dirPath , shell : true } ,
191+ } ) ;
192+ } catch ( error ) {
193+ console . error ( error ) ;
194+ }
195+ }
196+
174197 spinner . success ( ) ;
175198 } catch ( err ) {
176199 spinner . error ( ) ;
0 commit comments