Skip to content

Commit

Permalink
feat(config): add option to force require config files
Browse files Browse the repository at this point in the history
- Added `forceRequire` parameter to `parseConfig` and `importConfig` functions.
- Enabled forcing the use of `require()` for configuration files, regardless of their extension.
- Improved compatibility with Windows by replacing backslashes with forward slashes when using `import()`.
  • Loading branch information
mitsuki31 committed Aug 27, 2024
1 parent 8b08c73 commit a63d067
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function configChecker({ config, file }) {
* @see {@link module:config~importConfig importConfig}
* (alias for <code>parseConfig(config, true)</code>)
*/
function parseConfig(configFile, resolve=true) {
function parseConfig(configFile, resolve=true, forceRequire=false) {
function resolveOrCheckOnly(config) {
// Attempt to extract the default export (only on ES module) if the configuration
// object only contains that 'default' property, for clarity:
Expand Down Expand Up @@ -294,15 +294,17 @@ function parseConfig(configFile, resolve=true) {
}`);
}

// Resolve the configuration file path
configFile = path.resolve(configFile);

// Import the configuration file
let config = null;
// Only include '.cjs' and '.json' to use require()
if (KNOWN_CONFIG_EXTS.slice(2).includes(path.extname(configFile))) {
if (KNOWN_CONFIG_EXTS.slice(2).includes(path.extname(configFile))
|| forceRequire) {
config = require(configFile);
} else {
// On Windows, replace all '\' with '/' to use import()
if (process.platform === 'win32') {
configFile = 'file:///' + configFile.replace(/\\/g, '/');
}
config = import(configFile);
}

Expand All @@ -328,7 +330,7 @@ function parseConfig(configFile, resolve=true) {
* @since 1.0.0
* @see {@link module:config~parseConfig parseConfig}
*/
const importConfig = (file) => parseConfig(file, true);
const importConfig = (file, forceRequire=false) => parseConfig(file, true, forceRequire);


module.exports = Object.freeze({
Expand Down

0 comments on commit a63d067

Please sign in to comment.