Files
medusa-store/packages/medusa-core-utils/src/get-config-file.ts
Carlos R. L. Rodrigues 77d46220c2 Feat(modules-sdk,inventory,stock-location): modules isolated connection (#3329)
* feat: scoped container for modules
2023-03-15 12:09:45 -03:00

33 lines
954 B
TypeScript

import { join } from "path"
/**
* Attempts to resolve the config file in a given root directory.
* @param {string} rootDir - the directory to find the config file in.
* @param {string} configName - the name of the config file.
* @return {object} an object containing the config module and its path as well as an error property if the config couldn't be loaded.
*/
function getConfigFile<TConfig = unknown>(
rootDir: string,
configName: string
): { configModule: TConfig; configFilePath: string; error?: any } {
const configPath = join(rootDir, configName)
let configFilePath = ``
let configModule
let err
try {
configFilePath = require.resolve(configPath)
configModule = require(configFilePath)
} catch (e) {
err = e
}
if (configModule && typeof configModule.default === "object") {
configModule = configModule.default
}
return { configModule, configFilePath, error: err }
}
export default getConfigFile