Skip to content

Commit f258339

Browse files
yeasin2002shadcn
andauthored
fix(cli): validate project name using npm package name rules (#9161)
* fix(cli): #9160 updated CLI name validation * chore: minor refactor and error message --------- Co-authored-by: shadcn <m@shadcn.com>
1 parent c2fd847 commit f258339

4 files changed

Lines changed: 38 additions & 8 deletions

File tree

.changeset/rude-clowns-retire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"shadcn": patch
3+
---
4+
5+
validate app name on create

packages/shadcn/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"@babel/preset-typescript": "^7.27.1",
8383
"@dotenvx/dotenvx": "^1.48.4",
8484
"@modelcontextprotocol/sdk": "^1.17.2",
85+
"@types/validate-npm-package-name": "^4.0.2",
8586
"browserslist": "^4.26.2",
8687
"commander": "^14.0.0",
8788
"cosmiconfig": "^9.0.0",
@@ -105,6 +106,7 @@
105106
"stringify-object": "^5.0.0",
106107
"ts-morph": "^26.0.0",
107108
"tsconfig-paths": "^4.2.0",
109+
"validate-npm-package-name": "^7.0.1",
108110
"zod": "^3.24.1",
109111
"zod-to-json-schema": "^3.24.6"
110112
},

packages/shadcn/src/commands/create.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Command } from "commander"
1414
import dedent from "dedent"
1515
import open from "open"
1616
import prompts from "prompts"
17+
import validateProjectName from "validate-npm-package-name"
1718

1819
import { initOptionsSchema, runInit } from "./init"
1920

@@ -88,10 +89,15 @@ export const create = new Command()
8889
message: "What is your project named?",
8990
initial: opts.template ? `${opts.template}-app` : "my-app",
9091
format: (value: string) => value.trim(),
91-
validate: (value: string) =>
92-
value.length > 128
93-
? `Name should be less than 128 characters.`
94-
: true,
92+
validate: (name) => {
93+
const validation = validateProjectName(
94+
path.basename(path.resolve(name))
95+
)
96+
if (validation.validForNewPackages) {
97+
return true
98+
}
99+
return "Invalid project name. Name should be lowercase, URL-friendly, and not start with a period or underscore."
100+
},
95101
})
96102

97103
if (!enteredName) {

pnpm-lock.yaml

Lines changed: 21 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)