Skip to content

Commit

Permalink
jiti alias
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov committed Feb 5, 2025
1 parent 54a298a commit 1afd758
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/config/get-config.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { loadConfig } from 'c12'
import { consola } from 'consola'
import { getJitiAlias } from '../utils/jiti'
import { getCWD } from './get-cwd.mjs'
import { getMillisPrefix } from './get-file-prefix.mjs'
import type {
Expand All @@ -24,6 +25,7 @@ export async function getConfig(
dotenv: true,
envName: args.environment,
jitiOptions: {
alias: await getJitiAlias(),
debug: Boolean(args.debug),
fsCache: Boolean(args['filesystem-caching']),
},
Expand Down
45 changes: 45 additions & 0 deletions src/utils/jiti.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { join } from 'pathe'
import type { CompilerOptions } from 'typescript'
import { getCWD } from '../config/get-cwd.mjs'
import { getTSConfig } from './tsconfig.mjs'

export interface ArgsLike {
cwd?: string
}

export async function getJitiAlias(): Promise<
Record<string, string> | undefined
> {
try {
const tsconfig = await getTSConfig()

const { baseUrl, paths } = (tsconfig.compilerOptions ||
{}) as CompilerOptions

if (!paths) {
return
}

const cwd = getCWD()

const jitiAlias: Record<string, string> = {}

for (const [alias, [path]] of Object.entries(paths)) {
if (!path) {
continue
}

jitiAlias[removeWildcards(alias)] = removeWildcards(
join(cwd, baseUrl || '.', path),
)
}

return jitiAlias
} catch (error) {
return {}
}
}

function removeWildcards(path: string): string {
return path.replace(/(\/?\*\*?)+$/, '')
}
10 changes: 10 additions & 0 deletions src/utils/tsconfig.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { type TSConfig, readTSConfig } from 'pkg-types'
import { getCWD } from '../config/get-cwd.mjs'

let tsconfig: TSConfig

export async function getTSConfig(): Promise<TSConfig> {
return (tsconfig ||= await readTSConfig(undefined, {
startingFrom: getCWD(),
}))
}

0 comments on commit 1afd758

Please sign in to comment.