refactor: load jsconfig and remove root option

This commit is contained in:
Dunqing 2023-09-18 14:19:20 +08:00
parent d588ce03fb
commit 996114de19
4 changed files with 22 additions and 25 deletions

View File

@ -117,12 +117,6 @@ export async function promptForConfig(
{ title: 'Nuxt', value: 'nuxt' }, { title: 'Nuxt', value: 'nuxt' },
], ],
}, },
{
type: 'text',
name: 'root',
message: `Where is your ${highlight('source code')} folder located`,
initial: (prev, values) => defaultConfig?.root ?? values.framework === 'nuxt' ? '.' : 'src',
},
{ {
type: 'select', type: 'select',
name: 'style', name: 'style',
@ -181,7 +175,6 @@ export async function promptForConfig(
const config = rawConfigSchema.parse({ const config = rawConfigSchema.parse({
// $schema: 'https://ui.shadcn.com/schema.json', // $schema: 'https://ui.shadcn.com/schema.json',
root: options.root,
style: options.style, style: options.style,
typescript: options.typescript, typescript: options.typescript,
framework: options.framework, framework: options.framework,

View File

@ -23,7 +23,6 @@ const explorer = cosmiconfig('components', {
export const rawConfigSchema = z export const rawConfigSchema = z
.object({ .object({
$schema: z.string().optional(), $schema: z.string().optional(),
root: z.string(),
style: z.string(), style: z.string(),
typescript: z.boolean().default(false), typescript: z.boolean().default(false),
tailwind: z.object({ tailwind: z.object({
@ -65,35 +64,40 @@ export async function getConfig(cwd: string) {
export async function resolveConfigPaths(cwd: string, config: RawConfig) { export async function resolveConfigPaths(cwd: string, config: RawConfig) {
let tsConfig: ConfigLoaderResult | undefined let tsConfig: ConfigLoaderResult | undefined
let tsConfigPath = path.resolve(
cwd,
config.framework === 'nuxt' ? '.nuxt/tsconfig.json' : './tsconfig.json',
)
if (config.typescript) { if (config.typescript) {
const TSCONFIG_PATH = config.framework === 'nuxt' ? '.nuxt/tsconfig.json' : './tsconfig.json'
// Read tsconfig.json. // Read tsconfig.json.
const tsconfigPath = path.resolve(cwd, TSCONFIG_PATH) tsConfig = loadConfig(tsConfigPath)
tsConfig = loadConfig(tsconfigPath)
// In new Vue project, tsconfig has references to tsconfig.app.json, which is causing the path not resolving correctly // In new Vue project, tsconfig has references to tsconfig.app.json, which is causing the path not resolving correctly
// If no paths were found, try to load tsconfig.app.json. // If no paths were found, try to load tsconfig.app.json.
if ('paths' in tsConfig && Object.keys(tsConfig.paths).length === 0) { if ('paths' in tsConfig && Object.keys(tsConfig.paths).length === 0) {
const FALLBACK_TSCONFIG_PATH = path.resolve(cwd, './tsconfig.app.json') tsConfigPath = path.resolve(cwd, './tsconfig.app.json')
if (existsSync(FALLBACK_TSCONFIG_PATH)) if (existsSync(tsConfigPath))
tsConfig = loadConfig(FALLBACK_TSCONFIG_PATH) tsConfig = loadConfig(tsConfigPath)
}
}
else {
tsConfigPath = path.resolve(cwd, './jsconfig.json')
tsConfig = loadConfig(tsConfigPath)
} }
if (tsConfig.resultType === 'failed') { if (tsConfig.resultType === 'failed') {
throw new Error( throw new Error(
`Failed to load tsconfig.json. ${tsConfig.message ?? ''}`.trim(), `Failed to load ${tsConfigPath}. ${tsConfig.message ?? ''}`.trim(),
) )
} }
}
return configSchema.parse({ return configSchema.parse({
...config, ...config,
resolvedPaths: { resolvedPaths: {
tailwindConfig: path.resolve(cwd, config.tailwind.config), tailwindConfig: path.resolve(cwd, config.tailwind.config),
tailwindCss: path.resolve(cwd, config.tailwind.css), tailwindCss: path.resolve(cwd, config.tailwind.css),
utils: tsConfig ? await resolveImport(config.aliases.utils, tsConfig) : config.aliases.utils, utils: await resolveImport(config.aliases.utils, tsConfig),
components: tsConfig ? await resolveImport(config.aliases.components, tsConfig) : config.aliases.components, components: await resolveImport(config.aliases.components, tsConfig),
}, },
}) })
} }

View File

@ -1,4 +1,5 @@
import path from 'node:path' import path from 'node:path'
import process from 'node:process'
import { HttpsProxyAgent } from 'https-proxy-agent' import { HttpsProxyAgent } from 'https-proxy-agent'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import type * as z from 'zod' import type * as z from 'zod'
@ -129,7 +130,6 @@ export function getItemTargetPath(
return null return null
return path.join( return path.join(
config.typescript ? '' : config.root,
config.resolvedPaths[parent as keyof typeof config.resolvedPaths], config.resolvedPaths[parent as keyof typeof config.resolvedPaths],
type, type,
) )

View File

@ -18,7 +18,7 @@ interface File {
export async function transformSFC(file: File, config: Config) { export async function transformSFC(file: File, config: Config) {
let content = transformImport(file.content, config) let content = transformImport(file.content, config)
if (config.typescript) if (!config.typescript)
content = await transformByDetype(content, file.name) content = await transformByDetype(content, file.name)
return content return content