Skip to content

Commit

Permalink
Check the app name before proceeding. (#628)
Browse files Browse the repository at this point in the history
* Check the app name before proceeding.

* Refactor.

* Use arrow function and template string.

* Remove comment.

* Rephrase the error.

* Add missing semicolons.
  • Loading branch information
mareksuscak authored and fson committed Sep 11, 2016
1 parent 55f965a commit d8b6529
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion global-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,17 @@ createApp(commands[0], argv.verbose, argv['scripts-version']);

function createApp(name, verbose, version) {
var root = path.resolve(name);
var appName = path.basename(root);

checkAppName(appName);

if (!pathExists.sync(name)) {
fs.mkdirSync(root);
} else if (!isSafeToCreateProjectIn(root)) {
console.log('The directory `' + name + '` contains file(s) that could conflict. Aborting.');
process.exit(1);
}

var appName = path.basename(root);
console.log(
'Creating a new React app in ' + root + '.'
);
Expand Down Expand Up @@ -167,6 +170,29 @@ function checkNodeVersion() {
}
}

function checkAppName(appName) {
// TODO: there should be a single place that holds the dependencies
var dependencies = ['react', 'react-dom'];
var devDependencies = ['react-scripts'];
var allDependencies = dependencies.concat(devDependencies).sort();

if (allDependencies.indexOf(appName) >= 0) {
console.error(
chalk.red(
`Can't use "${appName}" as the app name because a dependency with the same name exists.\n\n` +
`Following names ${chalk.red.bold('must not')} be used:\n\n`
)

+

chalk.cyan(
allDependencies.map(depName => ` ${depName}`).join('\n')
)
);
process.exit(1);
}
}

// 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
Expand Down

0 comments on commit d8b6529

Please sign in to comment.