create-shadcn-js 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -31
- package/SECURITY.md +25 -0
- package/package.json +15 -5
- package/src/index.js +28 -40
- package/src/options.js +53 -0
- package/src/prompts.js +30 -2
- package/src/scaffold.js +106 -19
- package/src/shadcn.js +380 -29
- package/src/templates/app.js +17 -6
- package/src/templates/configs.js +116 -7
- package/src/templates/indexCss.js +83 -0
- package/src/utils.js +263 -22
package/src/templates/app.js
CHANGED
|
@@ -2,10 +2,21 @@
|
|
|
2
2
|
* Generates an interactive, polished App.jsx file.
|
|
3
3
|
* @param {object} options
|
|
4
4
|
* @param {boolean} [options.hasButton=true]
|
|
5
|
+
* @param {string} [options.addCommand="npx shadcn@4 add"]
|
|
5
6
|
* @param {boolean} [options.hasCard=false]
|
|
6
7
|
* @param {boolean} [options.hasBadge=false]
|
|
7
8
|
*/
|
|
8
|
-
export function getAppJsx({
|
|
9
|
+
export function getAppJsx({
|
|
10
|
+
hasButton = true,
|
|
11
|
+
addCommand = "npx shadcn@4 add",
|
|
12
|
+
hasCard = false,
|
|
13
|
+
hasBadge = false,
|
|
14
|
+
} = {}) {
|
|
15
|
+
// Callers normally pass a command prefix without the placeholder, but strip
|
|
16
|
+
// it here too: a literal "<component>" inside JSX is parsed as a tag and
|
|
17
|
+
// breaks the generated App.jsx.
|
|
18
|
+
const commandPrefix = addCommand.replace(/\s*<component>\s*$/, "")
|
|
19
|
+
|
|
9
20
|
if (hasButton) {
|
|
10
21
|
return `import { useState, useEffect } from "react"
|
|
11
22
|
import { Button } from "@/components/ui/button"
|
|
@@ -29,7 +40,7 @@ export default function App() {
|
|
|
29
40
|
<div className="w-full max-w-2xl space-y-8 text-center">
|
|
30
41
|
{/* Header Badge & Title */}
|
|
31
42
|
<div className="space-y-3">
|
|
32
|
-
<div className="inline-flex items-center gap-2 rounded-full border border-border bg-muted/60 px-3 py-1 text-xs font-medium text-muted-foreground shadow-
|
|
43
|
+
<div className="inline-flex items-center gap-2 rounded-full border border-border bg-muted/60 px-3 py-1 text-xs font-medium text-muted-foreground shadow-sm">
|
|
33
44
|
<span className="size-2 rounded-full bg-emerald-500 animate-pulse" />
|
|
34
45
|
Vite + React (JavaScript) + shadcn/ui
|
|
35
46
|
</div>
|
|
@@ -46,7 +57,7 @@ export default function App() {
|
|
|
46
57
|
<div className="flex items-center justify-between border-b border-border/60 pb-4">
|
|
47
58
|
<div className="text-left">
|
|
48
59
|
<h2 className="text-sm font-semibold">Interactive Component Demo</h2>
|
|
49
|
-
<p className="text-xs text-muted-foreground">Tailwind CSS theme tokens &
|
|
60
|
+
<p className="text-xs text-muted-foreground">Tailwind CSS theme tokens & shadcn/ui primitives</p>
|
|
50
61
|
</div>
|
|
51
62
|
<Button
|
|
52
63
|
variant="outline"
|
|
@@ -79,7 +90,7 @@ export default function App() {
|
|
|
79
90
|
{/* Code snippet suggestion */}
|
|
80
91
|
<div className="rounded-lg bg-muted/60 p-4 text-left font-mono text-xs text-muted-foreground border border-border/40">
|
|
81
92
|
<p className="text-foreground font-semibold mb-1">Add more components anytime:</p>
|
|
82
|
-
<code
|
|
93
|
+
<code>${commandPrefix} dialog dropdown-menu input table</code>
|
|
83
94
|
</div>
|
|
84
95
|
</div>
|
|
85
96
|
|
|
@@ -113,7 +124,7 @@ export default function App() {
|
|
|
113
124
|
return (
|
|
114
125
|
<div className="flex min-h-screen flex-col items-center justify-center p-6 bg-background text-foreground transition-colors duration-200">
|
|
115
126
|
<div className="w-full max-w-xl space-y-6 text-center">
|
|
116
|
-
<div className="inline-flex items-center gap-2 rounded-full border border-border bg-muted/60 px-3 py-1 text-xs font-medium text-muted-foreground shadow-
|
|
127
|
+
<div className="inline-flex items-center gap-2 rounded-full border border-border bg-muted/60 px-3 py-1 text-xs font-medium text-muted-foreground shadow-sm">
|
|
117
128
|
<span className="size-2 rounded-full bg-emerald-500 animate-pulse" />
|
|
118
129
|
Vite + React (JavaScript) + shadcn/ui Ready
|
|
119
130
|
</div>
|
|
@@ -126,7 +137,7 @@ export default function App() {
|
|
|
126
137
|
<div className="rounded-xl border border-border bg-card p-6 shadow-sm text-left font-mono text-xs text-muted-foreground space-y-2">
|
|
127
138
|
<p className="text-foreground font-semibold">Add your first shadcn component:</p>
|
|
128
139
|
<div className="p-3 bg-muted rounded-md text-foreground">
|
|
129
|
-
<code
|
|
140
|
+
<code>${commandPrefix} button</code>
|
|
130
141
|
</div>
|
|
131
142
|
</div>
|
|
132
143
|
</div>
|
package/src/templates/configs.js
CHANGED
|
@@ -64,26 +64,135 @@ export default defineConfig({
|
|
|
64
64
|
|
|
65
65
|
/**
|
|
66
66
|
* Generates tailwind.config.js for Tailwind CSS v3.
|
|
67
|
+
*
|
|
68
|
+
* This is intentionally self-contained: shadcn@2.3.0 writes v4-flavoured
|
|
69
|
+
* variables (oklch) that do not work with the v3 `hsl(var(--token))` theme,
|
|
70
|
+
* so the scaffold owns the v3 theme instead of relying on the legacy CLI.
|
|
67
71
|
*/
|
|
68
72
|
export function getTailwindConfigV3() {
|
|
69
|
-
return
|
|
73
|
+
return `import animate from "tailwindcss-animate"
|
|
74
|
+
|
|
75
|
+
/** @type {import('tailwindcss').Config} */
|
|
70
76
|
export default {
|
|
71
77
|
darkMode: ["class"],
|
|
72
|
-
content: [
|
|
73
|
-
"./index.html",
|
|
74
|
-
"./src/**/*.{js,ts,jsx,tsx}",
|
|
75
|
-
],
|
|
78
|
+
content: ["./index.html", "./src/**/*.{js,jsx}"],
|
|
76
79
|
theme: {
|
|
80
|
+
container: {
|
|
81
|
+
center: true,
|
|
82
|
+
padding: "2rem",
|
|
83
|
+
screens: {
|
|
84
|
+
"2xl": "1400px",
|
|
85
|
+
},
|
|
86
|
+
},
|
|
77
87
|
extend: {
|
|
88
|
+
colors: {
|
|
89
|
+
border: "hsl(var(--border))",
|
|
90
|
+
input: "hsl(var(--input))",
|
|
91
|
+
ring: "hsl(var(--ring))",
|
|
92
|
+
background: "hsl(var(--background))",
|
|
93
|
+
foreground: "hsl(var(--foreground))",
|
|
94
|
+
primary: {
|
|
95
|
+
DEFAULT: "hsl(var(--primary))",
|
|
96
|
+
foreground: "hsl(var(--primary-foreground))",
|
|
97
|
+
},
|
|
98
|
+
secondary: {
|
|
99
|
+
DEFAULT: "hsl(var(--secondary))",
|
|
100
|
+
foreground: "hsl(var(--secondary-foreground))",
|
|
101
|
+
},
|
|
102
|
+
destructive: {
|
|
103
|
+
DEFAULT: "hsl(var(--destructive))",
|
|
104
|
+
foreground: "hsl(var(--destructive-foreground))",
|
|
105
|
+
},
|
|
106
|
+
muted: {
|
|
107
|
+
DEFAULT: "hsl(var(--muted))",
|
|
108
|
+
foreground: "hsl(var(--muted-foreground))",
|
|
109
|
+
},
|
|
110
|
+
accent: {
|
|
111
|
+
DEFAULT: "hsl(var(--accent))",
|
|
112
|
+
foreground: "hsl(var(--accent-foreground))",
|
|
113
|
+
},
|
|
114
|
+
popover: {
|
|
115
|
+
DEFAULT: "hsl(var(--popover))",
|
|
116
|
+
foreground: "hsl(var(--popover-foreground))",
|
|
117
|
+
},
|
|
118
|
+
card: {
|
|
119
|
+
DEFAULT: "hsl(var(--card))",
|
|
120
|
+
foreground: "hsl(var(--card-foreground))",
|
|
121
|
+
},
|
|
122
|
+
sidebar: {
|
|
123
|
+
DEFAULT: "hsl(var(--sidebar-background))",
|
|
124
|
+
foreground: "hsl(var(--sidebar-foreground))",
|
|
125
|
+
primary: "hsl(var(--sidebar-primary))",
|
|
126
|
+
"primary-foreground": "hsl(var(--sidebar-primary-foreground))",
|
|
127
|
+
accent: "hsl(var(--sidebar-accent))",
|
|
128
|
+
"accent-foreground": "hsl(var(--sidebar-accent-foreground))",
|
|
129
|
+
border: "hsl(var(--sidebar-border))",
|
|
130
|
+
ring: "hsl(var(--sidebar-ring))",
|
|
131
|
+
},
|
|
132
|
+
},
|
|
78
133
|
borderRadius: {
|
|
79
134
|
lg: "var(--radius)",
|
|
80
135
|
md: "calc(var(--radius) - 2px)",
|
|
81
136
|
sm: "calc(var(--radius) - 4px)",
|
|
82
137
|
},
|
|
83
|
-
|
|
138
|
+
keyframes: {
|
|
139
|
+
"accordion-down": {
|
|
140
|
+
from: { height: "0" },
|
|
141
|
+
to: { height: "var(--radix-accordion-content-height)" },
|
|
142
|
+
},
|
|
143
|
+
"accordion-up": {
|
|
144
|
+
from: { height: "var(--radix-accordion-content-height)" },
|
|
145
|
+
to: { height: "0" },
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
animation: {
|
|
149
|
+
"accordion-down": "accordion-down 0.2s ease-out",
|
|
150
|
+
"accordion-up": "accordion-up 0.2s ease-out",
|
|
151
|
+
},
|
|
84
152
|
},
|
|
85
153
|
},
|
|
86
|
-
plugins: [],
|
|
154
|
+
plugins: [animate],
|
|
155
|
+
}
|
|
156
|
+
`
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Generates components.json for the legacy Tailwind CSS v3 + shadcn CLI 2.x setup.
|
|
161
|
+
*/
|
|
162
|
+
export function getComponentsJsonV3() {
|
|
163
|
+
return `{
|
|
164
|
+
"$schema": "https://ui.shadcn.com/schema.json",
|
|
165
|
+
"style": "new-york",
|
|
166
|
+
"rsc": false,
|
|
167
|
+
"tsx": false,
|
|
168
|
+
"tailwind": {
|
|
169
|
+
"config": "tailwind.config.js",
|
|
170
|
+
"css": "src/index.css",
|
|
171
|
+
"baseColor": "zinc",
|
|
172
|
+
"cssVariables": true,
|
|
173
|
+
"prefix": ""
|
|
174
|
+
},
|
|
175
|
+
"aliases": {
|
|
176
|
+
"components": "@/components",
|
|
177
|
+
"utils": "@/lib/utils",
|
|
178
|
+
"ui": "@/components/ui",
|
|
179
|
+
"lib": "@/lib",
|
|
180
|
+
"hooks": "@/hooks"
|
|
181
|
+
},
|
|
182
|
+
"iconLibrary": "lucide"
|
|
183
|
+
}
|
|
184
|
+
`
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Generates the JavaScript cn() helper used by shadcn components.
|
|
189
|
+
*/
|
|
190
|
+
export function getShadcnUtilsJs() {
|
|
191
|
+
return `import { clsx } from "clsx"
|
|
192
|
+
import { twMerge } from "tailwind-merge"
|
|
193
|
+
|
|
194
|
+
export function cn(...inputs) {
|
|
195
|
+
return twMerge(clsx(inputs))
|
|
87
196
|
}
|
|
88
197
|
`
|
|
89
198
|
}
|
|
@@ -11,5 +11,88 @@ export function getIndexCss(tailwindVersion = "v4") {
|
|
|
11
11
|
return `@tailwind base;
|
|
12
12
|
@tailwind components;
|
|
13
13
|
@tailwind utilities;
|
|
14
|
+
|
|
15
|
+
@layer base {
|
|
16
|
+
:root {
|
|
17
|
+
--background: 0 0% 100%;
|
|
18
|
+
--foreground: 240 10% 3.9%;
|
|
19
|
+
--card: 0 0% 100%;
|
|
20
|
+
--card-foreground: 240 10% 3.9%;
|
|
21
|
+
--popover: 0 0% 100%;
|
|
22
|
+
--popover-foreground: 240 10% 3.9%;
|
|
23
|
+
--primary: 240 5.9% 10%;
|
|
24
|
+
--primary-foreground: 0 0% 98%;
|
|
25
|
+
--secondary: 240 4.8% 95.9%;
|
|
26
|
+
--secondary-foreground: 240 5.9% 10%;
|
|
27
|
+
--muted: 240 4.8% 95.9%;
|
|
28
|
+
--muted-foreground: 240 3.8% 46.1%;
|
|
29
|
+
--accent: 240 4.8% 95.9%;
|
|
30
|
+
--accent-foreground: 240 5.9% 10%;
|
|
31
|
+
--destructive: 0 84.2% 60.2%;
|
|
32
|
+
--destructive-foreground: 0 0% 98%;
|
|
33
|
+
--border: 240 5.9% 90%;
|
|
34
|
+
--input: 240 5.9% 90%;
|
|
35
|
+
--ring: 240 10% 3.9%;
|
|
36
|
+
--chart-1: 12 76% 61%;
|
|
37
|
+
--chart-2: 173 58% 39%;
|
|
38
|
+
--chart-3: 197 37% 24%;
|
|
39
|
+
--chart-4: 43 74% 66%;
|
|
40
|
+
--chart-5: 27 87% 67%;
|
|
41
|
+
--radius: 0.5rem;
|
|
42
|
+
--sidebar-background: 0 0% 98%;
|
|
43
|
+
--sidebar-foreground: 240 5.3% 26.1%;
|
|
44
|
+
--sidebar-primary: 240 5.9% 10%;
|
|
45
|
+
--sidebar-primary-foreground: 0 0% 98%;
|
|
46
|
+
--sidebar-accent: 240 4.8% 95.9%;
|
|
47
|
+
--sidebar-accent-foreground: 240 5.9% 10%;
|
|
48
|
+
--sidebar-border: 220 13% 91%;
|
|
49
|
+
--sidebar-ring: 217.2 91.2% 59.8%;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.dark {
|
|
53
|
+
--background: 240 10% 3.9%;
|
|
54
|
+
--foreground: 0 0% 98%;
|
|
55
|
+
--card: 240 10% 3.9%;
|
|
56
|
+
--card-foreground: 0 0% 98%;
|
|
57
|
+
--popover: 240 10% 3.9%;
|
|
58
|
+
--popover-foreground: 0 0% 98%;
|
|
59
|
+
--primary: 0 0% 98%;
|
|
60
|
+
--primary-foreground: 240 5.9% 10%;
|
|
61
|
+
--secondary: 240 3.7% 15.9%;
|
|
62
|
+
--secondary-foreground: 0 0% 98%;
|
|
63
|
+
--muted: 240 3.7% 15.9%;
|
|
64
|
+
--muted-foreground: 240 5% 64.9%;
|
|
65
|
+
--accent: 240 3.7% 15.9%;
|
|
66
|
+
--accent-foreground: 0 0% 98%;
|
|
67
|
+
--destructive: 0 62.8% 30.6%;
|
|
68
|
+
--destructive-foreground: 0 0% 98%;
|
|
69
|
+
--border: 240 3.7% 15.9%;
|
|
70
|
+
--input: 240 3.7% 15.9%;
|
|
71
|
+
--ring: 240 4.9% 83.9%;
|
|
72
|
+
--chart-1: 220 70% 50%;
|
|
73
|
+
--chart-2: 160 60% 45%;
|
|
74
|
+
--chart-3: 30 80% 55%;
|
|
75
|
+
--chart-4: 280 65% 60%;
|
|
76
|
+
--chart-5: 340 75% 55%;
|
|
77
|
+
--sidebar-background: 240 5.9% 10%;
|
|
78
|
+
--sidebar-foreground: 240 4.8% 95.9%;
|
|
79
|
+
--sidebar-primary: 224.3 76.3% 48%;
|
|
80
|
+
--sidebar-primary-foreground: 0 0% 100%;
|
|
81
|
+
--sidebar-accent: 240 3.7% 15.9%;
|
|
82
|
+
--sidebar-accent-foreground: 240 4.8% 95.9%;
|
|
83
|
+
--sidebar-border: 240 3.7% 15.9%;
|
|
84
|
+
--sidebar-ring: 217.2 91.2% 59.8%;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
@layer base {
|
|
89
|
+
* {
|
|
90
|
+
@apply border-border;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
body {
|
|
94
|
+
@apply bg-background text-foreground;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
14
97
|
`
|
|
15
98
|
}
|
package/src/utils.js
CHANGED
|
@@ -1,7 +1,64 @@
|
|
|
1
1
|
import fs from "fs-extra"
|
|
2
2
|
import path from "node:path"
|
|
3
3
|
import spawn from "cross-spawn"
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
const ALLOWED_PACKAGE_MANAGERS = new Set(["npm", "pnpm", "yarn", "bun"])
|
|
6
|
+
const ALLOWED_TAILWIND_VERSIONS = new Set(["v4", "v3"])
|
|
7
|
+
const ALLOWED_INIT_MODES = new Set(["interactive", "quickstart"])
|
|
8
|
+
const ALLOWED_BASE_LIBRARIES = new Set(["base", "radix", "aria"])
|
|
9
|
+
const SAFE_IDENTIFIER_REGEX = /^[a-zA-Z0-9][a-zA-Z0-9_-]*$/
|
|
10
|
+
const SAFE_PROJECT_NAME_REGEX = /^[a-zA-Z0-9@._-]+$/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* CLI packages pinned by major so a future shadcn release cannot silently
|
|
14
|
+
* change the flags or component format used by the scaffolder.
|
|
15
|
+
*/
|
|
16
|
+
export const SHADCN_CLI_V4 = "shadcn@4"
|
|
17
|
+
export const SHADCN_CLI_V3 = "shadcn@2.3.0"
|
|
18
|
+
export const CREATE_VITE_PACKAGE = "create-vite@8"
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Builds the create-vite invocation with the selected package manager.
|
|
22
|
+
* @param {'npm' | 'pnpm' | 'yarn' | 'bun'} packageManager
|
|
23
|
+
* @param {string} folderName
|
|
24
|
+
* @returns {[string, string[]]}
|
|
25
|
+
*/
|
|
26
|
+
export function getCreateViteCommand(packageManager, folderName) {
|
|
27
|
+
const valid = validateProjectName(folderName)
|
|
28
|
+
if (typeof valid === "string") {
|
|
29
|
+
throw new Error(valid)
|
|
30
|
+
}
|
|
31
|
+
return getPackageManagerCommands(packageManager).dlx(CREATE_VITE_PACKAGE, [
|
|
32
|
+
folderName,
|
|
33
|
+
"--template",
|
|
34
|
+
"react",
|
|
35
|
+
"-y",
|
|
36
|
+
])
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Returns the shadcn CLI package for the selected Tailwind version.
|
|
41
|
+
* Tailwind v3 must use the legacy 2.x CLI, which is the last release that
|
|
42
|
+
* ships Tailwind v3-compatible components.
|
|
43
|
+
* @param {'v4' | 'v3'} tailwindVersion
|
|
44
|
+
*/
|
|
45
|
+
export function getShadcnCliPackage(tailwindVersion) {
|
|
46
|
+
return tailwindVersion === "v3" ? SHADCN_CLI_V3 : SHADCN_CLI_V4
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Human-readable command for adding a component later.
|
|
51
|
+
* @param {'npm' | 'pnpm' | 'yarn' | 'bun'} packageManager
|
|
52
|
+
* @param {'v4' | 'v3'} tailwindVersion
|
|
53
|
+
*/
|
|
54
|
+
export function getShadcnAddCommand(packageManager, tailwindVersion) {
|
|
55
|
+
const cli = getShadcnCliPackage(tailwindVersion)
|
|
56
|
+
// Reuse the same runner the CLI will actually execute. This keeps the
|
|
57
|
+
// copy-paste hint correct for Yarn 1, where `yarn dlx` does not exist and
|
|
58
|
+
// the scaffolder falls back to npx.
|
|
59
|
+
const { runner } = getPackageManagerCommands(packageManager)
|
|
60
|
+
return `${runner} ${cli} add <component>`
|
|
61
|
+
}
|
|
5
62
|
|
|
6
63
|
/**
|
|
7
64
|
* Detects the package manager used to run the CLI.
|
|
@@ -20,28 +77,43 @@ export function detectPackageManager() {
|
|
|
20
77
|
* @param {'npm' | 'pnpm' | 'yarn' | 'bun'} pm
|
|
21
78
|
*/
|
|
22
79
|
export function getPackageManagerCommands(pm) {
|
|
23
|
-
|
|
80
|
+
const safePm = sanitizePackageManager(pm)
|
|
81
|
+
switch (safePm) {
|
|
24
82
|
case "pnpm":
|
|
25
83
|
return {
|
|
26
|
-
install: ["pnpm", ["install"]],
|
|
27
|
-
add: (deps, isDev = false) => [
|
|
28
|
-
|
|
84
|
+
install: ["pnpm", ["install", "--ignore-scripts=false"]],
|
|
85
|
+
add: (deps, isDev = false) => [
|
|
86
|
+
"pnpm",
|
|
87
|
+
["add", ...(isDev ? ["-D"] : []), ...sanitizeDependencies(deps)],
|
|
88
|
+
],
|
|
89
|
+
run: (script) => ["pnpm", [sanitizeIdentifier(script)]],
|
|
29
90
|
dlx: (pkg, args = []) => ["pnpm", ["dlx", pkg, ...args]],
|
|
30
91
|
runner: "pnpm dlx",
|
|
31
92
|
}
|
|
32
|
-
case "yarn":
|
|
93
|
+
case "yarn": {
|
|
94
|
+
// `yarn dlx` only exists in Yarn 2+ (Berry). Yarn 1 projects need npx.
|
|
95
|
+
const isBerry = getYarnMajorVersion() >= 2
|
|
33
96
|
return {
|
|
34
|
-
install: ["yarn", []],
|
|
35
|
-
add: (deps, isDev = false) => [
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
97
|
+
install: ["yarn", ["install"]],
|
|
98
|
+
add: (deps, isDev = false) => [
|
|
99
|
+
"yarn",
|
|
100
|
+
["add", ...(isDev ? ["-D"] : []), ...sanitizeDependencies(deps)],
|
|
101
|
+
],
|
|
102
|
+
run: (script) => ["yarn", [sanitizeIdentifier(script)]],
|
|
103
|
+
dlx: isBerry
|
|
104
|
+
? (pkg, args = []) => ["yarn", ["dlx", pkg, ...args]]
|
|
105
|
+
: (pkg, args = []) => ["npx", ["--yes", pkg, ...args]],
|
|
106
|
+
runner: isBerry ? "yarn dlx" : "npx",
|
|
39
107
|
}
|
|
108
|
+
}
|
|
40
109
|
case "bun":
|
|
41
110
|
return {
|
|
42
111
|
install: ["bun", ["install"]],
|
|
43
|
-
add: (deps, isDev = false) => [
|
|
44
|
-
|
|
112
|
+
add: (deps, isDev = false) => [
|
|
113
|
+
"bun",
|
|
114
|
+
["add", ...(isDev ? ["-d"] : []), ...sanitizeDependencies(deps)],
|
|
115
|
+
],
|
|
116
|
+
run: (script) => ["bun", ["run", sanitizeIdentifier(script)]],
|
|
45
117
|
dlx: (pkg, args = []) => ["bunx", ["--bun", pkg, ...args]],
|
|
46
118
|
runner: "bunx --bun",
|
|
47
119
|
}
|
|
@@ -49,8 +121,11 @@ export function getPackageManagerCommands(pm) {
|
|
|
49
121
|
default:
|
|
50
122
|
return {
|
|
51
123
|
install: ["npm", ["install"]],
|
|
52
|
-
add: (deps, isDev = false) => [
|
|
53
|
-
|
|
124
|
+
add: (deps, isDev = false) => [
|
|
125
|
+
"npm",
|
|
126
|
+
["install", ...(isDev ? ["-D"] : []), ...sanitizeDependencies(deps)],
|
|
127
|
+
],
|
|
128
|
+
run: (script) => ["npm", ["run", sanitizeIdentifier(script)]],
|
|
54
129
|
dlx: (pkg, args = []) => ["npx", ["--yes", pkg, ...args]],
|
|
55
130
|
runner: "npx",
|
|
56
131
|
}
|
|
@@ -87,6 +162,8 @@ export function runCommandSync(cmd, args, options = {}) {
|
|
|
87
162
|
export function runCommandOutput(cmd, args, options = {}) {
|
|
88
163
|
const result = spawn.sync(cmd, args, {
|
|
89
164
|
encoding: "utf-8",
|
|
165
|
+
// Package-manager output can exceed Node's 1 MB default on large trees.
|
|
166
|
+
maxBuffer: 20 * 1024 * 1024,
|
|
90
167
|
...options,
|
|
91
168
|
})
|
|
92
169
|
|
|
@@ -94,31 +171,195 @@ export function runCommandOutput(cmd, args, options = {}) {
|
|
|
94
171
|
throw result.error
|
|
95
172
|
}
|
|
96
173
|
if (result.status !== 0) {
|
|
97
|
-
const
|
|
174
|
+
const details = [result.stderr, result.stdout]
|
|
175
|
+
.filter(Boolean)
|
|
176
|
+
.join("\n")
|
|
177
|
+
.trim()
|
|
178
|
+
const errorMsg = details || `exit code ${result.status}`
|
|
98
179
|
throw new Error(`Command "${cmd} ${args.join(" ")}" failed: ${errorMsg}`)
|
|
99
180
|
}
|
|
100
181
|
return result.stdout ? result.stdout.trim() : ""
|
|
101
182
|
}
|
|
102
183
|
|
|
103
184
|
/**
|
|
104
|
-
*
|
|
185
|
+
* Detects the installed Yarn major version.
|
|
186
|
+
* Returns 1 when Yarn is unavailable or its version cannot be parsed, which
|
|
187
|
+
* makes the caller fall back to npx instead of the Berry-only `yarn dlx`.
|
|
188
|
+
* @returns {number}
|
|
189
|
+
*/
|
|
190
|
+
function getYarnMajorVersion() {
|
|
191
|
+
try {
|
|
192
|
+
const result = spawn.sync("yarn", ["--version"], { encoding: "utf-8" })
|
|
193
|
+
if (result.error || result.status !== 0 || !result.stdout) return 1
|
|
194
|
+
const major = Number.parseInt(result.stdout.trim().split(".")[0], 10)
|
|
195
|
+
return Number.isNaN(major) ? 1 : major
|
|
196
|
+
} catch {
|
|
197
|
+
return 1
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Validates a project directory name to prevent path traversal and argument injection.
|
|
105
203
|
* @param {string} name
|
|
106
204
|
* @returns {string | boolean} Returns error message if invalid, or true if valid
|
|
107
205
|
*/
|
|
108
206
|
export function validateProjectName(name) {
|
|
109
|
-
if (!name || name.trim().length === 0) {
|
|
207
|
+
if (!name || typeof name !== "string" || name.trim().length === 0) {
|
|
110
208
|
return "Project name cannot be empty."
|
|
111
209
|
}
|
|
112
210
|
const trimmed = name.trim()
|
|
113
|
-
|
|
114
|
-
|
|
211
|
+
|
|
212
|
+
// Prevent flag injection (e.g. --template, -v)
|
|
213
|
+
if (trimmed.startsWith("-")) {
|
|
214
|
+
return "Project name cannot start with a hyphen (-)."
|
|
115
215
|
}
|
|
116
|
-
|
|
117
|
-
|
|
216
|
+
|
|
217
|
+
// "." and ".." resolve to an existing directory. The scaffolder empties the
|
|
218
|
+
// target directory, so accepting them could delete the user's project.
|
|
219
|
+
if (trimmed === "." || trimmed === ".." || /^\.+$/.test(trimmed)) {
|
|
220
|
+
return 'Project name cannot be "." or "..".'
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Prevent path traversal
|
|
224
|
+
if (trimmed.includes("..") || trimmed.includes("/") || trimmed.includes("\\")) {
|
|
225
|
+
return "Project name cannot contain path separators (/ or \\) or path traversal (..)."
|
|
118
226
|
}
|
|
227
|
+
|
|
228
|
+
// Windows strips trailing dots, which can alias a different directory.
|
|
229
|
+
if (trimmed.endsWith(".")) {
|
|
230
|
+
return "Project name cannot end with a dot."
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Check valid characters
|
|
234
|
+
if (!SAFE_PROJECT_NAME_REGEX.test(trimmed)) {
|
|
235
|
+
return "Project name can only contain alphanumeric characters, hyphens, underscores, dots, and @."
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Prevent Windows reserved filenames
|
|
239
|
+
const baseUpper = trimmed.split(".")[0].toUpperCase()
|
|
240
|
+
const reserved = new Set([
|
|
241
|
+
"CON", "PRN", "AUX", "NUL",
|
|
242
|
+
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
|
|
243
|
+
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"
|
|
244
|
+
])
|
|
245
|
+
if (reserved.has(baseUpper)) {
|
|
246
|
+
return `"${trimmed}" is a reserved system filename. Please choose another name.`
|
|
247
|
+
}
|
|
248
|
+
|
|
119
249
|
return true
|
|
120
250
|
}
|
|
121
251
|
|
|
252
|
+
/**
|
|
253
|
+
* Sanitizes and validates package manager against strict allowlist.
|
|
254
|
+
* @param {string} pm
|
|
255
|
+
* @returns {'npm' | 'pnpm' | 'yarn' | 'bun'}
|
|
256
|
+
*/
|
|
257
|
+
export function sanitizePackageManager(pm) {
|
|
258
|
+
if (pm === undefined || pm === null || pm === "") return "npm"
|
|
259
|
+
const normalized = String(pm).toLowerCase()
|
|
260
|
+
if (ALLOWED_PACKAGE_MANAGERS.has(normalized)) {
|
|
261
|
+
return normalized
|
|
262
|
+
}
|
|
263
|
+
throw new Error(
|
|
264
|
+
`Invalid package manager: "${pm}". Allowed values: npm, pnpm, yarn, bun.`
|
|
265
|
+
)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Sanitizes and validates Tailwind CSS version.
|
|
270
|
+
* @param {string} tw
|
|
271
|
+
* @returns {'v4' | 'v3'}
|
|
272
|
+
*/
|
|
273
|
+
export function sanitizeTailwindVersion(tw) {
|
|
274
|
+
if (tw === undefined || tw === null || tw === "") return "v4"
|
|
275
|
+
const normalized = String(tw).toLowerCase()
|
|
276
|
+
if (ALLOWED_TAILWIND_VERSIONS.has(normalized)) {
|
|
277
|
+
return normalized
|
|
278
|
+
}
|
|
279
|
+
throw new Error(`Invalid Tailwind CSS version: "${tw}". Allowed values: v4, v3.`)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Sanitizes and validates init mode.
|
|
284
|
+
* @param {string} mode
|
|
285
|
+
* @returns {'interactive' | 'quickstart'}
|
|
286
|
+
*/
|
|
287
|
+
export function sanitizeInitMode(mode) {
|
|
288
|
+
if (mode === undefined || mode === null || mode === "") return "interactive"
|
|
289
|
+
const normalized = String(mode).toLowerCase()
|
|
290
|
+
if (ALLOWED_INIT_MODES.has(normalized)) {
|
|
291
|
+
return normalized
|
|
292
|
+
}
|
|
293
|
+
throw new Error(
|
|
294
|
+
`Invalid init mode: "${mode}". Allowed values: interactive, quickstart.`
|
|
295
|
+
)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Sanitizes and validates base library.
|
|
300
|
+
* @param {string} [base]
|
|
301
|
+
* @returns {string | undefined}
|
|
302
|
+
*/
|
|
303
|
+
export function sanitizeBaseLibrary(base) {
|
|
304
|
+
if (!base) return undefined
|
|
305
|
+
const lower = base.toLowerCase()
|
|
306
|
+
if (ALLOWED_BASE_LIBRARIES.has(lower)) {
|
|
307
|
+
return lower
|
|
308
|
+
}
|
|
309
|
+
throw new Error(`Invalid base library: "${base}". Allowed values: base, radix, aria.`)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Sanitizes and validates preset name against injection.
|
|
314
|
+
* @param {string} [preset]
|
|
315
|
+
* @returns {string | undefined}
|
|
316
|
+
*/
|
|
317
|
+
export function sanitizePreset(preset) {
|
|
318
|
+
if (!preset) return undefined
|
|
319
|
+
if (SAFE_IDENTIFIER_REGEX.test(preset)) {
|
|
320
|
+
return preset
|
|
321
|
+
}
|
|
322
|
+
throw new Error(`Invalid preset name: "${preset}". Must contain only alphanumeric characters, hyphens, and underscores.`)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Sanitizes an array of dependencies or components.
|
|
327
|
+
* @param {string[]} deps
|
|
328
|
+
* @returns {string[]}
|
|
329
|
+
*/
|
|
330
|
+
export function sanitizeDependencies(deps) {
|
|
331
|
+
if (!Array.isArray(deps)) return []
|
|
332
|
+
return deps.map((d) => {
|
|
333
|
+
const isSafe =
|
|
334
|
+
typeof d === "string" &&
|
|
335
|
+
/^[a-zA-Z0-9@/._-]+$/.test(d) &&
|
|
336
|
+
// Reject flag injection and filesystem escapes: arguments are passed
|
|
337
|
+
// to npm/npx as an argv array, but "-x", "/abs" and "../x" are still
|
|
338
|
+
// interpreted as options or out-of-project paths.
|
|
339
|
+
!d.startsWith("-") &&
|
|
340
|
+
!d.startsWith("/") &&
|
|
341
|
+
!d.startsWith("./") &&
|
|
342
|
+
d !== "." &&
|
|
343
|
+
!d.includes("..")
|
|
344
|
+
if (!isSafe) {
|
|
345
|
+
throw new Error(`Invalid package identifier: "${d}"`)
|
|
346
|
+
}
|
|
347
|
+
return d
|
|
348
|
+
})
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Sanitizes a generic single identifier.
|
|
353
|
+
* @param {string} id
|
|
354
|
+
* @returns {string}
|
|
355
|
+
*/
|
|
356
|
+
export function sanitizeIdentifier(id) {
|
|
357
|
+
if (typeof id !== "string" || !SAFE_IDENTIFIER_REGEX.test(id)) {
|
|
358
|
+
throw new Error(`Invalid identifier: "${id}"`)
|
|
359
|
+
}
|
|
360
|
+
return id
|
|
361
|
+
}
|
|
362
|
+
|
|
122
363
|
/**
|
|
123
364
|
* Formats a path to display nicely to the user.
|
|
124
365
|
* @param {string} targetPath
|