Skip to content

Commit

Permalink
refactor: use get-tsconfig instead of tsconfig-paths
Browse files Browse the repository at this point in the history
refactor: replace `node:path` with `pathe`
chore: add `tinyexec` instead of `execa`
  • Loading branch information
sadeghbarati committed Dec 4, 2024
1 parent 3f0ded8 commit 8a8d1ef
Show file tree
Hide file tree
Showing 20 changed files with 60 additions and 73 deletions.
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/diff.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/migrate.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/migrations/migrate-icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ 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'
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'

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/preflights/preflight-add.ts
Original file line number Diff line number Diff line change
@@ -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> = {}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/preflights/preflight-init.ts
Original file line number Diff line number Diff line change
@@ -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> = {}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/preflights/preflight-migrate.ts
Original file line number Diff line number Diff line change
@@ -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>,
Expand Down
16 changes: 8 additions & 8 deletions packages/cli/src/utils/create-project.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -93,11 +91,13 @@ export async function createProject(options: Pick<z.infer<typeof initOptionsSche
]

try {
await execa(
await x(
'npx',
['[email protected]', projectPath, '--silent', ...args],
{
cwd: options.cwd,
nodeOptions: {
cwd: options.cwd,
},
},
)
}
Expand Down
25 changes: 15 additions & 10 deletions packages/cli/src/utils/get-config.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
})

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils/get-package-info.ts
Original file line number Diff line number Diff line change
@@ -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 = '',
Expand Down
17 changes: 7 additions & 10 deletions packages/cli/src/utils/get-project-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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/*')
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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'

Expand Down
19 changes: 8 additions & 11 deletions packages/cli/src/utils/resolve-import.ts
Original file line number Diff line number Diff line change
@@ -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]
}
2 changes: 1 addition & 1 deletion packages/cli/src/utils/updaters/update-css-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils/updaters/update-files.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils/updaters/update-tailwind-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils/updaters/update-tailwind-content.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
Loading

0 comments on commit 8a8d1ef

Please sign in to comment.