Skip to content

Commit a2c5be9

Browse files
johann-sonntagbauergaearon
authored andcommitted
add project name validation (#1662)
* add project name validation * Tweak console output
1 parent 39d0952 commit a2c5be9

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

packages/create-react-app/index.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'use strict';
4040

4141
var chalk = require('chalk');
42+
var validateProjectName = require("validate-npm-package-name");
4243

4344
var currentNodeVersion = process.versions.node;
4445
if (currentNodeVersion.split('.')[0] < 4) {
@@ -97,6 +98,14 @@ if (typeof projectName === 'undefined') {
9798
process.exit(1);
9899
}
99100

101+
function printValidationResults(results) {
102+
if (typeof results !== 'undefined') {
103+
results.forEach(function (error) {
104+
console.error(chalk.red(' * ' + error));
105+
});
106+
}
107+
}
108+
100109
var hiddenProgram = new commander.Command()
101110
.option('--internal-testing-template <path-to-template>', '(internal usage only, DO NOT RELY ON THIS) ' +
102111
'use a non-standard application template')
@@ -304,11 +313,18 @@ function checkNodeVersion(packageName) {
304313
}
305314

306315
function checkAppName(appName) {
316+
var validationResult = validateProjectName(appName);
317+
if (!validationResult.validForNewPackages) {
318+
console.error('Could not create a project called ' + chalk.red('"' + appName + '"') + ' because of npm naming restrictions:');
319+
printValidationResults(validationResult.errors);
320+
printValidationResults(validationResult.warnings);
321+
process.exit(1);
322+
}
323+
307324
// TODO: there should be a single place that holds the dependencies
308325
var dependencies = ['react', 'react-dom'];
309326
var devDependencies = ['react-scripts'];
310327
var allDependencies = dependencies.concat(devDependencies).sort();
311-
312328
if (allDependencies.indexOf(appName) >= 0) {
313329
console.error(
314330
chalk.red(

packages/create-react-app/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"commander": "^2.9.0",
2525
"cross-spawn": "^4.0.0",
2626
"fs-extra": "^1.0.0",
27-
"semver": "^5.0.3"
27+
"semver": "^5.0.3",
28+
"validate-npm-package-name": "^3.0.0"
2829
}
2930
}

0 commit comments

Comments
 (0)