Skip to content

Commit 6b67f4c

Browse files
mggaearon
authored andcommitted
Don't delete error logs when install fails (#2705)
* don't delete error logs * Style nit * Fix the logic
1 parent b3527d7 commit 6b67f4c

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

Diff for: packages/create-react-app/createReactApp.js

+44-29
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ const os = require('os');
5252

5353
const packageJson = require('./package.json');
5454

55+
// These files should be allowed to remain on a failed install,
56+
// but then silently removed during the next create.
57+
const errorLogFilePatterns = [
58+
'npm-debug.log',
59+
'yarn-error.log',
60+
'yarn-debug.log',
61+
];
62+
5563
let projectName;
5664

5765
const program = new commander.Command(packageJson.name)
@@ -344,22 +352,12 @@ function run(
344352
console.log();
345353

346354
// On 'exit' we will delete these files from target directory.
347-
const knownGeneratedFiles = [
348-
'package.json',
349-
'npm-debug.log',
350-
'yarn-error.log',
351-
'yarn-debug.log',
352-
'node_modules',
353-
];
355+
const knownGeneratedFiles = ['package.json', 'node_modules'];
354356
const currentFiles = fs.readdirSync(path.join(root));
355357
currentFiles.forEach(file => {
356358
knownGeneratedFiles.forEach(fileToMatch => {
357-
// This will catch `(npm-debug|yarn-error|yarn-debug).log*` files
358-
// and the rest of knownGeneratedFiles.
359-
if (
360-
(fileToMatch.match(/.log/g) && file.indexOf(fileToMatch) === 0) ||
361-
file === fileToMatch
362-
) {
359+
// This remove all of knownGeneratedFiles.
360+
if (file === fileToMatch) {
363361
console.log(`Deleting generated file... ${chalk.cyan(file)}`);
364362
fs.removeSync(path.join(root, file));
365363
}
@@ -369,7 +367,7 @@ function run(
369367
if (!remainingFiles.length) {
370368
// Delete target folder if empty
371369
console.log(
372-
`Deleting ${chalk.cyan(`${appName} /`)} from ${chalk.cyan(
370+
`Deleting ${chalk.cyan(`${appName}/`)} from ${chalk.cyan(
373371
path.resolve(root, '..')
374372
)}`
375373
);
@@ -608,6 +606,8 @@ function setCaretRangeForRuntimeDeps(packageName) {
608606
}
609607

610608
// If project only contains files generated by GH, it’s safe.
609+
// Also, if project contains remnant error logs from a previous
610+
// installation, lets remove them now.
611611
// We also special case IJ-based products .idea because it integrates with CRA:
612612
// https://github.com/facebookincubator/create-react-app/pull/368#issuecomment-243446094
613613
function isSafeToCreateProjectIn(root, name) {
@@ -634,24 +634,39 @@ function isSafeToCreateProjectIn(root, name) {
634634

635635
const conflicts = fs
636636
.readdirSync(root)
637-
.filter(file => !validFiles.includes(file));
638-
if (conflicts.length < 1) {
639-
return true;
640-
}
637+
.filter(file => !validFiles.includes(file))
638+
// Don't treat log files from previous installation as conflicts
639+
.filter(
640+
file => !errorLogFilePatterns.some(pattern => file.indexOf(pattern) === 0)
641+
);
641642

642-
console.log(
643-
`The directory ${chalk.green(name)} contains files that could conflict:`
644-
);
645-
console.log();
646-
for (const file of conflicts) {
647-
console.log(` ${file}`);
643+
if (conflicts.length > 0) {
644+
console.log(
645+
`The directory ${chalk.green(name)} contains files that could conflict:`
646+
);
647+
console.log();
648+
for (const file of conflicts) {
649+
console.log(` ${file}`);
650+
}
651+
console.log();
652+
console.log(
653+
'Either try using a new directory name, or remove the files listed above.'
654+
);
655+
656+
return false;
648657
}
649-
console.log();
650-
console.log(
651-
'Either try using a new directory name, or remove the files listed above.'
652-
);
653658

654-
return false;
659+
// Remove any remnant files from a previous installation
660+
const currentFiles = fs.readdirSync(path.join(root));
661+
currentFiles.forEach(file => {
662+
errorLogFilePatterns.forEach(errorLogFilePattern => {
663+
// This will catch `(npm-debug|yarn-error|yarn-debug).log*` files
664+
if (file.indexOf(errorLogFilePattern) === 0) {
665+
fs.removeSync(path.join(root, file));
666+
}
667+
});
668+
});
669+
return true;
655670
}
656671

657672
function getProxy() {

0 commit comments

Comments
 (0)