From d5a8de9de8065cc5b657a05c43618c37467091b6 Mon Sep 17 00:00:00 2001 From: milocosmopolitan Date: Fri, 19 May 2017 00:01:00 -0400 Subject: [PATCH 1/4] Ejecting should ensure you have clean git status --- packages/react-scripts/scripts/eject.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/react-scripts/scripts/eject.js b/packages/react-scripts/scripts/eject.js index 28dd41b8aaf..432ea121047 100644 --- a/packages/react-scripts/scripts/eject.js +++ b/packages/react-scripts/scripts/eject.js @@ -21,6 +21,7 @@ const path = require('path'); const spawnSync = require('cross-spawn').sync; const chalk = require('chalk'); const inquirer = require('inquirer'); +const execSync = require('child_process').execSync; const paths = require('../config/paths'); const createJestConfig = require('./utils/createJestConfig'); @@ -35,6 +36,30 @@ inquirer default: false, }) .then(answer => { + // Make sure there are no dirty git status + function statusSync() { + try { + let stdout = execSync(`git status --porcelain`).toString(); + let status = stdout + .trim() + .split(/\r?\n/) + .filter(file => file.substr(0, 2) === '??').length; + return status; + } catch (e) { + return false; + } + } + + const dirtyStatus = statusSync(); + if (dirtyStatus) { + console.error( + `This git repository has ${dirtyStatus} ${dirtyStatus > 1 ? 'files' : 'file'} with uncommitted changes.\n` + + 'Ejecting would cause these files to be overwritten. \n' + + 'Please commit your changes with `git commit` and then run this command again.' + ); + answer.shouldEject = false; + } + if (!answer.shouldEject) { console.log(cyan('Close one! Eject aborted.')); process.exit(1); From 64ba43ea1b529f84687c655b26cdd8a5df7c9b2c Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Sat, 20 May 2017 17:54:53 +0100 Subject: [PATCH 2/4] Rename function --- packages/react-scripts/scripts/eject.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react-scripts/scripts/eject.js b/packages/react-scripts/scripts/eject.js index 432ea121047..faaa9e6991e 100644 --- a/packages/react-scripts/scripts/eject.js +++ b/packages/react-scripts/scripts/eject.js @@ -37,7 +37,7 @@ inquirer }) .then(answer => { // Make sure there are no dirty git status - function statusSync() { + function hasUncommitedGitChanges() { try { let stdout = execSync(`git status --porcelain`).toString(); let status = stdout @@ -50,7 +50,7 @@ inquirer } } - const dirtyStatus = statusSync(); + const dirtyStatus = hasUncommitedGitChanges(); if (dirtyStatus) { console.error( `This git repository has ${dirtyStatus} ${dirtyStatus > 1 ? 'files' : 'file'} with uncommitted changes.\n` + From 96389285b9fc49af8699c0347637169776bb8b39 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Sat, 20 May 2017 17:56:02 +0100 Subject: [PATCH 3/4] Style --- packages/react-scripts/scripts/eject.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/react-scripts/scripts/eject.js b/packages/react-scripts/scripts/eject.js index faaa9e6991e..9b4e65b3599 100644 --- a/packages/react-scripts/scripts/eject.js +++ b/packages/react-scripts/scripts/eject.js @@ -43,8 +43,9 @@ inquirer let status = stdout .trim() .split(/\r?\n/) - .filter(file => file.substr(0, 2) === '??').length; - return status; + .filter(file => file.substr(0, 2) === '??') + .length; + return status > 0; } catch (e) { return false; } From c423696c1ea4c3cc40b2b638969728a625e19085 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Sat, 20 May 2017 18:27:09 +0100 Subject: [PATCH 4/4] 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 --- packages/react-scripts/scripts/eject.js | 47 ++++++++++++------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/packages/react-scripts/scripts/eject.js b/packages/react-scripts/scripts/eject.js index 9b4e65b3599..ce3cf419707 100644 --- a/packages/react-scripts/scripts/eject.js +++ b/packages/react-scripts/scripts/eject.js @@ -18,16 +18,27 @@ 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'); -const execSync = require('child_process').execSync; const paths = require('../config/paths'); 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', @@ -36,33 +47,21 @@ inquirer default: false, }) .then(answer => { - // Make sure there are no dirty git status - function hasUncommitedGitChanges() { - try { - let stdout = execSync(`git status --porcelain`).toString(); - let status = stdout - .trim() - .split(/\r?\n/) - .filter(file => file.substr(0, 2) === '??') - .length; - return status > 0; - } catch (e) { - return false; - } + if (!answer.shouldEject) { + console.log(cyan('Close one! Eject aborted.')); + return; } - const dirtyStatus = hasUncommitedGitChanges(); - if (dirtyStatus) { + const gitStatus = getGitStatus(); + if (gitStatus) { console.error( - `This git repository has ${dirtyStatus} ${dirtyStatus > 1 ? 'files' : 'file'} with uncommitted changes.\n` + - 'Ejecting would cause these files to be overwritten. \n' + - 'Please commit your changes with `git commit` and then run this command again.' + 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.' + ) ); - answer.shouldEject = false; - } - - if (!answer.shouldEject) { - console.log(cyan('Close one! Eject aborted.')); process.exit(1); }