@@ -46,17 +46,15 @@ module.exports = class Creator {
46
46
this . promptCompleteCbs = [ ]
47
47
this . createCompleteCbs = [ ]
48
48
49
+ this . run = this . run . bind ( this )
50
+
49
51
const promptAPI = new PromptModuleAPI ( this )
50
52
promptModules . forEach ( m => m ( promptAPI ) )
51
53
}
52
54
53
55
async create ( cliOptions = { } ) {
54
56
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
60
58
61
59
let preset
62
60
if ( cliOptions . preset ) {
@@ -114,7 +112,8 @@ module.exports = class Creator {
114
112
115
113
// intilaize git repository before installing deps
116
114
// so that vue-cli-service can setup git hooks.
117
- if ( hasGit ( ) ) {
115
+ const shouldInitGit = await this . shouldInitGit ( cliOptions )
116
+ if ( shouldInitGit ) {
118
117
logWithSpinner ( `🗃` , `Initializing git repository...` )
119
118
await run ( 'git init' )
120
119
}
@@ -158,7 +157,7 @@ module.exports = class Creator {
158
157
}
159
158
160
159
// commit initial state
161
- if ( hasGit ( ) ) {
160
+ if ( shouldInitGit ) {
162
161
await run ( 'git add -A' )
163
162
if ( isTestOrDebug ) {
164
163
await run ( 'git' , [ 'config' , 'user.name' , 'test' ] )
@@ -181,6 +180,11 @@ module.exports = class Creator {
181
180
generator . printExitLogs ( )
182
181
}
183
182
183
+ run ( command , args ) {
184
+ if ( ! args ) { [ command , ...args ] = command . split ( / \s + / ) }
185
+ return execa ( command , args , { cwd : this . context } )
186
+ }
187
+
184
188
async promptAndResolvePreset ( ) {
185
189
// prompt
186
190
await clearConsole ( true )
@@ -379,4 +383,23 @@ module.exports = class Creator {
379
383
debug ( 'vue-cli:prompts' ) ( prompts )
380
384
return prompts
381
385
}
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
+ }
382
405
}
0 commit comments