Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions packages/create-react-app/createReactApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,7 @@ function createApp(name, verbose, version, template) {

checkAppName(appName);
fs.ensureDirSync(name);
if (!isSafeToCreateProjectIn(root)) {
console.log(
`The directory ${chalk.green(name)} contains files that could conflict.`
);
console.log('Try using a new directory name.');
if (!isSafeToCreateProjectIn(root, name)) {
process.exit(1);
}

Expand Down Expand Up @@ -571,7 +567,7 @@ function setCaretRangeForRuntimeDeps(packageName) {
// If project only contains files generated by GH, it’s safe.
// 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) {
function isSafeToCreateProjectIn(root, name) {
const validFiles = [
'.DS_Store',
'Thumbs.db',
Expand All @@ -585,7 +581,30 @@ function isSafeToCreateProjectIn(root) {
'.hgignore',
'.hgcheck',
];
return fs.readdirSync(root).every(file => validFiles.indexOf(file) >= 0);
console.log();
let conflicts = fs.readdirSync(root).map((file, index) => {
Copy link
Copy Markdown
Contributor

@Timer Timer Jul 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be accomplished more succinctly with a filter:

const conflicts = fs.readdirSync(root).filter(file => !validFiles.includes(file));

if (!validFiles.indexOf(file) >= 0) {
return file;
}
});

if (conflicts.length > 0) {
Copy link
Copy Markdown
Contributor

@Timer Timer Jul 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we invert this for less nesting?

if (conflicts.length < 1) {
  return true;
}

console.log(
`The directory ${chalk.green(
name
)} already exists and contains files that could cause conflicts:`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like "already exists" is redundant, the original message should be fine:

`The directory ${chalk.green(name)} contains files that could conflict.`

);
console.log();
console.log(JSON.stringify(conflicts, null, ' '));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of JSON output; can we list them instead?

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;
}

return true;
}

function checkIfOnline(useYarn) {
Expand Down