Skip to content

Commit 1af66c2

Browse files
jherrshadcn
andauthored
Adding support for ~ in target specification (#4848)
* Adding support for ~ in target specification * test(shadcn): add a test for srcDir false * chore: changeset --------- Co-authored-by: shadcn <[email protected]>
1 parent 0993d98 commit 1af66c2

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

Diff for: .changeset/clever-swans-tan.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"shadcn": patch
3+
---
4+
5+
add support for ~ dir in target path

Diff for: packages/shadcn/src/utils/updaters/update-files.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ import { transformRsc } from "@/src/utils/transformers/transform-rsc"
1717
import { transformTwPrefixes } from "@/src/utils/transformers/transform-tw-prefix"
1818
import prompts from "prompts"
1919

20+
export function resolveTargetDir(
21+
projectInfo: Awaited<ReturnType<typeof getProjectInfo>>,
22+
config: Config,
23+
target: string
24+
) {
25+
if (target.startsWith("~/")) {
26+
return path.join(config.resolvedPaths.cwd, target.replace("~/", ""))
27+
}
28+
return projectInfo?.isSrcDir
29+
? path.join(config.resolvedPaths.cwd, "src", target)
30+
: path.join(config.resolvedPaths.cwd, target)
31+
}
32+
2033
export async function updateFiles(
2134
files: RegistryItem["files"],
2235
config: Config,
@@ -58,9 +71,7 @@ export async function updateFiles(
5871
let filePath = path.join(targetDir, fileName)
5972

6073
if (file.target) {
61-
filePath = projectInfo?.isSrcDir
62-
? path.join(config.resolvedPaths.cwd, "src", file.target)
63-
: path.join(config.resolvedPaths.cwd, file.target)
74+
filePath = resolveTargetDir(projectInfo, config, file.target)
6475
targetDir = path.dirname(filePath)
6576
}
6677

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { describe, expect, test } from "vitest"
2+
3+
import { resolveTargetDir } from "../../../src/utils/updaters/update-files"
4+
5+
describe("resolveTargetDir", () => {
6+
test("should handle a home target without a src directory", () => {
7+
const targetDir = resolveTargetDir(
8+
{
9+
isSrcDir: false,
10+
},
11+
{
12+
resolvedPaths: {
13+
cwd: "/foo/bar",
14+
},
15+
},
16+
"~/.env"
17+
)
18+
expect(targetDir).toBe("/foo/bar/.env")
19+
})
20+
21+
test("should handle a home target even with a src directory", () => {
22+
const targetDir = resolveTargetDir(
23+
{
24+
isSrcDir: true,
25+
},
26+
{
27+
resolvedPaths: {
28+
cwd: "/foo/bar",
29+
},
30+
},
31+
"~/.env"
32+
)
33+
expect(targetDir).toBe("/foo/bar/.env")
34+
})
35+
36+
test("should handle a simple target", () => {
37+
const targetDir = resolveTargetDir(
38+
{
39+
isSrcDir: false,
40+
},
41+
{
42+
resolvedPaths: {
43+
cwd: "/foo/bar",
44+
},
45+
},
46+
"./components/ui/button.tsx"
47+
)
48+
expect(targetDir).toBe("/foo/bar/components/ui/button.tsx")
49+
})
50+
51+
test("should handle a simple target with src directory", () => {
52+
const targetDir = resolveTargetDir(
53+
{
54+
isSrcDir: true,
55+
},
56+
{
57+
resolvedPaths: {
58+
cwd: "/foo/bar",
59+
},
60+
},
61+
"./components/ui/button.tsx"
62+
)
63+
expect(targetDir).toBe("/foo/bar/src/components/ui/button.tsx")
64+
})
65+
})

0 commit comments

Comments
 (0)