From 65f7b389ccedaf979575c287c576b0f387549211 Mon Sep 17 00:00:00 2001 From: Philip Roberts Date: Tue, 21 Jun 2016 20:08:49 +0100 Subject: [PATCH] Validate project name in generator to ensure it's a valid npm package name (#298) --- .../generator-react-server/generators/app/index.js | 14 +++++++++++++- packages/generator-react-server/package.json | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/generator-react-server/generators/app/index.js b/packages/generator-react-server/generators/app/index.js index 977b10dc1..a54a392cf 100644 --- a/packages/generator-react-server/generators/app/index.js +++ b/packages/generator-react-server/generators/app/index.js @@ -2,6 +2,7 @@ var yeoman = require('yeoman-generator'); var chalk = require('chalk'); var yosay = require('yosay'); +var validatePackageName = require('validate-npm-package-name'); module.exports = yeoman.Base.extend({ prompting: function () { @@ -13,7 +14,18 @@ module.exports = yeoman.Base.extend({ type: 'input', name: 'name', message: 'What would you like to call your app?', - default: this.appname + default: this.appname.trim().replace(/\s+/g, '-'), + validate: function (name) { + var validation = validatePackageName(name); + var warnings = validation.warnings || []; + var errors = validation.errors || []; + + if (validation.validForNewPackages) { + return true; + } + + return warnings.concat(errors).join('\n'); + } }, { type: 'confirm', name: 'dockerCfg', diff --git a/packages/generator-react-server/package.json b/packages/generator-react-server/package.json index c2e372f83..5d7e25ed8 100644 --- a/packages/generator-react-server/package.json +++ b/packages/generator-react-server/package.json @@ -18,6 +18,7 @@ ], "dependencies": { "chalk": "^1.0.0", + "validate-npm-package-name": "^2.2.2", "yeoman-generator": "^0.23.0", "yosay": "^1.0.0" },