fix(cli): break caused by non-existence of tsconfig.app.json (#38)

* fix(cli): break caused by non-existence of `tsconfig.app.json`

* feat: better
This commit is contained in:
Dunqing 2023-09-09 17:21:17 +08:00 committed by GitHub
parent ea1998f862
commit 529b2b4942
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
import path from 'node:path' import path from 'node:path'
import { existsSync } from 'node:fs'
import { cosmiconfig } from 'cosmiconfig' import { cosmiconfig } from 'cosmiconfig'
import { loadConfig } from 'tsconfig-paths' import { loadConfig } from 'tsconfig-paths'
import * as z from 'zod' import * as z from 'zod'
@ -62,16 +63,18 @@ export async function getConfig(cwd: string) {
export async function resolveConfigPaths(cwd: string, config: RawConfig) { export async function resolveConfigPaths(cwd: string, config: RawConfig) {
const TSCONFIG_PATH = config.framework === 'nuxt' ? '.nuxt/tsconfig.json' : './tsconfig.json' const TSCONFIG_PATH = config.framework === 'nuxt' ? '.nuxt/tsconfig.json' : './tsconfig.json'
// In new Vue project, tsconfig has references to tsconfig.app.json, which is causing the path not resolving correctly
const FALLBACK_TSCONFIG_PATH = './tsconfig.app.json'
// Read tsconfig.json. // Read tsconfig.json.
const tsconfigPath = path.resolve(cwd, TSCONFIG_PATH) const tsconfigPath = path.resolve(cwd, TSCONFIG_PATH)
let tsConfig = loadConfig(tsconfigPath) let tsConfig = loadConfig(tsconfigPath)
// If no paths were found, we load the fallback tsconfig // In new Vue project, tsconfig has references to tsconfig.app.json, which is causing the path not resolving correctly
if ('paths' in tsConfig && Object.keys(tsConfig.paths).length === 0) // If no paths were found, try to load tsconfig.app.json.
tsConfig = loadConfig(path.resolve(cwd, FALLBACK_TSCONFIG_PATH)) if ('paths' in tsConfig && Object.keys(tsConfig.paths).length === 0) {
const FALLBACK_TSCONFIG_PATH = path.resolve(cwd, './tsconfig.app.json')
if (existsSync(FALLBACK_TSCONFIG_PATH))
tsConfig = loadConfig(FALLBACK_TSCONFIG_PATH)
}
if (tsConfig.resultType === 'failed') { if (tsConfig.resultType === 'failed') {
throw new Error( throw new Error(