Skip to content

Commit a257e7d

Browse files
milocosmopolitangaearon
authored andcommitted
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
1 parent ba0d0da commit a257e7d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

packages/react-scripts/scripts/eject.js

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ process.on('unhandledRejection', err => {
1818

1919
const fs = require('fs-extra');
2020
const path = require('path');
21+
const execSync = require('child_process').execSync;
2122
const spawnSync = require('cross-spawn').sync;
2223
const chalk = require('chalk');
2324
const inquirer = require('inquirer');
@@ -27,6 +28,17 @@ const createJestConfig = require('./utils/createJestConfig');
2728
const green = chalk.green;
2829
const cyan = chalk.cyan;
2930

31+
function getGitStatus() {
32+
try {
33+
let stdout = execSync(`git status --porcelain`, {
34+
stdio: ['pipe', 'pipe', 'ignore'],
35+
}).toString();
36+
return stdout.trim();
37+
} catch (e) {
38+
return '';
39+
}
40+
}
41+
3042
inquirer
3143
.prompt({
3244
type: 'confirm',
@@ -37,6 +49,19 @@ inquirer
3749
.then(answer => {
3850
if (!answer.shouldEject) {
3951
console.log(cyan('Close one! Eject aborted.'));
52+
return;
53+
}
54+
55+
const gitStatus = getGitStatus();
56+
if (gitStatus) {
57+
console.error(
58+
chalk.red(
59+
`This git repository has untracked files or uncommitted changes:\n\n` +
60+
gitStatus.split('\n').map(line => ' ' + line) +
61+
'\n\n' +
62+
'Remove untracked files, stash or commit any changes, and try again.'
63+
)
64+
);
4065
process.exit(1);
4166
}
4267

0 commit comments

Comments
 (0)