11import fs from 'node:fs' ;
22import path from 'node:path' ;
33import process from 'node:process' ;
4- import url from 'node:url' ;
54
65import {
76 asyncReduce ,
@@ -17,6 +16,7 @@ import {
1716 type NormalizedOptions ,
1817 removeFilesAndEmptyFolders ,
1918} from '@orval/core' ;
19+ import { createJiti } from 'jiti' ;
2020
2121import { importSpecs } from './import-specs' ;
2222import { normalizeOptions } from './utils/options' ;
@@ -95,10 +95,14 @@ export const generateSpecs = async (
9595
9696function findConfigFile ( configFilePath ?: string ) {
9797 if ( configFilePath ) {
98- if ( ! fs . existsSync ( configFilePath ) )
98+ const absolutePath = path . isAbsolute ( configFilePath )
99+ ? configFilePath
100+ : path . resolve ( process . cwd ( ) , configFilePath ) ;
101+
102+ if ( ! fs . existsSync ( absolutePath ) )
99103 throw new Error ( `Config file ${ configFilePath } does not exist` ) ;
100104
101- return configFilePath ;
105+ return absolutePath ;
102106 }
103107
104108 const root = process . cwd ( ) ;
@@ -113,21 +117,28 @@ function findConfigFile(configFilePath?: string) {
113117 throw new Error ( `No config file found in ${ root } ` ) ;
114118}
115119
120+ async function loadConfigFile ( configFilePath : string ) : Promise < ConfigExternal > {
121+ const jiti = createJiti ( import . meta. url , {
122+ interopDefault : true ,
123+ } ) ;
124+
125+ const module = await jiti . import ( configFilePath , { default : true } ) ;
126+
127+ if ( module === undefined ) {
128+ throw new Error ( `${ configFilePath } doesn't have a default export` ) ;
129+ }
130+
131+ return await Promise . resolve ( module as ConfigExternal ) ;
132+ }
133+
116134export const generateConfig = async (
117135 configFile ?: string ,
118136 options ?: GlobalOptions ,
119137) => {
120138 const configFilePath = findConfigFile ( configFile ) ;
121139 let configExternal : ConfigExternal ;
122140 try {
123- const importPath = url . pathToFileURL ( configFilePath ) . href ;
124- const importedModule = ( await import ( importPath ) ) as {
125- default ?: ConfigExternal ;
126- } ;
127- if ( importedModule . default === undefined ) {
128- throw new Error ( `${ configFilePath } doesn't have a default export` ) ;
129- }
130- configExternal = importedModule . default ;
141+ configExternal = await loadConfigFile ( configFilePath ) ;
131142 } catch ( error ) {
132143 const errorMsg = error instanceof Error ? error . message : 'unknown error' ;
133144 throw new Error ( `failed to load from ${ configFilePath } => ${ errorMsg } ` ) ;
0 commit comments