mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-11 09:51:40 +00:00
chore: minor refactor and error message
This commit is contained in:
5
.changeset/rude-clowns-retire.md
Normal file
5
.changeset/rude-clowns-retire.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"shadcn": patch
|
||||
---
|
||||
|
||||
validate app name on create
|
||||
@@ -1,4 +1,3 @@
|
||||
import { basename, resolve } from "node:path"
|
||||
import path from "path"
|
||||
import { getPreset, getPresets, getRegistryItems } from "@/src/registry/api"
|
||||
import { configWithDefaults } from "@/src/registry/config"
|
||||
@@ -15,8 +14,8 @@ import { Command } from "commander"
|
||||
import dedent from "dedent"
|
||||
import open from "open"
|
||||
import prompts from "prompts"
|
||||
import validateProjectName from "validate-npm-package-name"
|
||||
|
||||
import { validateNpmName } from "../utils/validate-pkg"
|
||||
import { initOptionsSchema, runInit } from "./init"
|
||||
|
||||
const SHADCN_URL = "https://ui.shadcn.com"
|
||||
@@ -91,11 +90,13 @@ export const create = new Command()
|
||||
initial: opts.template ? `${opts.template}-app` : "my-app",
|
||||
format: (value: string) => value.trim(),
|
||||
validate: (name) => {
|
||||
const validation = validateNpmName(basename(resolve(name)))
|
||||
if (validation.valid) {
|
||||
const validation = validateProjectName(
|
||||
path.basename(path.resolve(name))
|
||||
)
|
||||
if (validation.validForNewPackages) {
|
||||
return true
|
||||
}
|
||||
return "Invalid project name: " + validation.problems[0]
|
||||
return "Invalid project name. Name should be lowercase, URL-friendly, and not start with a period or underscore."
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import validateProjectName from "validate-npm-package-name"
|
||||
|
||||
type ValidateNpmNameResult =
|
||||
| {
|
||||
valid: true
|
||||
}
|
||||
| {
|
||||
valid: false
|
||||
problems: string[]
|
||||
}
|
||||
|
||||
export function validateNpmName(name: string): ValidateNpmNameResult {
|
||||
const nameValidation = validateProjectName(name)
|
||||
if (nameValidation.validForNewPackages) {
|
||||
return { valid: true }
|
||||
}
|
||||
|
||||
return {
|
||||
valid: false,
|
||||
problems: [
|
||||
...(nameValidation.errors || []),
|
||||
...(nameValidation.warnings || []),
|
||||
],
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user