Skip to content

Commit

Permalink
Adding support for ~ in target specification (#4848)
Browse files Browse the repository at this point in the history
* Adding support for ~ in target specification

* test(shadcn): add a test for srcDir false

* chore: changeset

---------

Co-authored-by: shadcn <[email protected]>
  • Loading branch information
jherr and shadcn authored Sep 15, 2024
1 parent 0993d98 commit 1af66c2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-swans-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn": patch
---

add support for ~ dir in target path
17 changes: 14 additions & 3 deletions packages/shadcn/src/utils/updaters/update-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ import { transformRsc } from "@/src/utils/transformers/transform-rsc"
import { transformTwPrefixes } from "@/src/utils/transformers/transform-tw-prefix"
import prompts from "prompts"

export function resolveTargetDir(
projectInfo: Awaited<ReturnType<typeof getProjectInfo>>,
config: Config,
target: string
) {
if (target.startsWith("~/")) {
return path.join(config.resolvedPaths.cwd, target.replace("~/", ""))
}
return projectInfo?.isSrcDir
? path.join(config.resolvedPaths.cwd, "src", target)
: path.join(config.resolvedPaths.cwd, target)
}

export async function updateFiles(
files: RegistryItem["files"],
config: Config,
Expand Down Expand Up @@ -58,9 +71,7 @@ export async function updateFiles(
let filePath = path.join(targetDir, fileName)

if (file.target) {
filePath = projectInfo?.isSrcDir
? path.join(config.resolvedPaths.cwd, "src", file.target)
: path.join(config.resolvedPaths.cwd, file.target)
filePath = resolveTargetDir(projectInfo, config, file.target)
targetDir = path.dirname(filePath)
}

Expand Down
65 changes: 65 additions & 0 deletions packages/shadcn/test/utils/updaters/update-files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, expect, test } from "vitest"

import { resolveTargetDir } from "../../../src/utils/updaters/update-files"

describe("resolveTargetDir", () => {
test("should handle a home target without a src directory", () => {
const targetDir = resolveTargetDir(
{
isSrcDir: false,
},
{
resolvedPaths: {
cwd: "/foo/bar",
},
},
"~/.env"
)
expect(targetDir).toBe("/foo/bar/.env")
})

test("should handle a home target even with a src directory", () => {
const targetDir = resolveTargetDir(
{
isSrcDir: true,
},
{
resolvedPaths: {
cwd: "/foo/bar",
},
},
"~/.env"
)
expect(targetDir).toBe("/foo/bar/.env")
})

test("should handle a simple target", () => {
const targetDir = resolveTargetDir(
{
isSrcDir: false,
},
{
resolvedPaths: {
cwd: "/foo/bar",
},
},
"./components/ui/button.tsx"
)
expect(targetDir).toBe("/foo/bar/components/ui/button.tsx")
})

test("should handle a simple target with src directory", () => {
const targetDir = resolveTargetDir(
{
isSrcDir: true,
},
{
resolvedPaths: {
cwd: "/foo/bar",
},
},
"./components/ui/button.tsx"
)
expect(targetDir).toBe("/foo/bar/src/components/ui/button.tsx")
})
})

0 comments on commit 1af66c2

Please sign in to comment.