From a61078bc8fa0a8768b681e4fc71e56256c1a896d Mon Sep 17 00:00:00 2001 From: icodesign Date: Fri, 30 Aug 2024 13:28:10 +0800 Subject: [PATCH] Support config directory detection --- packages/base/src/config.ts | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/packages/base/src/config.ts b/packages/base/src/config.ts index 0e7ddf9..3bd0a24 100644 --- a/packages/base/src/config.ts +++ b/packages/base/src/config.ts @@ -95,32 +95,39 @@ export type Config = z.infer; export async function parseConfig(userConfigPath?: string): Promise { var configPath = userConfigPath; if (!configPath) { - // search dolphin.y[a]ml under root path - // check if the file exists - const searchFiles = ['dolphin.yml', 'dolphin.yaml']; - const rootPath = process.cwd(); + throw new Error( + `Missing config file. You can either set using --config or put dolphin.y[a]ml under the root path of the project.`, + ); + } + if (!path.isAbsolute(configPath)) { + configPath = path.join(process.cwd(), configPath); + } + // Check if configPath is a directory + const stats = await fs.promises.stat(configPath); + if (stats.isDirectory()) { logger.info( - `No config file provided. Searching config file (dolphin.y[a]ml) under current directory(${rootPath})...`, + `No yaml config file provided. Searching config file (dolphin.y[a]ml) under the directory(${configPath})...`, ); + const searchFiles = ['dolphin.yml', 'dolphin.yaml']; + let found = false; for (const file of searchFiles) { - const attemptConfigPath = path.join(rootPath, file); + const attemptConfigPath = path.join(configPath, file); try { await fs.promises.access(attemptConfigPath); configPath = attemptConfigPath; + found = true; break; } catch (error) { continue; } } + if (!found) { + throw new Error( + `No dolphin.y[a]ml found in the specified directory: ${configPath}`, + ); + } } - if (!configPath) { - throw new Error( - `Missing config file. You can either set using --config or put dolphin.y[a]ml under the root path of the project.`, - ); - } - if (!path.isAbsolute(configPath)) { - configPath = path.join(process.cwd(), configPath); - } + logger.info(`Using config file at ${configPath}`); let fileContent; try {