@@ -10,6 +10,7 @@ import { getPackageInfo } from "@/src/utils/get-package-info"
1010import fg from "fast-glob"
1111import fs from "fs-extra"
1212import { loadConfig } from "tsconfig-paths"
13+ import { z } from "zod"
1314
1415type ProjectInfo = {
1516 framework : Framework
@@ -29,6 +30,12 @@ const PROJECT_SHARED_IGNORE = [
2930 "build" ,
3031]
3132
33+ const TS_CONFIG_SCHEMA = z . object ( {
34+ compilerOptions : z . object ( {
35+ paths : z . record ( z . string ( ) . or ( z . array ( z . string ( ) ) ) ) ,
36+ } ) ,
37+ } )
38+
3239export async function getProjectInfo ( cwd : string ) : Promise < ProjectInfo | null > {
3340 const [
3441 configFiles ,
@@ -153,7 +160,10 @@ export async function getTailwindConfigFile(cwd: string) {
153160export async function getTsConfigAliasPrefix ( cwd : string ) {
154161 const tsConfig = await loadConfig ( cwd )
155162
156- if ( tsConfig ?. resultType === "failed" || ! tsConfig ?. paths ) {
163+ if (
164+ tsConfig ?. resultType === "failed" ||
165+ ! Object . entries ( tsConfig ?. paths ) . length
166+ ) {
157167 return null
158168 }
159169
@@ -169,7 +179,8 @@ export async function getTsConfigAliasPrefix(cwd: string) {
169179 }
170180 }
171181
172- return null
182+ // Use the first alias as the prefix.
183+ return Object . keys ( tsConfig ?. paths ) ?. [ 0 ] . replace ( / \/ \* $ / , "" ) ?? null
173184}
174185
175186export async function isTypeScriptProject ( cwd : string ) {
@@ -182,19 +193,30 @@ export async function isTypeScriptProject(cwd: string) {
182193 return files . length > 0
183194}
184195
185- export async function getTsConfig ( ) {
186- try {
187- const tsconfigPath = path . join ( "tsconfig.json" )
188- const tsconfig = await fs . readJSON ( tsconfigPath )
196+ export async function getTsConfig ( cwd : string ) {
197+ for ( const fallback of [
198+ "tsconfig.json" ,
199+ "tsconfig.web.json" ,
200+ "tsconfig.app.json" ,
201+ ] ) {
202+ const filePath = path . resolve ( cwd , fallback )
203+ if ( ! ( await fs . pathExists ( filePath ) ) ) {
204+ continue
205+ }
189206
190- if ( ! tsconfig ) {
191- throw new Error ( "tsconfig.json is missing" )
207+ // We can't use fs.readJSON because it doesn't support comments.
208+ const contents = await fs . readFile ( filePath , "utf8" )
209+ const cleanedContents = contents . replace ( / \/ \* \s * \* \/ / g, "" )
210+ const result = TS_CONFIG_SCHEMA . safeParse ( JSON . parse ( cleanedContents ) )
211+
212+ if ( result . error ) {
213+ continue
192214 }
193215
194- return tsconfig
195- } catch ( error ) {
196- return null
216+ return result . data
197217 }
218+
219+ return null
198220}
199221
200222export async function getProjectConfig (
0 commit comments