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): handling of tsconfig aliases #5813

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-turtles-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn": patch
---

fix handling of aliases
44 changes: 33 additions & 11 deletions packages/shadcn/src/utils/get-project-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getPackageInfo } from "@/src/utils/get-package-info"
import fg from "fast-glob"
import fs from "fs-extra"
import { loadConfig } from "tsconfig-paths"
import { z } from "zod"

type ProjectInfo = {
framework: Framework
Expand All @@ -29,6 +30,12 @@ const PROJECT_SHARED_IGNORE = [
"build",
]

const TS_CONFIG_SCHEMA = z.object({
compilerOptions: z.object({
paths: z.record(z.string().or(z.array(z.string()))),
}),
})

export async function getProjectInfo(cwd: string): Promise<ProjectInfo | null> {
const [
configFiles,
Expand Down Expand Up @@ -153,7 +160,10 @@ export async function getTailwindConfigFile(cwd: string) {
export async function getTsConfigAliasPrefix(cwd: string) {
const tsConfig = await loadConfig(cwd)

if (tsConfig?.resultType === "failed" || !tsConfig?.paths) {
if (
tsConfig?.resultType === "failed" ||
!Object.entries(tsConfig?.paths).length
) {
return null
}

Expand All @@ -169,7 +179,8 @@ export async function getTsConfigAliasPrefix(cwd: string) {
}
}

return null
// Use the first alias as the prefix.
return Object.keys(tsConfig?.paths)?.[0].replace(/\/\*$/, "") ?? null
}

export async function isTypeScriptProject(cwd: string) {
Expand All @@ -182,19 +193,30 @@ export async function isTypeScriptProject(cwd: string) {
return files.length > 0
}

export async function getTsConfig() {
try {
const tsconfigPath = path.join("tsconfig.json")
const tsconfig = await fs.readJSON(tsconfigPath)
export async function getTsConfig(cwd: string) {
for (const fallback of [
"tsconfig.json",
"tsconfig.web.json",
"tsconfig.app.json",
]) {
const filePath = path.resolve(cwd, fallback)
if (!(await fs.pathExists(filePath))) {
continue
}

if (!tsconfig) {
throw new Error("tsconfig.json is missing")
// We can't use fs.readJSON because it doesn't support comments.
const contents = await fs.readFile(filePath, "utf8")
const cleanedContents = contents.replace(/\/\*\s*\*\//g, "")
const result = TS_CONFIG_SCHEMA.safeParse(JSON.parse(cleanedContents))

if (result.error) {
continue
}

return tsconfig
} catch (error) {
return null
return result.data
}

return null
}

export async function getProjectConfig(
Expand Down
Loading