diff --git a/packages/react-scripts/scripts/init.js b/packages/react-scripts/scripts/init.js index 556bab02957..dec22b7ec23 100644 --- a/packages/react-scripts/scripts/init.js +++ b/packages/react-scripts/scripts/init.js @@ -41,8 +41,7 @@ function isInMercurialRepository() { } } -function tryGitInit(appPath) { - let didInit = false; +function tryGitInit() { try { execSync('git --version', { stdio: 'ignore' }); if (isInGitRepository() || isInMercurialRepository()) { @@ -50,26 +49,33 @@ function tryGitInit(appPath) { } execSync('git init', { stdio: 'ignore' }); - didInit = true; + return true; + } catch (e) { + console.warn('Git repo not initialized', e); + return false; + } +} +function tryGitCommit(appPath) { + try { execSync('git add -A', { stdio: 'ignore' }); execSync('git commit -m "Initialize project using Create React App"', { stdio: 'ignore', }); return true; } catch (e) { - if (didInit) { - // If we successfully initialized but couldn't commit, - // maybe the commit author config is not set. - // In the future, we might supply our own committer - // like Ember CLI does, but for now, let's just - // remove the Git files to avoid a half-done state. - try { - // unlinkSync() doesn't work on directories. - fs.removeSync(path.join(appPath, '.git')); - } catch (removeErr) { - // Ignore. - } + // We couldn't commit in already initialized git repo, + // maybe the commit author config is not set. + // In the future, we might supply our own committer + // like Ember CLI does, but for now, let's just + // remove the Git files to avoid a half-done state. + console.warn('Git commit not created', e); + console.warn('Removing .git directory...'); + try { + // unlinkSync() doesn't work on directories. + fs.removeSync(path.join(appPath, '.git')); + } catch (removeErr) { + // Ignore. } return false; } @@ -255,6 +261,15 @@ module.exports = function( ); } + // Initialize git repo + let initializedGit = false; + + if (tryGitInit()) { + initializedGit = true; + console.log(); + console.log('Initialized a git repository.'); + } + let command; let remove; let args; @@ -316,9 +331,10 @@ module.exports = function( return; } - if (tryGitInit(appPath)) { + // Create git commit if git repo was initialized + if (initializedGit && tryGitCommit(appPath)) { console.log(); - console.log('Initialized a git repository.'); + console.log('Created git commit.'); } // Display the most elegant way to cd.