From b3527d7783b290d37275925a1d6a7ff9e40e7a86 Mon Sep 17 00:00:00 2001 From: Dan Abramov <dan.abramov@gmail.com> Date: Sat, 20 Jan 2018 18:32:41 +0000 Subject: [PATCH] Fix git init race condition (#3877) --- packages/react-scripts/scripts/init.js | 38 +++++++++++++------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/react-scripts/scripts/init.js b/packages/react-scripts/scripts/init.js index 8837c28ea..3347a37f4 100644 --- a/packages/react-scripts/scripts/init.js +++ b/packages/react-scripts/scripts/init.js @@ -40,7 +40,7 @@ function insideMercurialRepository() { } } -function gitInit() { +function tryGitInit() { try { execSync('git --version', { stdio: 'ignore' }); @@ -114,23 +114,22 @@ module.exports = function( // Rename gitignore after the fact to prevent npm from renaming it to .npmignore // See: https://github.com/npm/npm/issues/1862 - fs.move( - path.join(appPath, 'gitignore'), - path.join(appPath, '.gitignore'), - [], - err => { - if (err) { - // Append if there's already a `.gitignore` file there - if (err.code === 'EEXIST') { - const data = fs.readFileSync(path.join(appPath, 'gitignore')); - fs.appendFileSync(path.join(appPath, '.gitignore'), data); - fs.unlinkSync(path.join(appPath, 'gitignore')); - } else { - throw err; - } - } + try { + fs.moveSync( + path.join(appPath, 'gitignore'), + path.join(appPath, '.gitignore'), + [] + ); + } catch (err) { + // Append if there's already a `.gitignore` file there + if (err.code === 'EEXIST') { + const data = fs.readFileSync(path.join(appPath, 'gitignore')); + fs.appendFileSync(path.join(appPath, '.gitignore'), data); + fs.unlinkSync(path.join(appPath, 'gitignore')); + } else { + throw err; } - ); + } let command; let args; @@ -173,8 +172,9 @@ module.exports = function( } } - if (gitInit()) { - console.log('Initialized git repository'); + if (tryGitInit()) { + console.log(); + console.log('Initialized a git repository.'); } // Display the most elegant way to cd. -- GitLab