Skip to content

Commit

Permalink
Run git init before template dependencies are installed (#8282)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszfiszer authored Jan 31, 2020
1 parent 9233caf commit 84d8b14
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions packages/react-scripts/scripts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,41 @@ function isInMercurialRepository() {
}
}

function tryGitInit(appPath) {
let didInit = false;
function tryGitInit() {
try {
execSync('git --version', { stdio: 'ignore' });
if (isInGitRepository() || isInMercurialRepository()) {
return false;
}

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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 84d8b14

Please sign in to comment.