Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/shadcn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@babel/preset-typescript": "^7.27.1",
"@dotenvx/dotenvx": "^1.48.4",
"@modelcontextprotocol/sdk": "^1.17.2",
"@types/validate-npm-package-name": "^4.0.2",
"browserslist": "^4.26.2",
"commander": "^14.0.0",
"cosmiconfig": "^9.0.0",
Expand All @@ -105,6 +106,7 @@
"stringify-object": "^5.0.0",
"ts-morph": "^26.0.0",
"tsconfig-paths": "^4.2.0",
"validate-npm-package-name": "^7.0.1",
"zod": "^3.24.1",
"zod-to-json-schema": "^3.24.6"
},
Expand Down
13 changes: 9 additions & 4 deletions packages/shadcn/src/commands/create.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { basename, resolve } from "node:path"
import path from "path"
import { getPreset, getPresets, getRegistryItems } from "@/src/registry/api"
import { configWithDefaults } from "@/src/registry/config"
Expand All @@ -15,6 +16,7 @@ import dedent from "dedent"
import open from "open"
import prompts from "prompts"

import { validateNpmName } from "../utils/validate-pkg"
import { initOptionsSchema, runInit } from "./init"

const SHADCN_URL = "https://ui.shadcn.com"
Expand Down Expand Up @@ -88,10 +90,13 @@ export const create = new Command()
message: "What is your project named?",
initial: opts.template ? `${opts.template}-app` : "my-app",
format: (value: string) => value.trim(),
validate: (value: string) =>
value.length > 128
? `Name should be less than 128 characters.`
: true,
validate: (name) => {
const validation = validateNpmName(basename(resolve(name)))
if (validation.valid) {
return true
}
return "Invalid project name: " + validation.problems[0]
},
})

if (!enteredName) {
Expand Down
25 changes: 25 additions & 0 deletions packages/shadcn/src/utils/validate-pkg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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 || []),
],
}
}
25 changes: 21 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.