Skip to content

Commit b3527d7

Browse files
authored
Fix git init race condition (facebook#3877)
1 parent ab2e0f8 commit b3527d7

File tree

1 file changed

+19
-19
lines changed
  • packages/react-scripts/scripts

1 file changed

+19
-19
lines changed

packages/react-scripts/scripts/init.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function insideMercurialRepository() {
4040
}
4141
}
4242

43-
function gitInit() {
43+
function tryGitInit() {
4444
try {
4545
execSync('git --version', { stdio: 'ignore' });
4646

@@ -114,23 +114,22 @@ module.exports = function(
114114

115115
// Rename gitignore after the fact to prevent npm from renaming it to .npmignore
116116
// See: https://github.com/npm/npm/issues/1862
117-
fs.move(
118-
path.join(appPath, 'gitignore'),
119-
path.join(appPath, '.gitignore'),
120-
[],
121-
err => {
122-
if (err) {
123-
// Append if there's already a `.gitignore` file there
124-
if (err.code === 'EEXIST') {
125-
const data = fs.readFileSync(path.join(appPath, 'gitignore'));
126-
fs.appendFileSync(path.join(appPath, '.gitignore'), data);
127-
fs.unlinkSync(path.join(appPath, 'gitignore'));
128-
} else {
129-
throw err;
130-
}
131-
}
117+
try {
118+
fs.moveSync(
119+
path.join(appPath, 'gitignore'),
120+
path.join(appPath, '.gitignore'),
121+
[]
122+
);
123+
} catch (err) {
124+
// Append if there's already a `.gitignore` file there
125+
if (err.code === 'EEXIST') {
126+
const data = fs.readFileSync(path.join(appPath, 'gitignore'));
127+
fs.appendFileSync(path.join(appPath, '.gitignore'), data);
128+
fs.unlinkSync(path.join(appPath, 'gitignore'));
129+
} else {
130+
throw err;
132131
}
133-
);
132+
}
134133

135134
let command;
136135
let args;
@@ -173,8 +172,9 @@ module.exports = function(
173172
}
174173
}
175174

176-
if (gitInit()) {
177-
console.log('Initialized git repository');
175+
if (tryGitInit()) {
176+
console.log();
177+
console.log('Initialized a git repository.');
178178
}
179179

180180
// Display the most elegant way to cd.

0 commit comments

Comments
 (0)