create-shadcn-js 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +246 -2
- package/SECURITY.md +25 -0
- package/bin/cli.js +8 -0
- package/package.json +62 -4
- package/src/index.js +137 -0
- package/src/options.js +53 -0
- package/src/prompts.js +197 -0
- package/src/scaffold.js +235 -0
- package/src/shadcn.js +456 -0
- package/src/templates/app.js +148 -0
- package/src/templates/configs.js +235 -0
- package/src/templates/indexCss.js +98 -0
- package/src/utils.js +370 -0
package/src/shadcn.js
ADDED
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import fs from "fs-extra"
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import * as p from "@clack/prompts"
|
|
4
|
+
import pc from "picocolors"
|
|
5
|
+
import {
|
|
6
|
+
getPackageManagerCommands,
|
|
7
|
+
getShadcnAddCommand,
|
|
8
|
+
getShadcnCliPackage,
|
|
9
|
+
runCommandSync,
|
|
10
|
+
runCommandOutput,
|
|
11
|
+
sanitizeDependencies,
|
|
12
|
+
} from "./utils.js"
|
|
13
|
+
import { getAppJsx } from "./templates/app.js"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Reads components.json, returning null when it is missing or invalid.
|
|
17
|
+
* @param {string} targetDir
|
|
18
|
+
* @returns {object | null}
|
|
19
|
+
*/
|
|
20
|
+
function readComponentsJson(targetDir) {
|
|
21
|
+
const configPath = path.join(targetDir, "components.json")
|
|
22
|
+
if (!fs.existsSync(configPath)) return null
|
|
23
|
+
try {
|
|
24
|
+
return fs.readJsonSync(configPath)
|
|
25
|
+
} catch {
|
|
26
|
+
return null
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Resolves a project-relative path while keeping it inside the project root.
|
|
32
|
+
* @param {string} targetDir
|
|
33
|
+
* @param {string} relativePath
|
|
34
|
+
* @returns {string | null}
|
|
35
|
+
*/
|
|
36
|
+
function resolveProjectFile(targetDir, relativePath) {
|
|
37
|
+
if (typeof relativePath !== "string" || relativePath.length === 0) return null
|
|
38
|
+
const root = path.resolve(targetDir)
|
|
39
|
+
const resolved = path.resolve(root, relativePath)
|
|
40
|
+
if (!resolved.startsWith(root + path.sep)) return null
|
|
41
|
+
return resolved
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {string} filePath
|
|
46
|
+
* @returns {boolean} True when the path exists and is a regular file.
|
|
47
|
+
*/
|
|
48
|
+
function isExistingFile(filePath) {
|
|
49
|
+
try {
|
|
50
|
+
return fs.statSync(filePath).isFile()
|
|
51
|
+
} catch {
|
|
52
|
+
return false
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Validates the required shape of components.json. A parseable file is not
|
|
58
|
+
* necessarily a usable one: an interrupted init can leave `{}` or a config
|
|
59
|
+
* without aliases behind.
|
|
60
|
+
* @param {unknown} config
|
|
61
|
+
* @returns {string | null} A human-readable problem, or null when valid.
|
|
62
|
+
*/
|
|
63
|
+
function validateConfigShape(config) {
|
|
64
|
+
if (!config || typeof config !== "object" || Array.isArray(config)) {
|
|
65
|
+
return "expected a JSON object"
|
|
66
|
+
}
|
|
67
|
+
if (typeof config.style !== "string" || config.style.length === 0) {
|
|
68
|
+
return 'missing "style"'
|
|
69
|
+
}
|
|
70
|
+
if (
|
|
71
|
+
!config.tailwind ||
|
|
72
|
+
typeof config.tailwind !== "object" ||
|
|
73
|
+
Array.isArray(config.tailwind)
|
|
74
|
+
) {
|
|
75
|
+
return 'missing "tailwind" configuration'
|
|
76
|
+
}
|
|
77
|
+
if (
|
|
78
|
+
typeof config.tailwind.css !== "string" ||
|
|
79
|
+
config.tailwind.css.length === 0
|
|
80
|
+
) {
|
|
81
|
+
return 'missing "tailwind.css"'
|
|
82
|
+
}
|
|
83
|
+
if (
|
|
84
|
+
!config.aliases ||
|
|
85
|
+
typeof config.aliases !== "object" ||
|
|
86
|
+
Array.isArray(config.aliases)
|
|
87
|
+
) {
|
|
88
|
+
return 'missing "aliases"'
|
|
89
|
+
}
|
|
90
|
+
for (const key of ["components", "utils", "ui"]) {
|
|
91
|
+
if (
|
|
92
|
+
typeof config.aliases[key] !== "string" ||
|
|
93
|
+
!config.aliases[key].startsWith("@/")
|
|
94
|
+
) {
|
|
95
|
+
return `invalid "aliases.${key}"`
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return null
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Resolves the utils alias from components.json to a file inside src/.
|
|
103
|
+
* @param {string} targetDir
|
|
104
|
+
* @param {object} config
|
|
105
|
+
* @returns {{ jsPath: string, tsPath: string } | null}
|
|
106
|
+
*/
|
|
107
|
+
function resolveUtilsPaths(targetDir, config) {
|
|
108
|
+
const alias = config?.aliases?.utils
|
|
109
|
+
if (typeof alias !== "string" || !alias.startsWith("@/")) return null
|
|
110
|
+
|
|
111
|
+
const relative = alias.slice(2).replace(/\.(?:js|jsx|ts|tsx)$/, "")
|
|
112
|
+
const srcDir = path.resolve(targetDir, "src")
|
|
113
|
+
const basePath = path.resolve(srcDir, relative)
|
|
114
|
+
|
|
115
|
+
// Never write into (or outside) the src root itself: the utils alias must
|
|
116
|
+
// name a file inside src/, even if a hand-edited or registry-supplied
|
|
117
|
+
// components.json contains a traversal alias such as "@/lib/..".
|
|
118
|
+
if (!basePath.startsWith(srcDir + path.sep)) {
|
|
119
|
+
return null
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
jsPath: `${basePath}.js`,
|
|
124
|
+
tsPath: `${basePath}.ts`,
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Verifies that the stylesheet actually received shadcn's theme variables.
|
|
130
|
+
* components.json and the CSS file can both exist after an interrupted init
|
|
131
|
+
* while the stylesheet still only imports Tailwind, which would build but
|
|
132
|
+
* render components with no theme colors. Configs that explicitly disable
|
|
133
|
+
* CSS variables intentionally use inline colors instead.
|
|
134
|
+
* @param {string} cssPath
|
|
135
|
+
* @param {object} config
|
|
136
|
+
*/
|
|
137
|
+
function validateThemeTokens(cssPath, config) {
|
|
138
|
+
if (config?.tailwind?.cssVariables === false) return
|
|
139
|
+
|
|
140
|
+
// Ignore comments so a commented-out token cannot satisfy the check, and
|
|
141
|
+
// require a value so alias lines like `--color-background: var(...)` in
|
|
142
|
+
// `@theme inline` do not masquerade as the real `:root` definition.
|
|
143
|
+
const css = fs.readFileSync(cssPath, "utf-8").replace(/\/\*[\s\S]*?\*\//g, "")
|
|
144
|
+
const missing = ["--background", "--primary"].filter(
|
|
145
|
+
(token) => !new RegExp(`${token}\\s*:\\s*[^;{}]+`).test(css)
|
|
146
|
+
)
|
|
147
|
+
if (missing.length > 0) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
`shadcn/ui initialization did not finish: "${config.tailwind.css}" is missing theme variables (${missing.join(
|
|
150
|
+
", "
|
|
151
|
+
)}). Check your network connection and run the command again.`
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Verifies that the cn() helper only imports packages declared by the
|
|
158
|
+
* project. An init that wrote config files but failed while installing
|
|
159
|
+
* dependencies would otherwise be reported as successful.
|
|
160
|
+
* @param {string} targetDir
|
|
161
|
+
* @param {string} utilsPath
|
|
162
|
+
*/
|
|
163
|
+
function validateUtilsDependencies(targetDir, utilsPath) {
|
|
164
|
+
const packageJsonPath = path.join(targetDir, "package.json")
|
|
165
|
+
if (!fs.existsSync(packageJsonPath)) return
|
|
166
|
+
|
|
167
|
+
let pkg
|
|
168
|
+
try {
|
|
169
|
+
pkg = fs.readJsonSync(packageJsonPath)
|
|
170
|
+
} catch {
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const declared = {
|
|
175
|
+
...(pkg?.dependencies || {}),
|
|
176
|
+
...(pkg?.devDependencies || {}),
|
|
177
|
+
}
|
|
178
|
+
const utils = fs.readFileSync(utilsPath, "utf-8")
|
|
179
|
+
const required = []
|
|
180
|
+
if (/from\s+["']cn["']/.test(utils)) required.push("cn")
|
|
181
|
+
if (/from\s+["']clsx["']/.test(utils)) required.push("clsx")
|
|
182
|
+
if (/from\s+["']tailwind-merge["']/.test(utils)) required.push("tailwind-merge")
|
|
183
|
+
|
|
184
|
+
const missing = required.filter((name) => !declared[name])
|
|
185
|
+
if (missing.length > 0) {
|
|
186
|
+
throw new Error(
|
|
187
|
+
`shadcn/ui initialization did not finish: package.json is missing ${missing.join(
|
|
188
|
+
", "
|
|
189
|
+
)}. Check your network connection and run the command again.`
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* shadcn@latest detects the project language, but a stale detection (or a
|
|
196
|
+
* preset created for TypeScript) can still leave a utils.ts behind. Make the
|
|
197
|
+
* project consistently JavaScript before any components are added.
|
|
198
|
+
* @param {string} targetDir
|
|
199
|
+
* @param {object} config
|
|
200
|
+
* @returns {string} Path to the JavaScript utils file.
|
|
201
|
+
*/
|
|
202
|
+
function ensureJavaScriptUtils(targetDir, config) {
|
|
203
|
+
const paths = resolveUtilsPaths(targetDir, config)
|
|
204
|
+
if (!paths) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
'shadcn/ui initialization produced an invalid "aliases.utils" path. ' +
|
|
207
|
+
"Check your components.json and run the command again."
|
|
208
|
+
)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (fs.existsSync(paths.jsPath)) {
|
|
212
|
+
if (fs.existsSync(paths.tsPath)) fs.removeSync(paths.tsPath)
|
|
213
|
+
return paths.jsPath
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
let content = `export { cn } from "cn"\n`
|
|
217
|
+
if (fs.existsSync(paths.tsPath)) {
|
|
218
|
+
const tsContent = fs.readFileSync(paths.tsPath, "utf-8")
|
|
219
|
+
// shadcn 4's utils.ts is already valid JavaScript apart from the
|
|
220
|
+
// extension. Older output imports clsx/tailwind-merge directly.
|
|
221
|
+
if (!/from\s+["']cn["']/.test(tsContent)) {
|
|
222
|
+
content = `import { clsx } from "clsx"\nimport { twMerge } from "tailwind-merge"\n\nexport function cn(...inputs) {\n return twMerge(clsx(inputs))\n}\n`
|
|
223
|
+
}
|
|
224
|
+
fs.removeSync(paths.tsPath)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
fs.ensureDirSync(path.dirname(paths.jsPath))
|
|
228
|
+
fs.writeFileSync(paths.jsPath, content, "utf-8")
|
|
229
|
+
return paths.jsPath
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Forces components.json into JavaScript mode and verifies that the shadcn
|
|
234
|
+
* CLI actually finished writing a usable config.
|
|
235
|
+
*
|
|
236
|
+
* Exported for tests; it is intentionally side-effect only on the targetDir.
|
|
237
|
+
* @param {string} targetDir
|
|
238
|
+
* @returns {object}
|
|
239
|
+
*/
|
|
240
|
+
export function finalizeShadcnConfig(targetDir) {
|
|
241
|
+
const configPath = path.join(targetDir, "components.json")
|
|
242
|
+
const config = readComponentsJson(targetDir)
|
|
243
|
+
|
|
244
|
+
if (!config) {
|
|
245
|
+
throw new Error(
|
|
246
|
+
"shadcn/ui initialization did not finish: components.json is missing or invalid. " +
|
|
247
|
+
"Check your network connection and run the command again."
|
|
248
|
+
)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const shapeProblem = validateConfigShape(config)
|
|
252
|
+
if (shapeProblem) {
|
|
253
|
+
throw new Error(
|
|
254
|
+
`shadcn/ui initialization produced an invalid components.json (${shapeProblem}). ` +
|
|
255
|
+
"Check your network connection and run the command again."
|
|
256
|
+
)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const cssPath = resolveProjectFile(targetDir, config.tailwind.css)
|
|
260
|
+
if (!cssPath || !isExistingFile(cssPath)) {
|
|
261
|
+
throw new Error(
|
|
262
|
+
`shadcn/ui initialization did not finish: "${config.tailwind.css}" is missing. ` +
|
|
263
|
+
"Check your network connection and run the command again."
|
|
264
|
+
)
|
|
265
|
+
}
|
|
266
|
+
validateThemeTokens(cssPath, config)
|
|
267
|
+
|
|
268
|
+
if (
|
|
269
|
+
typeof config.tailwind.config === "string" &&
|
|
270
|
+
config.tailwind.config.length > 0
|
|
271
|
+
) {
|
|
272
|
+
const tailwindConfigPath = resolveProjectFile(
|
|
273
|
+
targetDir,
|
|
274
|
+
config.tailwind.config
|
|
275
|
+
)
|
|
276
|
+
if (!tailwindConfigPath || !isExistingFile(tailwindConfigPath)) {
|
|
277
|
+
throw new Error(
|
|
278
|
+
`shadcn/ui initialization did not finish: "${config.tailwind.config}" is missing. ` +
|
|
279
|
+
"Check your network connection and run the command again."
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let changed = false
|
|
285
|
+
if (config.tsx !== false) {
|
|
286
|
+
config.tsx = false
|
|
287
|
+
changed = true
|
|
288
|
+
}
|
|
289
|
+
// This scaffolder only targets client-side Vite projects.
|
|
290
|
+
if (config.rsc !== false) {
|
|
291
|
+
config.rsc = false
|
|
292
|
+
changed = true
|
|
293
|
+
}
|
|
294
|
+
if (changed) {
|
|
295
|
+
fs.writeJsonSync(configPath, config, { spaces: 2 })
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const utilsPath = ensureJavaScriptUtils(targetDir, config)
|
|
299
|
+
if (!fs.existsSync(utilsPath)) {
|
|
300
|
+
throw new Error(
|
|
301
|
+
"shadcn/ui initialization did not finish: src/lib/utils.js could not be created."
|
|
302
|
+
)
|
|
303
|
+
}
|
|
304
|
+
validateUtilsDependencies(targetDir, utilsPath)
|
|
305
|
+
|
|
306
|
+
return config
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Initializes shadcn/ui in the scaffolded project.
|
|
311
|
+
*
|
|
312
|
+
* Tailwind v4 uses the official shadcn 4.x CLI. Tailwind v3 uses the legacy
|
|
313
|
+
* 2.3.0 CLI (the last release with v3 components); the scaffold has already
|
|
314
|
+
* written v3-compatible config, so no legacy init is run.
|
|
315
|
+
*
|
|
316
|
+
* @param {object} options
|
|
317
|
+
*/
|
|
318
|
+
export async function initShadcn({
|
|
319
|
+
targetDir,
|
|
320
|
+
packageManager,
|
|
321
|
+
tailwindVersion = "v4",
|
|
322
|
+
initMode = "interactive",
|
|
323
|
+
nonInteractive = false,
|
|
324
|
+
base,
|
|
325
|
+
preset,
|
|
326
|
+
}) {
|
|
327
|
+
const pmCmds = getPackageManagerCommands(packageManager)
|
|
328
|
+
const cliPackage = getShadcnCliPackage(tailwindVersion)
|
|
329
|
+
|
|
330
|
+
if (tailwindVersion === "v3") {
|
|
331
|
+
if (base || preset) {
|
|
332
|
+
p.log.warn(
|
|
333
|
+
pc.yellow(
|
|
334
|
+
"Tailwind v3 uses the legacy shadcn CLI with Radix + new-york defaults; --base and --preset are ignored."
|
|
335
|
+
)
|
|
336
|
+
)
|
|
337
|
+
}
|
|
338
|
+
p.log.step(`${pc.cyan("◆")} Writing legacy Tailwind v3 shadcn configuration...`)
|
|
339
|
+
finalizeShadcnConfig(targetDir)
|
|
340
|
+
p.log.success(`${pc.green("✔")} shadcn/ui (v3) configuration ready.`)
|
|
341
|
+
return
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (initMode === "interactive" && !nonInteractive) {
|
|
345
|
+
p.log.step(`${pc.cyan("◆")} Launching official shadcn/ui CLI...`)
|
|
346
|
+
p.log.message(
|
|
347
|
+
pc.dim(" Select your component library, preset, and styles below:\n")
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
const initArgs = ["init", "--no-monorepo"]
|
|
351
|
+
if (base) initArgs.push("-b", base)
|
|
352
|
+
if (preset) initArgs.push("-p", preset)
|
|
353
|
+
|
|
354
|
+
try {
|
|
355
|
+
// Run shadcn init interactively with inherited stdio
|
|
356
|
+
const [runnerBin, runnerArgs] = pmCmds.dlx(cliPackage, initArgs)
|
|
357
|
+
runCommandSync(runnerBin, runnerArgs, { cwd: targetDir })
|
|
358
|
+
} catch (error) {
|
|
359
|
+
// The CLI can exit non-zero after writing a valid config (for example
|
|
360
|
+
// when the user answers "no" to an optional prompt). Only continue when
|
|
361
|
+
// the config is actually usable.
|
|
362
|
+
if (!readComponentsJson(targetDir)) {
|
|
363
|
+
throw new Error(
|
|
364
|
+
`shadcn/ui init failed: ${error.message}\n` +
|
|
365
|
+
`Run "npx ${cliPackage} init" inside the project to finish setup.`
|
|
366
|
+
)
|
|
367
|
+
}
|
|
368
|
+
p.log.warn(
|
|
369
|
+
pc.yellow("shadcn/ui exited early, but a valid components.json was written.")
|
|
370
|
+
)
|
|
371
|
+
}
|
|
372
|
+
} else {
|
|
373
|
+
// Quickstart / non-interactive preset
|
|
374
|
+
const selectedBase = base || "radix"
|
|
375
|
+
const selectedPreset = preset || "nova"
|
|
376
|
+
const spinner = p.spinner()
|
|
377
|
+
spinner.start(
|
|
378
|
+
`Initializing shadcn/ui with ${selectedBase} library & ${selectedPreset} preset...`
|
|
379
|
+
)
|
|
380
|
+
try {
|
|
381
|
+
const [runnerBin, runnerArgs] = pmCmds.dlx(cliPackage, [
|
|
382
|
+
"init",
|
|
383
|
+
"--no-monorepo",
|
|
384
|
+
"-b",
|
|
385
|
+
selectedBase,
|
|
386
|
+
"-p",
|
|
387
|
+
selectedPreset,
|
|
388
|
+
"-y",
|
|
389
|
+
])
|
|
390
|
+
runCommandOutput(runnerBin, runnerArgs, { cwd: targetDir })
|
|
391
|
+
} catch (error) {
|
|
392
|
+
spinner.stop(`${pc.red("✖")} Failed to initialize shadcn/ui.`)
|
|
393
|
+
throw new Error(
|
|
394
|
+
`shadcn/ui init failed: ${error.message}\n` +
|
|
395
|
+
`You can retry with: npx ${cliPackage} init -b ${selectedBase} -p ${selectedPreset} -y`
|
|
396
|
+
)
|
|
397
|
+
}
|
|
398
|
+
spinner.stop(`${pc.green("✔")} shadcn/ui initialized.`)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
finalizeShadcnConfig(targetDir)
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Adds default components to the project.
|
|
406
|
+
* @param {object} options
|
|
407
|
+
*/
|
|
408
|
+
export async function addComponents({
|
|
409
|
+
targetDir,
|
|
410
|
+
packageManager,
|
|
411
|
+
tailwindVersion = "v4",
|
|
412
|
+
components = ["button"],
|
|
413
|
+
}) {
|
|
414
|
+
if (!components || components.length === 0) return
|
|
415
|
+
|
|
416
|
+
if (!readComponentsJson(targetDir)) {
|
|
417
|
+
throw new Error(
|
|
418
|
+
"Cannot add components because components.json is missing. " +
|
|
419
|
+
"Re-run the shadcn init step (or run it manually inside the project)."
|
|
420
|
+
)
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const spinner = p.spinner()
|
|
424
|
+
spinner.start(`Adding shadcn component(s): ${pc.cyan(components.join(", "))}...`)
|
|
425
|
+
|
|
426
|
+
const pmCmds = getPackageManagerCommands(packageManager)
|
|
427
|
+
const cliPackage = getShadcnCliPackage(tailwindVersion)
|
|
428
|
+
const addCommand = getShadcnAddCommand(packageManager, tailwindVersion)
|
|
429
|
+
|
|
430
|
+
try {
|
|
431
|
+
const safeComponents = sanitizeDependencies(components)
|
|
432
|
+
const [runnerBin, runnerArgs] = pmCmds.dlx(cliPackage, [
|
|
433
|
+
"add",
|
|
434
|
+
...safeComponents,
|
|
435
|
+
"-y",
|
|
436
|
+
])
|
|
437
|
+
runCommandOutput(runnerBin, runnerArgs, { cwd: targetDir })
|
|
438
|
+
spinner.stop(`${pc.green("✔")} Added component(s): ${pc.cyan(components.join(", "))}`)
|
|
439
|
+
|
|
440
|
+
// Update App.jsx with interactive component showcase
|
|
441
|
+
const hasButton = components.includes("button")
|
|
442
|
+
const appJsxPath = path.join(targetDir, "src", "App.jsx")
|
|
443
|
+
fs.writeFileSync(
|
|
444
|
+
appJsxPath,
|
|
445
|
+
getAppJsx({ hasButton, addCommand }),
|
|
446
|
+
"utf-8"
|
|
447
|
+
)
|
|
448
|
+
} catch (error) {
|
|
449
|
+
spinner.stop(
|
|
450
|
+
`${pc.yellow("⚠")} Could not automatically add ${components.join(", ")}. You can add it later via: ${pc.cyan(
|
|
451
|
+
addCommand.replace(" <component>", ` ${components.join(" ")}`)
|
|
452
|
+
)}`
|
|
453
|
+
)
|
|
454
|
+
p.log.warn(pc.dim(` ${error.message}`))
|
|
455
|
+
}
|
|
456
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates an interactive, polished App.jsx file.
|
|
3
|
+
* @param {object} options
|
|
4
|
+
* @param {boolean} [options.hasButton=true]
|
|
5
|
+
* @param {string} [options.addCommand="npx shadcn@4 add"]
|
|
6
|
+
* @param {boolean} [options.hasCard=false]
|
|
7
|
+
* @param {boolean} [options.hasBadge=false]
|
|
8
|
+
*/
|
|
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
|
+
|
|
20
|
+
if (hasButton) {
|
|
21
|
+
return `import { useState, useEffect } from "react"
|
|
22
|
+
import { Button } from "@/components/ui/button"
|
|
23
|
+
|
|
24
|
+
export default function App() {
|
|
25
|
+
const [count, setCount] = useState(0)
|
|
26
|
+
const [isDark, setIsDark] = useState(() => {
|
|
27
|
+
return window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (isDark) {
|
|
32
|
+
document.documentElement.classList.add("dark")
|
|
33
|
+
} else {
|
|
34
|
+
document.documentElement.classList.remove("dark")
|
|
35
|
+
}
|
|
36
|
+
}, [isDark])
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<div className="flex min-h-screen flex-col items-center justify-center p-6 bg-background text-foreground transition-colors duration-200">
|
|
40
|
+
<div className="w-full max-w-2xl space-y-8 text-center">
|
|
41
|
+
{/* Header Badge & Title */}
|
|
42
|
+
<div className="space-y-3">
|
|
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">
|
|
44
|
+
<span className="size-2 rounded-full bg-emerald-500 animate-pulse" />
|
|
45
|
+
Vite + React (JavaScript) + shadcn/ui
|
|
46
|
+
</div>
|
|
47
|
+
<h1 className="text-4xl font-extrabold tracking-tight sm:text-5xl">
|
|
48
|
+
create-shadcn-js
|
|
49
|
+
</h1>
|
|
50
|
+
<p className="text-lg text-muted-foreground max-w-lg mx-auto">
|
|
51
|
+
Zero-config Vite + Tailwind CSS + shadcn/ui setup for JavaScript. Path aliases, configs, and components ready to roll.
|
|
52
|
+
</p>
|
|
53
|
+
</div>
|
|
54
|
+
|
|
55
|
+
{/* Interactive Showcase Card */}
|
|
56
|
+
<div className="rounded-xl border border-border bg-card p-6 shadow-sm space-y-6 text-card-foreground">
|
|
57
|
+
<div className="flex items-center justify-between border-b border-border/60 pb-4">
|
|
58
|
+
<div className="text-left">
|
|
59
|
+
<h2 className="text-sm font-semibold">Interactive Component Demo</h2>
|
|
60
|
+
<p className="text-xs text-muted-foreground">Tailwind CSS theme tokens & shadcn/ui primitives</p>
|
|
61
|
+
</div>
|
|
62
|
+
<Button
|
|
63
|
+
variant="outline"
|
|
64
|
+
size="sm"
|
|
65
|
+
onClick={() => setIsDark((prev) => !prev)}
|
|
66
|
+
>
|
|
67
|
+
{isDark ? "Light Mode" : "Dark Mode"}
|
|
68
|
+
</Button>
|
|
69
|
+
</div>
|
|
70
|
+
|
|
71
|
+
{/* Button Variants Preview */}
|
|
72
|
+
<div className="flex flex-wrap items-center justify-center gap-3">
|
|
73
|
+
<Button onClick={() => setCount((c) => c + 1)}>
|
|
74
|
+
Clicked {count} {count === 1 ? "time" : "times"}
|
|
75
|
+
</Button>
|
|
76
|
+
<Button variant="secondary" onClick={() => setCount(0)}>
|
|
77
|
+
Reset Count
|
|
78
|
+
</Button>
|
|
79
|
+
<Button variant="outline">
|
|
80
|
+
Outline
|
|
81
|
+
</Button>
|
|
82
|
+
<Button variant="ghost">
|
|
83
|
+
Ghost
|
|
84
|
+
</Button>
|
|
85
|
+
<Button variant="destructive">
|
|
86
|
+
Destructive
|
|
87
|
+
</Button>
|
|
88
|
+
</div>
|
|
89
|
+
|
|
90
|
+
{/* Code snippet suggestion */}
|
|
91
|
+
<div className="rounded-lg bg-muted/60 p-4 text-left font-mono text-xs text-muted-foreground border border-border/40">
|
|
92
|
+
<p className="text-foreground font-semibold mb-1">Add more components anytime:</p>
|
|
93
|
+
<code>${commandPrefix} dialog dropdown-menu input table</code>
|
|
94
|
+
</div>
|
|
95
|
+
</div>
|
|
96
|
+
|
|
97
|
+
{/* Footer info */}
|
|
98
|
+
<p className="text-xs text-muted-foreground">
|
|
99
|
+
Edit <code className="font-semibold text-foreground">src/App.jsx</code> to start building your application.
|
|
100
|
+
</p>
|
|
101
|
+
</div>
|
|
102
|
+
</div>
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
`
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Fallback when no components installed yet
|
|
109
|
+
return `import { useState, useEffect } from "react"
|
|
110
|
+
|
|
111
|
+
export default function App() {
|
|
112
|
+
const [isDark, setIsDark] = useState(() => {
|
|
113
|
+
return window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (isDark) {
|
|
118
|
+
document.documentElement.classList.add("dark")
|
|
119
|
+
} else {
|
|
120
|
+
document.documentElement.classList.remove("dark")
|
|
121
|
+
}
|
|
122
|
+
}, [isDark])
|
|
123
|
+
|
|
124
|
+
return (
|
|
125
|
+
<div className="flex min-h-screen flex-col items-center justify-center p-6 bg-background text-foreground transition-colors duration-200">
|
|
126
|
+
<div className="w-full max-w-xl space-y-6 text-center">
|
|
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">
|
|
128
|
+
<span className="size-2 rounded-full bg-emerald-500 animate-pulse" />
|
|
129
|
+
Vite + React (JavaScript) + shadcn/ui Ready
|
|
130
|
+
</div>
|
|
131
|
+
<h1 className="text-4xl font-extrabold tracking-tight">
|
|
132
|
+
create-shadcn-js
|
|
133
|
+
</h1>
|
|
134
|
+
<p className="text-base text-muted-foreground">
|
|
135
|
+
Your project is fully configured with Tailwind CSS, jsconfig.json, and shadcn/ui.
|
|
136
|
+
</p>
|
|
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">
|
|
138
|
+
<p className="text-foreground font-semibold">Add your first shadcn component:</p>
|
|
139
|
+
<div className="p-3 bg-muted rounded-md text-foreground">
|
|
140
|
+
<code>${commandPrefix} button</code>
|
|
141
|
+
</div>
|
|
142
|
+
</div>
|
|
143
|
+
</div>
|
|
144
|
+
</div>
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
`
|
|
148
|
+
}
|