Skip to content

Commit 36d86b9

Browse files
chitchuTimer
authored andcommitted
Gracefully handle initial installation error (#1512)
* Gracefully handle initial installation error * Print out message when problem occurs * Delete project folder on errors * Fix directory deleting message Resolves #1505
1 parent 11851ee commit 36d86b9

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/create-react-app/index.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,30 @@ function run(root, appName, version, verbose, originalDirectory, template) {
182182

183183
install(allDependencies, verbose, function(code, command, args) {
184184
if (code !== 0) {
185-
console.error(chalk.cyan(command + ' ' + args.join(' ')) + ' failed');
185+
console.log();
186+
console.error('Aborting installation.', chalk.cyan(command + ' ' + args.join(' ')), 'has failed.');
187+
// On 'exit' we will delete these files from target directory.
188+
var knownGeneratedFiles = [
189+
'package.json', 'npm-debug.log', 'yarn-error.log', 'yarn-debug.log', 'node_modules'
190+
];
191+
var currentFiles = fs.readdirSync(path.join(root));
192+
currentFiles.forEach(function (file) {
193+
knownGeneratedFiles.forEach(function (fileToMatch) {
194+
// This will catch `(npm-debug|yarn-error|yarn-debug).log*` files
195+
// and the rest of knownGeneratedFiles.
196+
if ((fileToMatch.match(/.log/g) && file.indexOf(fileToMatch) === 0) || file === fileToMatch) {
197+
console.log('Deleting generated file...', chalk.cyan(file));
198+
fs.removeSync(path.join(root, file));
199+
}
200+
});
201+
});
202+
var remainingFiles = fs.readdirSync(path.join(root));
203+
if (!remainingFiles.length) {
204+
// Delete target folder if empty
205+
console.log('Deleting', chalk.cyan(appName + '/'), 'from', chalk.cyan(path.resolve(root, '..')));
206+
fs.removeSync(path.join(root));
207+
}
208+
console.log('Done.');
186209
process.exit(1);
187210
}
188211

tasks/e2e-installs.sh

+26
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,32 @@ cd test-app-fork
119119
# Check corresponding scripts version is installed.
120120
exists node_modules/react-scripts-fork
121121

122+
# ******************************************************************************
123+
# Test project folder is deleted on failing package installation
124+
# ******************************************************************************
125+
126+
cd $temp_app_path
127+
# we will install a non-existing package to simulate a failed installataion.
128+
create_react_app --scripts-version=`date +%s` test-app-should-not-exist || true
129+
# confirm that the project folder was deleted
130+
test ! -d test-app-should-not-exist
131+
132+
# ******************************************************************************
133+
# Test project folder is not deleted when creating app over existing folder
134+
# ******************************************************************************
135+
136+
cd $temp_app_path
137+
mkdir test-app-should-remain
138+
echo '## Hello' > ./test-app-should-remain/README.md
139+
# we will install a non-existing package to simulate a failed installataion.
140+
create_react_app --scripts-version=`date +%s` test-app-should-remain || true
141+
# confirm the file exist
142+
test -e test-app-should-remain/README.md
143+
# confirm only README.md is the only file in the directory
144+
if [ "$(ls -1 ./test-app-should-remain | wc -l | tr -d '[:space:]')" != "1" ]; then
145+
false
146+
fi
147+
122148
# ******************************************************************************
123149
# Test nested folder path as the project name
124150
# ******************************************************************************

0 commit comments

Comments
 (0)