Skip to content

Commit

Permalink
add project name validation (#1662)
Browse files Browse the repository at this point in the history
* add project name validation

* Tweak console output
  • Loading branch information
johann-sonntagbauer authored and gaearon committed Feb 28, 2017
1 parent 715968e commit c51a611
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 17 additions & 1 deletion packages/create-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'use strict';

var chalk = require('chalk');
var validateProjectName = require("validate-npm-package-name");

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

function printValidationResults(results) {
if (typeof results !== 'undefined') {
results.forEach(function (error) {
console.error(chalk.red(' * ' + error));
});
}
}

var hiddenProgram = new commander.Command()
.option('--internal-testing-template <path-to-template>', '(internal usage only, DO NOT RELY ON THIS) ' +
'use a non-standard application template')
Expand Down Expand Up @@ -304,11 +313,18 @@ function checkNodeVersion(packageName) {
}

function checkAppName(appName) {
var validationResult = validateProjectName(appName);
if (!validationResult.validForNewPackages) {
console.error('Could not create a project called ' + chalk.red('"' + appName + '"') + ' because of npm naming restrictions:');
printValidationResults(validationResult.errors);
printValidationResults(validationResult.warnings);
process.exit(1);
}

// 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(
Expand Down
3 changes: 2 additions & 1 deletion packages/create-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"commander": "^2.9.0",
"cross-spawn": "^4.0.0",
"fs-extra": "^1.0.0",
"semver": "^5.0.3"
"semver": "^5.0.3",
"validate-npm-package-name": "^3.0.0"
}
}

0 comments on commit c51a611

Please sign in to comment.