Skip to content

Commit f869937

Browse files
authored
Fix the E2E script (facebook#3888)
* Fix the E2E script * Delete .git if committing failed
1 parent 854d430 commit f869937

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

scripts/init.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const spawn = require('react-dev-utils/crossSpawn');
2222
const { defaultBrowsers } = require('react-dev-utils/browsersHelper');
2323
const os = require('os');
2424

25-
function insideGitRepository() {
25+
function isInGitRepository() {
2626
try {
2727
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
2828
return true;
@@ -31,7 +31,7 @@ function insideGitRepository() {
3131
}
3232
}
3333

34-
function insideMercurialRepository() {
34+
function isInMercurialRepository() {
3535
try {
3636
execSync('hg --cwd . root', { stdio: 'ignore' });
3737
return true;
@@ -40,22 +40,36 @@ function insideMercurialRepository() {
4040
}
4141
}
4242

43-
function tryGitInit() {
43+
function tryGitInit(appPath) {
44+
let didInit = false;
4445
try {
4546
execSync('git --version', { stdio: 'ignore' });
46-
47-
if (insideGitRepository() || insideMercurialRepository()) {
47+
if (isInGitRepository() || isInMercurialRepository()) {
4848
return false;
4949
}
5050

5151
execSync('git init', { stdio: 'ignore' });
52+
didInit = true;
53+
5254
execSync('git add -A', { stdio: 'ignore' });
5355
execSync('git commit -m "Initial commit from Create React App"', {
5456
stdio: 'ignore',
5557
});
56-
5758
return true;
5859
} catch (e) {
60+
if (didInit) {
61+
// If we successfully initialized but couldn't commit,
62+
// maybe the commit author config is not set.
63+
// In the future, we might supply our own committer
64+
// like Ember CLI does, but for now, let's just
65+
// remove the Git files to avoid a half-done state.
66+
try {
67+
// unlinkSync() doesn't work on directories.
68+
fs.removeSync(path.join(appPath, '.git'));
69+
} catch (removeErr) {
70+
// Ignore.
71+
}
72+
}
5973
return false;
6074
}
6175
}
@@ -172,7 +186,7 @@ module.exports = function(
172186
}
173187
}
174188

175-
if (tryGitInit()) {
189+
if (tryGitInit(appPath)) {
176190
console.log();
177191
console.log('Initialized a git repository.');
178192
}

0 commit comments

Comments
 (0)