refactor: use get-tsconfig instead of tsconfig-paths

refactor: replace `node:path` with `pathe`
chore: add `tinyexec` instead of `execa`
This commit is contained in:
Sadegh Barati 2024-12-05 00:00:10 +03:30
parent 3f0ded8e7f
commit 8a8d1ef20a
20 changed files with 60 additions and 73 deletions

View File

@ -60,6 +60,7 @@
"diff": "^7.0.0",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
"get-tsconfig": "^4.8.1",
"lodash-es": "^4.17.21",
"magic-string": "^0.30.13",
"nypm": "^0.3.12",
@ -72,8 +73,8 @@
"reka-ui": "catalog:",
"stringify-object": "^5.0.0",
"tailwindcss": "^3.4.16",
"tinyexec": "^0.3.1",
"ts-morph": "^24.0.0",
"tsconfig-paths": "^4.2.0",
"undici": "^7.0.0",
"vue-metamorph": "3.2.0",
"zod": "^3.23.8"

View File

@ -1,4 +1,3 @@
import path from 'node:path'
import { runInit } from '@/src/commands/init'
import { preFlightAdd } from '@/src/preflights/preflight-add'
import { addComponents } from '@/src/utils/add-components'
@ -8,6 +7,7 @@ import { highlighter } from '@/src/utils/highlighter'
import { logger } from '@/src/utils/logger'
import { getRegistryIndex } from '@/src/utils/registry'
import { Command } from 'commander'
import path from 'pathe'
import prompts from 'prompts'
import { z } from 'zod'

View File

