diff --git a/.changeset/moody-seas-compete.md b/.changeset/moody-seas-compete.md new file mode 100644 index 000000000..a8df01892 --- /dev/null +++ b/.changeset/moody-seas-compete.md @@ -0,0 +1,5 @@ +--- +"shadcn-svelte": patch +--- + +fix: Allow a registry dependency to optionally be overwritten diff --git a/packages/cli/src/commands/add.ts b/packages/cli/src/commands/add.ts index f617223e2..93cd116c5 100644 --- a/packages/cli/src/commands/add.ts +++ b/packages/cli/src/commands/add.ts @@ -87,9 +87,16 @@ async function runAdd(cwd: string, config: Config, options: AddOptions) { const registryIndex = await getRegistryIndex(); - let selectedComponents = options.all ? registryIndex.map(({ name }) => name) : options.components; + let selectedComponents = new Set( + options.all ? registryIndex.map(({ name }) => name) : options.components + ); - if (selectedComponents === undefined || selectedComponents.length === 0) { + const registryDepMap = new Map(); + for (const item of registryIndex) { + registryDepMap.set(item.name, item.registryDependencies); + } + + if (selectedComponents === undefined || selectedComponents.size === 0) { const components = await p.multiselect({ message: `Which ${highlight("components")} would you like to install?`, maxItems: 10, @@ -107,13 +114,19 @@ async function runAdd(cwd: string, config: Config, options: AddOptions) { p.cancel("Operation cancelled."); process.exit(0); } - selectedComponents = components; + selectedComponents = new Set(components); } else { - const prettyList = prettifyList(selectedComponents); + const prettyList = prettifyList(Array.from(selectedComponents)); p.log.step(`Components to install:\n${color.gray(prettyList)}`); } - const tree = await resolveTree(registryIndex, selectedComponents); + // adds `registryDependency` to `selectedComponents` so that they can be individually overwritten + for (const name of selectedComponents) { + const regDeps = registryDepMap.get(name); + regDeps?.forEach((dep) => selectedComponents.add(dep)); + } + + const tree = await resolveTree(registryIndex, Array.from(selectedComponents), false); const payload = await fetchTree(config, tree); // const baseColor = await getRegistryBaseColor(config.tailwind.baseColor); @@ -126,7 +139,7 @@ async function runAdd(cwd: string, config: Config, options: AddOptions) { const existingComponents: string[] = []; const targetPath = options.path ? path.resolve(cwd, options.path) : undefined; for (const item of payload) { - if (selectedComponents.includes(item.name) === false) continue; + if (selectedComponents.has(item.name) === false) continue; const targetDir = getItemTargetPath(config, item, targetPath); if (targetDir === null) continue; @@ -189,7 +202,7 @@ async function runAdd(cwd: string, config: Config, options: AddOptions) { if (!options.overwrite && existingComponents.includes(item.name)) { // Only confirm overwrites for selected components and not transitive dependencies - if (selectedComponents.includes(item.name)) { + if (selectedComponents.has(item.name)) { p.log.warn( `Component ${highlight(item.name)} already exists at ${color.gray(componentPath)}` ); diff --git a/packages/cli/src/utils/registry/index.ts b/packages/cli/src/utils/registry/index.ts index ae0b3142c..3def2ec1a 100644 --- a/packages/cli/src/utils/registry/index.ts +++ b/packages/cli/src/utils/registry/index.ts @@ -68,7 +68,7 @@ export async function getRegistryBaseColor(baseColor: string) { } type RegistryIndex = v.Output; -export async function resolveTree(index: RegistryIndex, names: string[]) { +export async function resolveTree(index: RegistryIndex, names: string[], includeRegDeps = true) { const tree: RegistryIndex = []; for (const name of names) { @@ -80,7 +80,7 @@ export async function resolveTree(index: RegistryIndex, names: string[]) { tree.push(entry); - if (entry.registryDependencies) { + if (includeRegDeps && entry.registryDependencies) { const dependencies = await resolveTree(index, entry.registryDependencies); tree.push(...dependencies); }