Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:(shadcn) Resolve directory location issues #5484

Closed

Conversation

abdulazizDhahri
Copy link

@abdulazizDhahri abdulazizDhahri commented Oct 21, 2024

Description

This PR addresses issues with incorrect directory resolution and import paths in the CLI package.

Related Issues

#5436

Summary of Changes

  • Fixed: Components now install in the correct location, respecting specified aliases.
  • Updated: transformImport function to correctly handle import paths, especially for utils imports.
  • Added: Tests

Key Code Changes

1. Directory Resolution Fix

Removed problematic code in update-files.ts:

// Removed:
if (file.target) {
  filePath = resolveTargetDir(projectInfo, config, file.target)
  targetDir = path.dirname(filePath)
}

This fix ensures correct installation location, but potential side effects need review.

2. Import Path Transformation
Updated transformImport in transform-import.ts

// Updated logic for @/lib/utils imports
if (moduleSpecifier.startsWith("@/lib/utils")) {
  importDeclaration.setModuleSpecifier(
    moduleSpecifier.replace(/^@\/lib\/utils/, config.aliases.utils)
  )
}

Removing the check for cnImport and still output the correct import path
This may has potential side effects need review

Alias Handling

if (!moduleSpecifier.startsWith("@/registry/")) {

	if (!moduleSpecifier.startsWith("@/lib/utils")) {
	  const alias = config.aliases.components.split("/")[0] ?? config.aliases.components.charAt(0)
 	 return moduleSpecifier.replace(/^@/, `${alias}`)
	}
	return moduleSpecifier
}
// rest

Files Changed

  • packages/shadcn/src/utils/transformers/transform-import.ts
  • packages/shadcn/src/utils/updaters/update-files.ts
  • packages/shadcn/test/utils/__snapshots__/transform-import.test.ts.snap
  • packages/shadcn/test/utils/schema/__snapshots__/registry-resolve-items-tree.test.ts.snap
  • packages/shadcn/test/utils/transform-import.test.ts

Testing

  • All tests pass.
  • Manually tested with Remix and v0 import to ensure components are installed in the correct locations and import paths are accurate.

Please review the changes and let me know if any adjustments are needed.
Thanks

Copy link

vercel bot commented Oct 21, 2024

@abdulazizDhahri is attempting to deploy a commit to the shadcn-pro Team on Vercel.

A member of the Team first needs to authorize it.

@lucasfeliciano
Copy link

For reference:

#5433 (comment)

@abdulazizDhahri
Copy link
Author

The issue fixed in PR #5530 addresses dir location

@abdulazizDhahri abdulazizDhahri deleted the fix/cli-imports-and-dir branch October 24, 2024 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants