Skip to content
Merged
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
28 changes: 28 additions & 0 deletions apps/v4/content/docs/06.cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,34 @@ Options:

---

## apply

Use the `apply` command to apply a preset to an existing project.

```bash
npx shadcn-vue@latest apply --preset nova
```

**Options**

```bash
Usage: shadcn-vue apply [options] [preset]

apply a preset to an existing project

Arguments:
preset the preset to apply

Options:
--preset <preset> preset configuration to apply
-y, --yes skip confirmation prompt. (default: false)
-c, --cwd <cwd> the working directory. defaults to the current directory.
-s, --silent mute output. (default: false)
-h, --help display help for command
```

---

## view

Use the `view` command to view items from the registry before installing them.
Expand Down
31 changes: 29 additions & 2 deletions apps/v4/registry/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ export function buildRegistryBase(
const baseItem = getBase(config.base)
const iconLibraryItem = getIconLibrary(config.iconLibrary)

// Mirrors shadcn-ui: if a user picked the same font for heading and body,
// collapse to "inherit" so we don't emit a redundant --font-heading var
// that's just an alias of --font-sans.
const normalizedFontHeading
= config.fontHeading === config.font ? "inherit" : config.fontHeading

if (!baseItem || !iconLibraryItem) {
throw new Error(
`Base "${config.base}" or icon library "${config.iconLibrary}" not found`,
Expand All @@ -339,7 +345,7 @@ export function buildRegistryBase(

// Build dependencies.
const dependencies = [
`shadcn@${SHADCN_VERSION}`,
`shadcn-vue@${SHADCN_VERSION}`,
"class-variance-authority",
"tw-animate-css",
...(baseItem.dependencies ?? []),
Expand All @@ -356,6 +362,10 @@ export function buildRegistryBase(
// that actually applies the font — the CLI's addFontImportPlugin only
// handles the Google Fonts @import url(...) line.
const fontItem = fonts.find(f => f.name === `font-${config.font}`)
const fontHeadingItem
= normalizedFontHeading !== "inherit"
? fonts.find(f => f.name === `font-${normalizedFontHeading}`)
: undefined

const themeVars: Record<string, string> = {
...(registryTheme.cssVars?.theme as Record<string, string> | undefined),
Expand All @@ -377,6 +387,19 @@ export function buildRegistryBase(
bodyRules[`@apply ${applyClass}`] = {}
}

// Emit --font-heading so the Tailwind v4 `font-heading` utility is wired
// up. When fontHeading is "inherit" (default) we alias it to the body
// font's CSS variable; otherwise we resolve the heading font's family
// from the web registry and emit it literally. The heading font's Google
// Fonts @import is pulled in CLI-side by addFontImportPlugin (see
// add-components.ts) via `config.fontHeading`.
if (normalizedFontHeading === "inherit") {
themeVars["--font-heading"] = `var(${fontItem?.font.variable ?? "--font-sans"})`
}
else if (fontHeadingItem) {
themeVars["--font-heading"] = fontHeadingItem.font.family
}

return {
name: `${config.base}-${config.style}`,
extends: "none",
Expand All @@ -385,6 +408,10 @@ export function buildRegistryBase(
style: `${config.base}-${config.style}`,
iconLibrary: iconLibraryItem.name,
font: config.font,
// Only persist fontHeading when it's a real override, so projects
// with the default ("inherit") don't gain a new components.json field.
...(normalizedFontHeading !== "inherit"
&& { fontHeading: normalizedFontHeading }),
rtl: config.rtl ?? false,
menuColor: config.menuColor,
menuAccent: config.menuAccent,
Expand All @@ -400,7 +427,7 @@ export function buildRegistryBase(
},
css: {
"@import \"tw-animate-css\"": {},
"@import \"shadcn/tailwind.css\"": {},
"@import \"shadcn-vue/tailwind.css\"": {},
"@layer base": {
"*": { "@apply border-border outline-ring/50": {} },
"body": bodyRules,
Expand Down
203 changes: 203 additions & 0 deletions packages/cli/src/commands/apply.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import { describe, expect, it, vi } from 'vitest'
import { encodePreset } from '@/src/preset/preset'
import {
getBase,
getInitCommand,
resolveApplyInitUrl,
resolveApplyPreset,
} from './apply'

vi.mock('@/src/utils/handle-error', () => ({
handleError: vi.fn(),
}))

vi.mock('@/src/utils/logger', () => ({
logger: {
error: vi.fn(),
warn: vi.fn(),
log: vi.fn(),
info: vi.fn(),
break: vi.fn(),
},
}))

describe('resolveApplyPreset', () => {
const baseOptions = {
cwd: '/tmp',
yes: false,
silent: false,
}

it('returns the positional preset when only a positional is provided', () => {
expect(
resolveApplyPreset({ ...baseOptions, positionalPreset: 'nova' }),
).toBe('nova')
})

it('returns the flag preset when only --preset is provided', () => {
expect(resolveApplyPreset({ ...baseOptions, preset: 'luma' })).toBe('luma')
})

it('returns the value when positional and flag agree', () => {
expect(
resolveApplyPreset({
...baseOptions,
positionalPreset: 'vega',
preset: 'vega',
}),
).toBe('vega')
})

it('trims surrounding whitespace from preset values', () => {
expect(
resolveApplyPreset({ ...baseOptions, positionalPreset: ' mira ' }),
).toBe('mira')
})

it('returns undefined when no preset is provided', () => {
expect(resolveApplyPreset(baseOptions)).toBeUndefined()
})

it('exits when positional and flag presets disagree', () => {
const exitSpy = vi
.spyOn(process, 'exit')
.mockImplementation(((_code?: number) => {
throw new Error('process.exit called')
}) as never)

try {
expect(() =>
resolveApplyPreset({
...baseOptions,
positionalPreset: 'vega',
preset: 'nova',
}),
).toThrow('process.exit called')

expect(exitSpy).toHaveBeenCalledWith(1)
}
finally {
// Guard with `finally` so a failed assertion above can't leak the
// spy into neighbouring tests. Vitest is not configured with
// `restoreMocks: true`, so this cleanup has to be explicit.
exitSpy.mockRestore()
}
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
})

describe('getBase', () => {
it('extracts the base segment from a composed style id', () => {
expect(getBase('reka-vega')).toBe('reka')
expect(getBase('reka-nova')).toBe('reka')
})

it('falls back to the default base for unknown / empty styles', () => {
expect(getBase(undefined)).toBe('reka')
expect(getBase('')).toBe('reka')
expect(getBase('mystery-style')).toBe('reka')
})
})

describe('getInitCommand', () => {
it('returns the bare init command when no preset is provided', () => {
expect(getInitCommand()).toBe('shadcn-vue init')
})

it('appends a simple preset name without quoting', () => {
expect(getInitCommand('nova')).toBe('shadcn-vue init --preset nova')
})

it('quotes preset values that contain shell-unsafe characters', () => {
expect(
getInitCommand('https://example.com/init?style=nova&base=reka'),
).toBe(
'shadcn-vue init --preset "https://example.com/init?style=nova&base=reka"',
)
})
})

describe('resolveApplyInitUrl', () => {
it('builds an init URL for a named preset and forces base + rtl', () => {
const url = resolveApplyInitUrl('nova', { base: 'reka', rtl: true })
expect(url).not.toBeNull()

const parsed = new URL(url!)
expect(parsed.pathname).toBe('/init')
expect(parsed.searchParams.get('base')).toBe('reka')
expect(parsed.searchParams.get('style')).toBe('nova')
expect(parsed.searchParams.get('iconLibrary')).toBe('lucide')
expect(parsed.searchParams.get('font')).toBe('geist-sans')
expect(parsed.searchParams.get('baseColor')).toBe('neutral')
expect(parsed.searchParams.get('rtl')).toBe('true')
})

it('always overrides the preset base with the project current base', () => {
// Even if a future named preset shipped with a different base, the
// current project base must win — applying a preset never silently
// switches the user's component library.
const url = resolveApplyInitUrl('vega', { base: 'reka', rtl: false })
expect(url).not.toBeNull()
const parsed = new URL(url!)
expect(parsed.searchParams.get('base')).toBe('reka')
expect(parsed.searchParams.get('rtl')).toBe('false')
})

it('builds an init URL for an encoded preset', () => {
const code = encodePreset({
style: 'nova',
baseColor: 'zinc',
theme: 'zinc',
font: 'inter',
iconLibrary: 'lucide',
})
const url = resolveApplyInitUrl(code, { base: 'reka', rtl: false })
expect(url).not.toBeNull()

const parsed = new URL(url!)
expect(parsed.searchParams.get('style')).toBe('nova')
expect(parsed.searchParams.get('baseColor')).toBe('zinc')
expect(parsed.searchParams.get('theme')).toBe('zinc')
expect(parsed.searchParams.get('font')).toBe('inter')
expect(parsed.searchParams.get('iconLibrary')).toBe('lucide')
// currentBase carried through.
expect(parsed.searchParams.get('base')).toBe('reka')
// Original preset code round-tripped for server-side compat fixups.
expect(parsed.searchParams.get('preset')).toBe(code)
})

it('passes a remote URL through with base + rtl overrides applied', () => {
const remote
= 'https://shadcn-vue.com/init?base=other&style=mira&font=figtree'
const url = resolveApplyInitUrl(remote, { base: 'reka', rtl: true })
expect(url).not.toBeNull()

const parsed = new URL(url!)
expect(parsed.searchParams.get('base')).toBe('reka')
expect(parsed.searchParams.get('rtl')).toBe('true')
// Non-overridden params come through unchanged.
expect(parsed.searchParams.get('style')).toBe('mira')
expect(parsed.searchParams.get('font')).toBe('figtree')
// First-party /init URLs are tracked.
expect(parsed.searchParams.get('track')).toBe('1')
})

it('always writes rtl=false on a remote URL when the project is LTR', () => {
const remote = 'https://shadcn-vue.com/init?base=reka&style=nova&rtl=true'
const url = resolveApplyInitUrl(remote, { base: 'reka', rtl: false })
const parsed = new URL(url!)
expect(parsed.searchParams.get('rtl')).toBe('false')
})

it('does not add track=1 to third-party URLs', () => {
const remote = 'https://example.com/init?style=nova'
const url = resolveApplyInitUrl(remote, { base: 'reka', rtl: false })
const parsed = new URL(url!)
expect(parsed.searchParams.has('track')).toBe(false)
})

it('returns null for an unknown named preset', () => {
expect(
resolveApplyInitUrl('not-a-real-preset', { base: 'reka', rtl: false }),
).toBeNull()
})
})
Loading
Loading