Skip to content

Commit 6699158

Browse files
authored
fix: handling of base style for add command (#8997)
* fix: handling of base style for add command * chore: changeset * fix: shadow config
1 parent 142cd8e commit 6699158

3 files changed

Lines changed: 40 additions & 8 deletions

File tree

.changeset/floppy-lies-smoke.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+
fix handling of base styles for add command

packages/shadcn/src/commands/add.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,20 @@ export const add = new Command()
9090
}
9191

9292
let itemType: z.infer<typeof registryItemTypeSchema> | undefined
93+
let shouldInstallBaseStyle = true
9394
if (components.length > 0) {
9495
const [registryItem] = await getRegistryItems([components[0]], {
9596
config: initialConfig,
9697
})
9798
itemType = registryItem?.type
99+
shouldInstallBaseStyle =
100+
itemType !== "registry:theme" && itemType !== "registry:style"
98101

99102
if (isUniversalRegistryItem(registryItem)) {
100-
await addComponents(components, initialConfig, options)
103+
await addComponents(components, initialConfig, {
104+
...options,
105+
baseStyle: shouldInstallBaseStyle,
106+
})
101107
return
102108
}
103109

@@ -170,11 +176,12 @@ export const add = new Command()
170176
force: true,
171177
defaults: false,
172178
skipPreflight: false,
173-
silent: options.silent || !hasNewRegistries,
179+
silent: options.silent && !hasNewRegistries,
174180
isNewProject: false,
175181
srcDir: options.srcDir,
176182
cssVariables: options.cssVariables,
177-
baseStyle: itemType !== "registry:theme",
183+
baseStyle: shouldInstallBaseStyle,
184+
baseColor: shouldInstallBaseStyle ? undefined : "neutral",
178185
components: options.components,
179186
})
180187
initHasRun = true
@@ -209,7 +216,8 @@ export const add = new Command()
209216
isNewProject: true,
210217
srcDir: options.srcDir,
211218
cssVariables: options.cssVariables,
212-
baseStyle: itemType !== "registry:theme",
219+
baseStyle: shouldInstallBaseStyle,
220+
baseColor: shouldInstallBaseStyle ? undefined : "neutral",
213221
components: options.components,
214222
})
215223
initHasRun = true
@@ -238,7 +246,7 @@ export const add = new Command()
238246
if (!initHasRun) {
239247
await addComponents(options.components, config, {
240248
...options,
241-
baseStyle: itemType !== "registry:theme",
249+
baseStyle: shouldInstallBaseStyle,
242250
})
243251
}
244252

packages/shadcn/src/commands/init.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
DEFAULT_TAILWIND_CONFIG,
2727
DEFAULT_TAILWIND_CSS,
2828
DEFAULT_UTILS,
29+
createConfig,
2930
getConfig,
3031
resolveConfigPaths,
3132
type Config,
@@ -161,15 +162,32 @@ export const init = new Command()
161162
if (components.length > 0) {
162163
// We don't know the full config at this point.
163164
// So we'll use a shadow config to fetch the first item.
164-
let shadowConfig = configWithDefaults({})
165+
let shadowConfig = configWithDefaults(
166+
createConfig({
167+
resolvedPaths: {
168+
cwd: options.cwd,
169+
},
170+
})
171+
)
165172

166173
// Check if there's a components.json file.
167174
// If so, we'll merge with our shadow config.
168175
const componentsJsonPath = path.resolve(options.cwd, "components.json")
169176
if (fsExtra.existsSync(componentsJsonPath)) {
170177
const existingConfig = await fsExtra.readJson(componentsJsonPath)
171178
const config = rawConfigSchema.partial().parse(existingConfig)
172-
shadowConfig = configWithDefaults(config)
179+
const baseConfig = createConfig({
180+
resolvedPaths: {
181+
cwd: options.cwd,
182+
},
183+
})
184+
shadowConfig = configWithDefaults({
185+
...config,
186+
resolvedPaths: {
187+
...baseConfig.resolvedPaths,
188+
cwd: options.cwd,
189+
},
190+
})
173191

174192
// Since components.json might not be valid at this point.
175193
// Temporarily rename components.json to allow preflight to run.
@@ -183,6 +201,7 @@ export const init = new Command()
183201
shadowConfig,
184202
{
185203
silent: true,
204+
writeFile: false,
186205
}
187206
)
188207
shadowConfig = updatedConfig
@@ -218,7 +237,7 @@ export const init = new Command()
218237
)} Project initialization completed.\nYou may now add components.`
219238
)
220239

221-
// We need when runninng with custom cwd.
240+
// We need when running with custom cwd.
222241
deleteFileBackup(path.resolve(options.cwd, "components.json"))
223242
logger.break()
224243
} catch (error) {

0 commit comments

Comments
 (0)