@@ -46,17 +46,15 @@ module.exports = class Creator {
4646 this . promptCompleteCbs = [ ]
4747 this . createCompleteCbs = [ ]
4848
49+ this . run = this . run . bind ( this )
50+
4951 const promptAPI = new PromptModuleAPI ( this )
5052 promptModules . forEach ( m => m ( promptAPI ) )
5153 }
5254
5355 async create ( cliOptions = { } ) {
5456 const isTestOrDebug = process . env . VUE_CLI_TEST || process . env . VUE_CLI_DEBUG
55- const { name, context, createCompleteCbs } = this
56- const run = ( command , args ) => {
57- if ( ! args ) { [ command , ...args ] = command . split ( / \s + / ) }
58- return execa ( command , args , { cwd : context } )
59- }
57+ const { run, name, context, createCompleteCbs } = this
6058
6159 let preset
6260 if ( cliOptions . preset ) {
@@ -114,7 +112,8 @@ module.exports = class Creator {
114112
115113 // intilaize git repository before installing deps
116114 // so that vue-cli-service can setup git hooks.
117- if ( hasGit ( ) ) {
115+ const shouldInitGit = await this . shouldInitGit ( cliOptions )
116+ if ( shouldInitGit ) {
118117 logWithSpinner ( `🗃` , `Initializing git repository...` )
119118 await run ( 'git init' )
120119 }
@@ -158,7 +157,7 @@ module.exports = class Creator {
158157 }
159158
160159 // commit initial state
161- if ( hasGit ( ) ) {
160+ if ( shouldInitGit ) {
162161 await run ( 'git add -A' )
163162 if ( isTestOrDebug ) {
164163 await run ( 'git' , [ 'config' , 'user.name' , 'test' ] )
@@ -181,6 +180,11 @@ module.exports = class Creator {
181180 generator . printExitLogs ( )
182181 }
183182
183+ run ( command , args ) {
184+ if ( ! args ) { [ command , ...args ] = command . split ( / \s + / ) }
185+ return execa ( command , args , { cwd : this . context } )
186+ }
187+
184188 async promptAndResolvePreset ( ) {
185189 // prompt
186190 await clearConsole ( true )
@@ -379,4 +383,23 @@ module.exports = class Creator {
379383 debug ( 'vue-cli:prompts' ) ( prompts )
380384 return prompts
381385 }
386+
387+ async shouldInitGit ( cliOptions ) {
388+ if ( ! hasGit ( ) ) {
389+ return false
390+ }
391+ if ( cliOptions . skipGit ) {
392+ return false
393+ }
394+ // check if we are in a git repo already
395+ try {
396+ await this . run ( 'git' , [ 'status' ] )
397+ } catch ( e ) {
398+ // if git status failed, let's create a fresh repo
399+ return true
400+ }
401+ // if git status worked, it means we are already in a git repo
402+ // so don't init again.
403+ return false
404+ }
382405}
0 commit comments