From 624993be4cec87448d6bcbd4c48270ad40657bca Mon Sep 17 00:00:00 2001 From: Mayur Kawale <122032765+Mefisto04@users.noreply.github.com> Date: Fri, 25 Oct 2024 00:46:17 +0530 Subject: [PATCH] minor changes --- src/config/configLoad.ts | 118 +++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 62 deletions(-) diff --git a/src/config/configLoad.ts b/src/config/configLoad.ts index d986aec99..d545d088d 100644 --- a/src/config/configLoad.ts +++ b/src/config/configLoad.ts @@ -14,47 +14,35 @@ const getGlobalConfigPath = () => { }; export const loadFileConfig = async (rootDir: string, argConfigPath: string | null): Promise => { - let useDefaultConfig = false; - let configPath = argConfigPath; - if (!configPath) { - useDefaultConfig = true; - configPath = defaultConfigPath; - } + let configPath = argConfigPath ?? defaultConfigPath; const fullPath = path.resolve(rootDir, configPath); - logger.trace('Loading local config from:', fullPath); - // Check local file existence - const isLocalFileExists = await fs - .stat(fullPath) - .then((stats) => stats.isFile()) - .catch(() => false); + const [isLocalFileExists, isGlobalFileExists] = await Promise.all([ + checkFileExists(fullPath), + checkFileExists(getGlobalConfigPath()), + ]); if (isLocalFileExists) { return await loadAndValidateConfig(fullPath); } - if (useDefaultConfig) { - // Try to load global config - const globalConfigPath = getGlobalConfigPath(); - logger.trace('Loading global config from:', globalConfigPath); - - const isGlobalFileExists = await fs - .stat(globalConfigPath) - .then((stats) => stats.isFile()) - .catch(() => false); + if (isGlobalFileExists) { + return await loadAndValidateConfig(getGlobalConfigPath()); + } - if (isGlobalFileExists) { - return await loadAndValidateConfig(globalConfigPath); - } + logger.note( + `No custom config found at ${configPath} or global config. You can add a config file for additional settings. Please check https://github.com/yamadashy/repopack for more information.`, + ); + return {}; +}; - logger.note( - `No custom config found at ${configPath} or global config at ${globalConfigPath}.\nYou can add a config file for additional settings. Please check https://github.com/yamadashy/repopack for more information.`, - ); - return {}; - } - throw new RepopackError(`Config file not found at ${configPath}`); +const checkFileExists = async (filePath: string): Promise => { + return fs + .stat(filePath) + .then((stats) => stats.isFile()) + .catch(() => false); }; const loadAndValidateConfig = async (filePath: string): Promise => { @@ -64,48 +52,29 @@ const loadAndValidateConfig = async (filePath: string): Promise { + const errorMessage = error instanceof Error ? error.message : 'Unknown error'; + throw new RepopackError(`Error loading config from ${filePath}: ${errorMessage}`); +}; + +const mergeConfigs = ( cwd: string, fileConfig: RepopackConfigFile, cliConfig: RepopackConfigCli, ): RepopackConfigMerged => { - // If the output file path is not provided in the config file or CLI, use the default file path for the style - if (cliConfig.output?.filePath == null && fileConfig.output?.filePath == null) { - const style = cliConfig.output?.style || fileConfig.output?.style || defaultConfig.output.style; - defaultConfig.output.filePath = defaultFilePathMap[style]; - } + const output = mergeOutput(fileConfig.output, cliConfig.output); + const ignore = mergeIgnore(fileConfig.ignore, cliConfig.ignore); + const include = Array.prototype.flat([defaultConfig.include || [], fileConfig.include || [], cliConfig.include || []]); return { cwd, - output: { - ...defaultConfig.output, - ...fileConfig.output, - ...cliConfig.output, - }, - ignore: { - ...defaultConfig.ignore, - ...fileConfig.ignore, - ...cliConfig.ignore, - customPatterns: [ - ...(defaultConfig.ignore.customPatterns || []), - ...(fileConfig.ignore?.customPatterns || []), - ...(cliConfig.ignore?.customPatterns || []), - ], - }, - include: [...(defaultConfig.include || []), ...(fileConfig.include || []), ...(cliConfig.include || [])], + output, + ignore, + include, security: { ...defaultConfig.security, ...fileConfig.security, @@ -113,3 +82,28 @@ export const mergeConfigs = ( }, }; }; + +const mergeOutput = (fileOutput: any, cliOutput: any) => { + const style = cliOutput?.style || fileOutput?.style || defaultConfig.output.style; + const filePath = cliOutput?.filePath ?? fileOutput?.filePath ?? defaultFilePathMap[style]; + + return { + ...defaultConfig.output, + ...fileOutput, + ...cliOutput, + filePath, + }; +}; + +const mergeIgnore = (fileIgnore: any, cliIgnore: any) => { + return { + ...defaultConfig.ignore, + ...fileIgnore, + ...cliIgnore, + customPatterns: [ + ...(defaultConfig.ignore.customPatterns || []), + ...(fileIgnore?.customPatterns || []), + ...(cliIgnore?.customPatterns || []), + ], + }; +};