Ejecting should ensure you have clean git status (#2221)

* Ejecting should ensure you have clean git status

* Rename function

* Style

* Minor changes

- extract function
- exclude error output for missing git
- more descriptive error message
- no need to mutate answer
- fix answering "no" to return 0 exit code
This commit is contained in:
Milo Kang
2017-05-20 13:55:16 -04:00
committed by Dan Abramov
parent ba0d0daf9c
commit a257e7d0fa

View File

@@ -18,6 +18,7 @@ process.on('unhandledRejection', err => {
const fs = require('fs-extra');
const path = require('path');
const execSync = require('child_process').execSync;
const spawnSync = require('cross-spawn').sync;
const chalk = require('chalk');
const inquirer = require('inquirer');
@@ -27,6 +28,17 @@ const createJestConfig = require('./utils/createJestConfig');
const green = chalk.green;
const cyan = chalk.cyan;
function getGitStatus() {
try {
let stdout = execSync(`git status --porcelain`, {
stdio: ['pipe', 'pipe', 'ignore'],
}).toString();
return stdout.trim();
} catch (e) {
return '';
}
}
inquirer
.prompt({
type: 'confirm',
@@ -37,6 +49,19 @@ inquirer
.then(answer => {
if (!answer.shouldEject) {
console.log(cyan('Close one! Eject aborted.'));
return;
}
const gitStatus = getGitStatus();
if (gitStatus) {
console.error(
chalk.red(
`This git repository has untracked files or uncommitted changes:\n\n` +
gitStatus.split('\n').map(line => ' ' + line) +
'\n\n' +
'Remove untracked files, stash or commit any changes, and try again.'
)
);
process.exit(1);
}