Skip to content

Commit

Permalink
feat(loadFileConfigs)!: use standard node js algorithm to resolve con…
Browse files Browse the repository at this point in the history
…figs (#23)

Co-authored-by: Gadzhi Gadzhiev <[email protected]>
  • Loading branch information
ValeraS and resure authored Aug 31, 2023
1 parent c1b4a7e commit 75ab0a7
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/lib/file-configs.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import fs from 'fs';
import path from 'path';

function interopRequire(filePath: string) {
const obj = require(filePath); // eslint-disable-line
// eslint-disable-next-line security/detect-non-literal-require, global-require
const obj = require(filePath);
return obj && obj.__esModule ? obj.default : obj;
}

function getConfigByPath(configPath: string, configName: string) {
const folderFilePath = path.resolve(configPath, configName, 'index.js');
const filePath = path.resolve(configPath, `${configName}.js`);

if (fs.existsSync(folderFilePath)) {
return interopRequire(folderFilePath);
}

if (fs.existsSync(filePath)) {
const filePath = path.resolve(configPath, configName);
try {
return interopRequire(filePath);
} catch (error) {
if (
!(
error &&
typeof error === 'object' &&
'code' in error &&
error.code === 'MODULE_NOT_FOUND'
)
) {
throw error;
}
}

return {};
Expand All @@ -30,9 +35,7 @@ export function loadFileConfigs(
return {};
}

const commonConfigPath = path.resolve(configsRootPath, 'common.js');

const commonConfig = fs.existsSync(commonConfigPath) ? interopRequire(commonConfigPath) : {};
const commonConfig = getConfigByPath(configsRootPath, 'common');

let envConfig = {};

Expand All @@ -42,15 +45,10 @@ export function loadFileConfigs(

const installationConfigs: {common?: object; env?: object} = {common: {}, env: {}};
if (appInstallation) {
const instCommonConfigPath = path.resolve(configsRootPath, appInstallation, 'common.js');
installationConfigs.common = fs.existsSync(instCommonConfigPath)
? interopRequire(instCommonConfigPath)
: {};
const installationConfigPath = path.resolve(configsRootPath, appInstallation);
installationConfigs.common = getConfigByPath(installationConfigPath, 'common');
if (appEnv) {
installationConfigs.env = getConfigByPath(
path.resolve(configsRootPath, appInstallation),
appEnv,
);
installationConfigs.env = getConfigByPath(installationConfigPath, appEnv);
}
}

Expand Down

0 comments on commit 75ab0a7

Please sign in to comment.