fix: #2360. Add validation for project name.#2385
fix: #2360. Add validation for project name.#2385sunkehappy wants to merge 4 commits intovitejs:mainfrom
Conversation
| const renameFiles = { | ||
| _gitignore: '.gitignore' | ||
| } | ||
| const projectNameRE = /^[A-Za-z0-9_-]*$/ |
There was a problem hiding this comment.
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.
| const renameFiles = { | ||
| _gitignore: '.gitignore' | ||
| } | ||
| const projectNameRE = /^[A-Za-z0-9_-]*$/ |
There was a problem hiding this comment.
| 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.
| 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.
|
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.