Skip to content

Commit

Permalink
fix: Allow a registry dependency to be optionally overwritten (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianGonz97 authored Apr 29, 2024
1 parent 9125246 commit ba14ef5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-seas-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn-svelte": patch
---

fix: Allow a registry dependency to optionally be overwritten
27 changes: 20 additions & 7 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]>();
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,
Expand All @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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)}`
);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/utils/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function getRegistryBaseColor(baseColor: string) {
}

type RegistryIndex = v.Output<typeof schemas.registryIndexSchema>;
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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit ba14ef5

Please sign in to comment.