@ -1,7 +1,6 @@
import type { Config } from '@/src/utils/get-config'
import type { registryIndexSchema } from '@/src/utils/registry/schema'
import { existsSync, promises as fs } from 'node:fs'
import path from 'node:path'
import { getConfig } from '@/src/utils/get-config'
import { handleError } from '@/src/utils/handle-error'
import { highlighter } from '@/src/utils/highlighter'
@ -15,6 +14,7 @@ import {
import { transform } from '@/src/utils/transformers'
import { Command } from 'commander'
import { type Change, diffLines } from 'diff'
import path from 'pathe'
import { z } from 'zod'
const updateOptionsSchema = z.object({

View File

@ -1,5 +1,4 @@
import { promises as fs } from 'node:fs'
import path from 'node:path'
import { addComponents } from '@/src/utils/add-components'
import {
type Config,
@ -19,6 +18,7 @@ import { getRegistryBaseColors, getRegistryStyles } from '@/src/utils/registry'
import { spinner } from '@/src/utils/spinner'
import { updateTailwindContent } from '@/src/utils/updaters/update-tailwind-content'
import { Command } from 'commander'
import path from 'pathe'
import prompts from 'prompts'
import { z } from 'zod'

View File

@ -1,10 +1,10 @@
import path from 'node:path'
import { migrateIcons } from '@/src/migrations/migrate-icons'
import { preFlightMigrate } from '@/src/preflights/preflight-migrate'
import * as ERRORS from '@/src/utils/errors'
import { handleError } from '@/src/utils/handle-error'
import { Command } from 'commander'
import consola from 'consola'
import path from 'pathe'
import { z } from 'zod'
export const migrations = [

View File

@ -4,7 +4,6 @@ import type { z } from 'zod'
import { randomBytes } from 'node:crypto'
import { promises as fs } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { highlighter } from '@/src/utils/highlighter'
import { ICON_LIBRARIES } from '@/src/utils/icon-libraries'
import { logger } from '@/src/utils/logger'
@ -12,6 +11,7 @@ import { getRegistryIcons } from '@/src/utils/registry'
import { spinner } from '@/src/utils/spinner'
import { updateDependencies } from '@/src/utils/updaters/update-dependencies'
import fg from 'fast-glob'
import path from 'pathe'
import prompts from 'prompts'
import { Project, ScriptKind, SyntaxKind } from 'ts-morph'

View File

@ -1,11 +1,11 @@
import type { addOptionsSchema } from '@/src/commands/add'
import type { z } from 'zod'
import path from 'node:path'
import * as ERRORS from '@/src/utils/errors'
import { getConfig } from '@/src/utils/get-config'
import { highlighter } from '@/src/utils/highlighter'
import { logger } from '@/src/utils/logger'
import fs from 'fs-extra'
import path from 'pathe'
export async function preFlightAdd(options: z.infer<typeof addOptionsSchema>) {
const errors: Record<string, boolean> = {}

View File

@ -1,12 +1,12 @@
import type { initOptionsSchema } from '@/src/commands/init'
import type { z } from 'zod'
import path from 'node:path'
import * as ERRORS from '@/src/utils/errors'
import { getProjectInfo, type ProjectInfo } from '@/src/utils/get-project-info'
import { highlighter } from '@/src/utils/highlighter'
import { logger } from '@/src/utils/logger'
import { spinner } from '@/src/utils/spinner'
import fs from 'fs-extra'
import path from 'pathe'
export async function preFlightInit(options: z.infer<typeof initOptionsSchema>) {
const errors: Record<string, boolean> = {}

View File

@ -1,12 +1,12 @@
import type { migrateOptionsSchema } from '@/src/commands/migrate'
import type { z } from 'zod'
import path from 'node:path'
// import { addOptionsSchema } from '@/src/commands/add'
import * as ERRORS from '@/src/utils/errors'
import { getConfig } from '@/src/utils/get-config'
import { highlighter } from '@/src/utils/highlighter'
import { logger } from '@/src/utils/logger'
import fs from 'fs-extra'
import path from 'pathe'
export async function preFlightMigrate(
options: z.infer<typeof migrateOptionsSchema>,

View File

@ -1,13 +1,13 @@
import type { initOptionsSchema } from '@/src/commands/init'
import type { z } from 'zod'
import path from 'node:path'
import { getPackageManager } from '@/src/utils/get-package-manager'
import { highlighter } from '@/src/utils/highlighter'
import { logger } from '@/src/utils/logger'
import { spinner } from '@/src/utils/spinner'
import { execa } from 'execa'
import fs from 'fs-extra'
import { detectPackageManager } from 'nypm'
import path from 'pathe'
import prompts from 'prompts'
import { x } from 'tinyexec'
export async function createProject(options: Pick<z.infer<typeof initOptionsSchema>, 'cwd' | 'force' | 'srcDir'>) {
options = {
@ -35,9 +35,7 @@ export async function createProject(options: Pick<z.infer<typeof initOptionsSche
}
}
const packageManager = await getPackageManager(options.cwd, {
withFallback: true,
})
const packageManager = detectPackageManager(options.cwd)
const { name } = await prompts({
type: 'text',
@ -93,11 +91,13 @@ export async function createProject(options: Pick<z.infer<typeof initOptionsSche
]
try {
await execa(
await x(
'npx',
['create-next-app@14.2.16', projectPath, '--silent', ...args],
{
cwd: options.cwd,
nodeOptions: {
cwd: options.cwd,
},
},
)
}

View File

@ -1,8 +1,9 @@
import { resolveImport } from '@/src/utils/resolve-import'
import { loadConfig as c12LoadConfig } from 'c12'
import { getTsconfig } from 'get-tsconfig'
import path from 'pathe'
import { loadConfig } from 'tsconfig-paths'
import { z } from 'zod'
import { highlighter } from './highlighter'
export const DEFAULT_STYLE = 'default'
export const DEFAULT_COMPONENTS = '@/components'
@ -74,18 +75,22 @@ export async function getConfig(cwd: string) {
return await resolveConfigPaths(cwd, config)
}
export async function resolveConfigPaths(cwd: string, config: RawConfig) {
// Read tsconfig.json.
const tsConfig = await loadConfig(cwd)
if (tsConfig.resultType === 'failed') {
export function getTSConfig(cwd: string, tsconfigName: 'tsconfig.json' | 'jsconfig.json') {
const parsedConfig = getTsconfig(path.resolve(cwd, 'package.json'), tsconfigName)
if (parsedConfig === null) {
throw new Error(
`Failed to load ${config.typescript ? 'tsconfig' : 'jsconfig'}.json. ${
tsConfig.message ?? ''
}`.trim(),
`Failed to find ${highlighter.info(tsconfigName)}`,
)
}
return parsedConfig
}
export async function resolveConfigPaths(cwd: string, config: RawConfig) {
// Read tsconfig.json.
const tsconfigType = config.typescript ? 'tsconfig.json' : 'jsconfig.json'
const tsConfig = getTSConfig(cwd, tsconfigType)
return configSchema.parse({
...config,
resolvedPaths: {
@ -125,7 +130,7 @@ export async function getRawConfig(cwd: string): Promise<RawConfig | null> {
try {
const configResult = await c12LoadConfig({
name: 'components',
configFile: 'components.json',
configFile: 'components',
cwd,
})

View File

@ -1,6 +1,6 @@
import type { PackageJson } from 'type-fest'
import path from 'node:path'
import fs from 'fs-extra'
import path from 'pathe'
export function getPackageInfo(
cwd: string = '',

View File

@ -6,13 +6,13 @@ import type {
import { FRAMEWORKS } from '@/src/utils/frameworks'
import {
getConfig,
getTSConfig,
resolveConfigPaths,
} from '@/src/utils/get-config'
import { getPackageInfo } from '@/src/utils/get-package-info'
import fg from 'fast-glob'
import fs from 'fs-extra'
import path from 'pathe'
import { loadConfig } from 'tsconfig-paths'
import { z } from 'zod'
export interface ProjectInfo {
@ -131,16 +131,13 @@ export async function getTailwindConfigFile(cwd: string) {
}
export async function getTsConfigAliasPrefix(cwd: string) {
const tsConfig = await loadConfig(cwd)
if (
tsConfig?.resultType === 'failed'
|| !Object.entries(tsConfig?.paths).length
) {
return null
}
const isTypescript = await isTypeScriptProject(cwd)
const tsconfigType = isTypescript ? 'tsconfig.json' : 'jsconfig.json'
const tsConfig = await getTSConfig(cwd, tsconfigType)
// This assume that the first alias is the prefix.
for (const [alias, paths] of Object.entries(tsConfig.paths)) {
for (const [alias, paths] of Object.entries(tsConfig.path)) {
if (
paths.includes('./*')
|| paths.includes('./src/*')
@ -154,7 +151,7 @@ export async function getTsConfigAliasPrefix(cwd: string) {
}
// Use the first alias as the prefix.
return Object.keys(tsConfig?.paths)?.[0].replace(/\/\*$/, '') ?? null
return Object.keys(tsConfig?.path)?.[0].replace(/\/\*$/, '') ?? null
}
export async function isTypeScriptProject(cwd: string) {

View File

@ -2,7 +2,6 @@ import type { Config } from '@/src/utils/get-config'
import type {
registryItemFileSchema,
} from '@/src/utils/registry/schema'
import path from 'node:path'
import { handleError } from '@/src/utils/handle-error'
import { logger } from '@/src/utils/logger'
import {
@ -16,6 +15,7 @@ import {
import { buildTailwindThemeColorsFromCssVars } from '@/src/utils/updaters/update-tailwind-config'
import deepmerge from 'deepmerge'
import { ofetch } from 'ofetch'
import path from 'pathe'
import { ProxyAgent } from 'undici'
import { z } from 'zod'

View File

@ -1,13 +1,10 @@
import { type ConfigLoaderSuccessResult, createMatchPath } from 'tsconfig-paths'
import { createPathsMatcher, type TsConfigResult } from 'get-tsconfig'
export function resolveImport(
importPath: string,
config: Pick<ConfigLoaderSuccessResult, 'absoluteBaseUrl' | 'paths'>,
) {
return createMatchPath(config.absoluteBaseUrl, config.paths)(
importPath,
undefined,
() => true,
['.ts', '.tsx', '.vue'],
)
export function resolveImport(importPath: string, config: TsConfigResult) {
const matcher = createPathsMatcher(config)
if (matcher === null) {
return
}
const paths = matcher(importPath)
return paths[0]
}

View File

@ -4,9 +4,9 @@ import type Root from 'postcss/lib/root'
import type Rule from 'postcss/lib/rule'
import type { z } from 'zod'
import { promises as fs } from 'node:fs'
import path from 'node:path'
import { highlighter } from '@/src/utils/highlighter'
import { spinner } from '@/src/utils/spinner'
import path from 'pathe'
import postcss from 'postcss'
import AtRule from 'postcss/lib/at-rule'

View File

@ -1,7 +1,6 @@
import type { Config } from '@/src/utils/get-config'
import type { RegistryItem } from '@/src/utils/registry/schema'
import { existsSync, promises as fs } from 'node:fs'
import path, { basename, dirname } from 'node:path'
import { getProjectInfo } from '@/src/utils/get-project-info'
import { highlighter } from '@/src/utils/highlighter'
import { logger } from '@/src/utils/logger'
@ -11,6 +10,7 @@ import {
} from '@/src/utils/registry'
import { spinner } from '@/src/utils/spinner'
import { transform } from '@/src/utils/transformers'
import path, { basename, dirname } from 'pathe'
// import { transformIcons } from '@/src/utils/transformers/transform-icons'
import prompts from 'prompts'

View File

@ -10,10 +10,10 @@ import type {
import type { z } from 'zod'
import { promises as fs } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { highlighter } from '@/src/utils/highlighter'
import { spinner } from '@/src/utils/spinner'
import deepmerge from 'deepmerge'
import path from 'pathe'
import objectToString from 'stringify-object'
import {
Project,

View File

@ -1,13 +1,13 @@
import type { Config } from '@/src/utils/get-config'
import type { ObjectLiteralExpression } from 'ts-morph'
import { promises as fs } from 'node:fs'
import path from 'node:path'
import { highlighter } from '@/src/utils/highlighter'
import { spinner } from '@/src/utils/spinner'
import {
_createSourceFile,
_getQuoteChar,
} from '@/src/utils/updaters/update-tailwind-config'
import path from 'pathe'
import { SyntaxKind } from 'ts-morph'
export async function updateTailwindContent(

View File

@ -273,6 +273,9 @@ importers:
fs-extra:
specifier: ^11.2.0
version: 11.2.0
get-tsconfig:
specifier: ^4.8.1
version: 4.8.1
lodash-es:
specifier: ^4.17.21
version: 4.17.21
@ -309,12 +312,12 @@ importers:
tailwindcss:
specifier: ^3.4.16
version: 3.4.16
tinyexec:
specifier: ^0.3.1
version: 0.3.1
ts-morph:
specifier: ^24.0.0
version: 24.0.0
tsconfig-paths:
specifier: ^4.2.0
version: 4.2.0
undici:
specifier: ^7.0.0
version: 7.1.0
@ -6833,10 +6836,6 @@ packages:
resolution: {integrity: sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==}
engines: {node: '>=12'}
strip-bom@3.0.0:
resolution: {integrity: sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==}
engines: {node: '>=4'}
strip-eof@1.0.0:
resolution: {integrity: sha512-7FCwGGmx8mD5xQd3RPUvnSpUXHM3BWuzjtpD4TXsfcZ9EL4azvVVUscFYwD9nx8Kh+uCBC00XBtAykoMHwTh8Q==}
engines: {node: '>=0.10.0'}
@ -7110,10 +7109,6 @@ packages:
typescript:
optional: true
tsconfig-paths@4.2.0:
resolution: {integrity: sha512-NoZ4roiN7LnbKn9QqE1amc9DJfzvZXxF4xDavcOWt1BPkdx+m+0gJuPM+S0vCe7zTJMYUP0R8pO2XMr+Y8oLIg==}
engines: {node: '>=6'}
tslib@1.14.1:
resolution: {integrity: sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==}
@ -15319,8 +15314,6 @@ snapshots:
dependencies:
ansi-regex: 6.1.0
strip-bom@3.0.0: {}
strip-eof@1.0.0: {}
strip-final-newline@3.0.0: {}
@ -15622,12 +15615,6 @@ snapshots:
optionalDependencies:
typescript: 5.7.2
tsconfig-paths@4.2.0:
dependencies:
json5: 2.2.3
minimist: 1.2.8
strip-bom: 3.0.0
tslib@1.14.1: {}
tslib@2.8.1: {}