diff --git a/packages/cli/src/helpers/config.ts b/packages/cli/src/helpers/config.ts index 93e0cb3..9c79a3e 100644 --- a/packages/cli/src/helpers/config.ts +++ b/packages/cli/src/helpers/config.ts @@ -2,15 +2,32 @@ import {CONFIG_DEFAULT_PATH, DEFAULT_CONFIG, ResolvedConfig} from '../const.js' import {configZod} from '../const.js' import {join} from 'node:path' import {existsSync} from 'node:fs' +import {changeExtension} from './path.js' export async function loadConfig(config?: string): Promise { - const configPath = join(process.cwd(), config ?? CONFIG_DEFAULT_PATH) + const userConfigPath = config ? join(process.cwd(), config) : null + const defaultConfigPath = join(process.cwd(), CONFIG_DEFAULT_PATH) + const cjsConfigPath = join(process.cwd(), await changeExtension(CONFIG_DEFAULT_PATH, '.cjs')) + const mjsConfigPath = join(process.cwd(), await changeExtension(CONFIG_DEFAULT_PATH, '.mjs')) - if (existsSync(configPath)) { - return (await import(configPath)).default - } else { - return DEFAULT_CONFIG + if (userConfigPath) { + if (existsSync(userConfigPath)) { + return (await import(userConfigPath)).default + } else { + throw new Error(`Error: config ${userConfigPath} not found.`) + } } + + if (existsSync(defaultConfigPath)) { + return (await import(defaultConfigPath)).default + } + if (existsSync(cjsConfigPath)) { + return (await import(cjsConfigPath)).default + } + if (existsSync(mjsConfigPath)) { + return (await import(mjsConfigPath)).default + } + return DEFAULT_CONFIG } export async function validateConfig(log: (message: string) => void, config?: unknown): Promise { diff --git a/packages/cli/src/helpers/path.ts b/packages/cli/src/helpers/path.ts index af04070..cb87069 100644 --- a/packages/cli/src/helpers/path.ts +++ b/packages/cli/src/helpers/path.ts @@ -1,7 +1,7 @@ import {ResolvedConfig} from '../const.js' import {readdir} from 'node:fs/promises' import {existsSync} from 'node:fs' -import {join} from 'node:path' +import {basename, dirname, extname, join} from 'node:path' export async function getComponentsInstalled(components: string[], config: ResolvedConfig) { const componentPath = join(process.cwd(), config.paths.components) @@ -12,3 +12,7 @@ export async function getComponentsInstalled(components: string[], config: Resol return [] } } + +export async function changeExtension(path: string, extension: string): Promise { + return join(dirname(path), basename(path, extname(path)) + extension) +}