-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: #2360. Add validation for project name. #2385
Conversation
@@ -31,6 +31,7 @@ const TEMPLATES = [ | |||
const renameFiles = { | |||
_gitignore: '.gitignore' | |||
} | |||
const projectNameRE = /^[A-Za-z0-9_-]*$/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is totally incorrect. Please use something like validate-npm-package-name
Vite depends on resolve.exports which wouldn't pass your regex, for example.
@@ -31,6 +32,7 @@ const TEMPLATES = [ | |||
const renameFiles = { | |||
_gitignore: '.gitignore' | |||
} | |||
const projectNameRE = /^[A-Za-z0-9_-]*$/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const projectNameRE = /^[A-Za-z0-9_-]*$/ |
This is no longer used.
packages/create-app/index.js
Outdated
const validateResult = validatePackageName(name) | ||
if (!validateResult.validForNewPackages) { | ||
console.error( | ||
// Only one name to be validated, so there will be only one error | ||
validateResult.errors.length && validateResult.errors[0] | ||
) | ||
process.exit(1) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const validateResult = validatePackageName(name) | |
if (!validateResult.validForNewPackages) { | |
console.error( | |
// Only one name to be validated, so there will be only one error | |
validateResult.errors.length && validateResult.errors[0] | |
) | |
process.exit(1) | |
} | |
const { errors } = validatePackageName(name); | |
if (errors) { | |
errors.unshift(`Invalid package name: ${name}`); | |
console.error(errors.join('\n - ')); | |
process.exit(1); | |
} |
While only one name will be validated, multiple errors can in fact be returned. For example, the name 'foobar '
will result in both name cannot contain leading or trailing spaces
and name can only contain URL-friendly characters
being returned. Multiple errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rschristian Thank you. I correct it now.
Thanks for the PR, but Also, we should allow user to create directories with any name, we just need to make sure the name used in |
@yyx990803 Apologies, Fair call on allowing any directory name, though 1dbf246 is not a valid solution as npm/yarn still won't appreciate users using I'd say it's probably beneficial to allow the lib which covers all edge cases to do this. Simpler than refining that regex and offloads the responsibility. |
Project name should not contain special characters like space. So we can exit when no suitable name provided.