Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ejecting should ensure you have clean git status #2090

Closed
wants to merge 15 commits into from
Closed
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/react-scripts/scripts/eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const path = require('path');
const spawnSync = require('cross-spawn').sync;
const chalk = require('chalk');
const prompt = require('react-dev-utils/prompt');
const execSync = require('child_process').execSync;
const paths = require('../config/paths');
const createJestConfig = require('./utils/createJestConfig');

Expand All @@ -36,6 +37,31 @@ prompt(
process.exit(1);
}

// Make sure there are no dirty git status
const git = fs.existsSync(path.join(process.cwd(), '.git'));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @thejameskyle, can you explain what are the benefits of using that library? Is it because it returns promised result? Cause I was trying to avoid using third-party library as possible. If promise is necessary I could just use instantiate Promise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@milocosmopolitan someone might have their git directory several levels up the tree, especially if it's a monorepo.

However, you shouldn't be checking for the existence of .git at all -- simply execute a git command in the directory and see if it errors out 😄.

Copy link
Contributor

@Timer Timer May 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 1288's files for inspiration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Timer, I've made a change according to your suggestion.


function statusSync() {
let stdout = execSync(`git status -s`).toString();
let status = { dirty: 0, untracked: 0 };
stdout.trim().split(/\r?\n/).forEach(file => {
if (file.substr(0, 2) === '??') status.untracked++;
else status.dirty++;
});
return status;
}

if (git) {
let status = statusSync();
if (status.dirty) {
console.error(
`Git repository has ${status.dirty} dirty ${status.dirty > 1 ? 'files' : 'file'}. ` +
'We cannot continue as you would lose all the changes in that file or directory. ' +
'Please push commit before and run this command again.'
);
process.exit(1);
}
}

console.log('Ejecting...');

const ownPath = paths.ownPath;
Expand Down