Skip to content

Commit 84d8b14

Browse files
authored
Run git init before template dependencies are installed (#8282)
1 parent 9233caf commit 84d8b14

File tree

1 file changed

+33
-17
lines changed
  • packages/react-scripts/scripts

1 file changed

+33
-17
lines changed

packages/react-scripts/scripts/init.js

+33-17
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,41 @@ function isInMercurialRepository() {
4141
}
4242
}
4343

44-
function tryGitInit(appPath) {
45-
let didInit = false;
44+
function tryGitInit() {
4645
try {
4746
execSync('git --version', { stdio: 'ignore' });
4847
if (isInGitRepository() || isInMercurialRepository()) {
4948
return false;
5049
}
5150

5251
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+
}
5458

59+
function tryGitCommit(appPath) {
60+
try {
5561
execSync('git add -A', { stdio: 'ignore' });
5662
execSync('git commit -m "Initialize project using Create React App"', {
5763
stdio: 'ignore',
5864
});
5965
return true;
6066
} 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.
7379
}
7480
return false;
7581
}
@@ -255,6 +261,15 @@ module.exports = function(
255261
);
256262
}
257263

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+
258273
let command;
259274
let remove;
260275
let args;
@@ -316,9 +331,10 @@ module.exports = function(
316331
return;
317332
}
318333

319-
if (tryGitInit(appPath)) {
334+
// Create git commit if git repo was initialized
335+
if (initializedGit && tryGitCommit(appPath)) {
320336
console.log();
321-
console.log('Initialized a git repository.');
337+
console.log('Created git commit.');
322338
}
323339

324340
// Display the most elegant way to cd.

0 commit comments

Comments
 (0)