Skip to content

Commit d8b6529

Browse files
mareksuscakfson
authored andcommitted
Check the app name before proceeding. (#628)
* Check the app name before proceeding. * Refactor. * Use arrow function and template string. * Remove comment. * Rephrase the error. * Add missing semicolons.
1 parent 55f965a commit d8b6529

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

global-cli/index.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,17 @@ createApp(commands[0], argv.verbose, argv['scripts-version']);
6969

7070
function createApp(name, verbose, version) {
7171
var root = path.resolve(name);
72+
var appName = path.basename(root);
73+
74+
checkAppName(appName);
75+
7276
if (!pathExists.sync(name)) {
7377
fs.mkdirSync(root);
7478
} else if (!isSafeToCreateProjectIn(root)) {
7579
console.log('The directory `' + name + '` contains file(s) that could conflict. Aborting.');
7680
process.exit(1);
7781
}
7882

79-
var appName = path.basename(root);
8083
console.log(
8184
'Creating a new React app in ' + root + '.'
8285
);
@@ -167,6 +170,29 @@ function checkNodeVersion() {
167170
}
168171
}
169172

173+
function checkAppName(appName) {
174+
// TODO: there should be a single place that holds the dependencies
175+
var dependencies = ['react', 'react-dom'];
176+
var devDependencies = ['react-scripts'];
177+
var allDependencies = dependencies.concat(devDependencies).sort();
178+
179+
if (allDependencies.indexOf(appName) >= 0) {
180+
console.error(
181+
chalk.red(
182+
`Can't use "${appName}" as the app name because a dependency with the same name exists.\n\n` +
183+
`Following names ${chalk.red.bold('must not')} be used:\n\n`
184+
)
185+
186+
+
187+
188+
chalk.cyan(
189+
allDependencies.map(depName => ` ${depName}`).join('\n')
190+
)
191+
);
192+
process.exit(1);
193+
}
194+
}
195+
170196
// If project only contains files generated by GH, it’s safe.
171197
// We also special case IJ-based products .idea because it integrates with CRA:
172198
// https://github.com/facebookincubator/create-react-app/pull/368#issuecomment-243446094

0 commit comments

Comments
 (0)