From 6b67f4cfd024854f206de015fbbd2c7694d8c0f2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Magn=C3=BAs=20=C3=96rn=20Gylfason?=
 <magnus.gylfason@gmail.com>
Date: Sat, 20 Jan 2018 19:07:26 +0000
Subject: [PATCH] Don't delete error logs when install fails (#2705)

* don't delete error logs

* Style nit

* Fix the logic
---
 packages/create-react-app/createReactApp.js | 73 +++++++++++++--------
 1 file changed, 44 insertions(+), 29 deletions(-)

diff --git a/packages/create-react-app/createReactApp.js b/packages/create-react-app/createReactApp.js
index c289f2d7c..10935928b 100755
--- a/packages/create-react-app/createReactApp.js
+++ b/packages/create-react-app/createReactApp.js
@@ -52,6 +52,14 @@ const os = require('os');
 
 const packageJson = require('./package.json');
 
+// These files should be allowed to remain on a failed install,
+// but then silently removed during the next create.
+const errorLogFilePatterns = [
+  'npm-debug.log',
+  'yarn-error.log',
+  'yarn-debug.log',
+];
+
 let projectName;
 
 const program = new commander.Command(packageJson.name)
@@ -344,22 +352,12 @@ function run(
       console.log();
 
       // On 'exit' we will delete these files from target directory.
-      const knownGeneratedFiles = [
-        'package.json',
-        'npm-debug.log',
-        'yarn-error.log',
-        'yarn-debug.log',
-        'node_modules',
-      ];
+      const knownGeneratedFiles = ['package.json', 'node_modules'];
       const currentFiles = fs.readdirSync(path.join(root));
       currentFiles.forEach(file => {
         knownGeneratedFiles.forEach(fileToMatch => {
-          // This will catch `(npm-debug|yarn-error|yarn-debug).log*` files
-          // and the rest of knownGeneratedFiles.
-          if (
-            (fileToMatch.match(/.log/g) && file.indexOf(fileToMatch) === 0) ||
-            file === fileToMatch
-          ) {
+          // This remove all of knownGeneratedFiles.
+          if (file === fileToMatch) {
             console.log(`Deleting generated file... ${chalk.cyan(file)}`);
             fs.removeSync(path.join(root, file));
           }
@@ -369,7 +367,7 @@ function run(
       if (!remainingFiles.length) {
         // Delete target folder if empty
         console.log(
-          `Deleting ${chalk.cyan(`${appName} /`)} from ${chalk.cyan(
+          `Deleting ${chalk.cyan(`${appName}/`)} from ${chalk.cyan(
             path.resolve(root, '..')
           )}`
         );
@@ -608,6 +606,8 @@ function setCaretRangeForRuntimeDeps(packageName) {
 }
 
 // If project only contains files generated by GH, it’s safe.
+// Also, if project contains remnant error logs from a previous
+// installation, lets remove them now.
 // We also special case IJ-based products .idea because it integrates with CRA:
 // https://github.com/facebookincubator/create-react-app/pull/368#issuecomment-243446094
 function isSafeToCreateProjectIn(root, name) {
@@ -634,24 +634,39 @@ function isSafeToCreateProjectIn(root, name) {
 
   const conflicts = fs
     .readdirSync(root)
-    .filter(file => !validFiles.includes(file));
-  if (conflicts.length < 1) {
-    return true;
-  }
+    .filter(file => !validFiles.includes(file))
+    // Don't treat log files from previous installation as conflicts
+    .filter(
+      file => !errorLogFilePatterns.some(pattern => file.indexOf(pattern) === 0)
+    );
 
-  console.log(
-    `The directory ${chalk.green(name)} contains files that could conflict:`
-  );
-  console.log();
-  for (const file of conflicts) {
-    console.log(`  ${file}`);
+  if (conflicts.length > 0) {
+    console.log(
+      `The directory ${chalk.green(name)} contains files that could conflict:`
+    );
+    console.log();
+    for (const file of conflicts) {
+      console.log(`  ${file}`);
+    }
+    console.log();
+    console.log(
+      'Either try using a new directory name, or remove the files listed above.'
+    );
+
+    return false;
   }
-  console.log();
-  console.log(
-    'Either try using a new directory name, or remove the files listed above.'
-  );
 
-  return false;
+  // Remove any remnant files from a previous installation
+  const currentFiles = fs.readdirSync(path.join(root));
+  currentFiles.forEach(file => {
+    errorLogFilePatterns.forEach(errorLogFilePattern => {
+      // This will catch `(npm-debug|yarn-error|yarn-debug).log*` files
+      if (file.indexOf(errorLogFilePattern) === 0) {
+        fs.removeSync(path.join(root, file));
+      }
+    });
+  });
+  return true;
 }
 
 function getProxy() {
-- 
GitLab