@@ -41,35 +41,41 @@ function isInMercurialRepository() {
41
41
}
42
42
}
43
43
44
- function tryGitInit ( appPath ) {
45
- let didInit = false ;
44
+ function tryGitInit ( ) {
46
45
try {
47
46
execSync ( 'git --version' , { stdio : 'ignore' } ) ;
48
47
if ( isInGitRepository ( ) || isInMercurialRepository ( ) ) {
49
48
return false ;
50
49
}
51
50
52
51
execSync ( 'git init' , { stdio : 'ignore' } ) ;
53
- didInit = true ;
52
+ return true ;
53
+ } catch ( e ) {
54
+ console . warn ( 'Git repo not initialized' , e ) ;
55
+ return false ;
56
+ }
57
+ }
54
58
59
+ function tryGitCommit ( appPath ) {
60
+ try {
55
61
execSync ( 'git add -A' , { stdio : 'ignore' } ) ;
56
62
execSync ( 'git commit -m "Initialize project using Create React App"' , {
57
63
stdio : 'ignore' ,
58
64
} ) ;
59
65
return true ;
60
66
} catch ( e ) {
61
- if ( didInit ) {
62
- // If we successfully initialized but couldn't commit,
63
- // maybe the commit author config is not set.
64
- // In the future, we might supply our own committer
65
- // like Ember CLI does, but for now, let's just
66
- // remove the Git files to avoid a half-done state.
67
- try {
68
- // unlinkSync() doesn't work on directories.
69
- fs . removeSync ( path . join ( appPath , '.git' ) ) ;
70
- } catch ( removeErr ) {
71
- // Ignore.
72
- }
67
+ // We couldn't commit in already initialized git repo,
68
+ // maybe the commit author config is not set.
69
+ // In the future, we might supply our own committer
70
+ // like Ember CLI does, but for now, let's just
71
+ // remove the Git files to avoid a half-done state.
72
+ console . warn ( ' Git commit not created' , e ) ;
73
+ console . warn ( 'Removing .git directory...' ) ;
74
+ try {
75
+ // unlinkSync() doesn't work on directories.
76
+ fs . removeSync ( path . join ( appPath , '.git' ) ) ;
77
+ } catch ( removeErr ) {
78
+ // Ignore.
73
79
}
74
80
return false ;
75
81
}
@@ -255,6 +261,15 @@ module.exports = function(
255
261
) ;
256
262
}
257
263
264
+ // Initialize git repo
265
+ let initializedGit = false ;
266
+
267
+ if ( tryGitInit ( ) ) {
268
+ initializedGit = true ;
269
+ console . log ( ) ;
270
+ console . log ( 'Initialized a git repository.' ) ;
271
+ }
272
+
258
273
let command ;
259
274
let remove ;
260
275
let args ;
@@ -316,9 +331,10 @@ module.exports = function(
316
331
return ;
317
332
}
318
333
319
- if ( tryGitInit ( appPath ) ) {
334
+ // Create git commit if git repo was initialized
335
+ if ( initializedGit && tryGitCommit ( appPath ) ) {
320
336
console . log ( ) ;
321
- console . log ( 'Initialized a git repository .' ) ;
337
+ console . log ( 'Created git commit .' ) ;
322
338
}
323
339
324
340
// Display the most elegant way to cd.
0 commit comments