Skip to content

Commit

Permalink
fix: vitejs#2360. Use a package for project name validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkehappy committed Mar 6, 2021
1 parent 362ead5 commit dd487ea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
7 changes: 5 additions & 2 deletions packages/create-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
lightRed,
stripColors
} = require('kolorist')
const validatePackageName = require('validate-npm-package-name')

const cwd = process.cwd()

Expand Down Expand Up @@ -45,9 +46,11 @@ async function init() {
message: `Project name:`,
initial: 'vite-project'
})
if (!projectNameRE.test(name)) {
const validateResult = validatePackageName(name)
if (!validateResult.validForNewPackages) {
console.error(
`Project name "${name}" should only contain letters, numbers, underscores and dashes.`
// Only one name to be validated, so there will be only one error
validateResult.errors.length && validateResult.errors[0]
)
process.exit(1)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/create-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"enquirer": "^2.3.6",
"kolorist": "^1.2.9",
"minimist": "^1.2.5"
"minimist": "^1.2.5",
"validate-npm-package-name": "^3.0.0"
}
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,11 @@ builtin-modules@^3.1.0, builtin-modules@^3.2.0:
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.2.0.tgz#45d5db99e7ee5e6bc4f362e008bf917ab5049887"
integrity sha512-lGzLKcioL90C7wMczpkY0n/oART3MbBa8R9OFGE1rJxoVI86u4WAGfEk8Wjv10eKSyTHVGkSo3bvBylCEtk7LA==

builtins@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/builtins/-/builtins-1.0.3.tgz#cb94faeb61c8696451db36534e1422f94f0aee88"
integrity sha1-y5T662HIaWRR2zZTThQi+U8K7og=

[email protected]:
version "3.0.0"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"
Expand Down Expand Up @@ -7833,6 +7838,13 @@ validate-npm-package-license@^3.0.1:
spdx-correct "^3.0.0"
spdx-expression-parse "^3.0.0"

validate-npm-package-name@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/validate-npm-package-name/-/validate-npm-package-name-3.0.0.tgz#5fa912d81eb7d0c74afc140de7317f0ca7df437e"
integrity sha1-X6kS2B630MdK/BQN5zF/DKffQ34=
dependencies:
builtins "^1.0.3"

validator@^8.0.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/validator/-/validator-8.2.0.tgz#3c1237290e37092355344fef78c231249dab77b9"
Expand Down

0 comments on commit dd487ea

Please sign in to comment.