Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(loadFileConfigs)!: use standard node js algorithm to resolve configs #23

Merged
merged 3 commits into from
Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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