Update readConfig to use fs.access so the async path is fully async.

This commit is contained in:
David Anson 2021-08-12 20:43:18 -07:00
parent 211f09afbc
commit 709e314836
5 changed files with 108 additions and 28 deletions

View file

@ -926,6 +926,36 @@ function parseConfiguration(name, content, parsers) {
};
}
/**
* Resolve referenced "extends" path in a configuration file
* using path.resolve() with require.resolve() as a fallback.
*
* @param {string} configFile Configuration file name.
* @param {string} referenceId Referenced identifier to resolve.
* @param {Object} fs File system implementation.
* @param {ResolveConfigExtendsCallback} [callback] Callback (err, result)
* function.
* @returns {void}
*/
function resolveConfigExtends(configFile, referenceId, fs, callback) {
const configFileDirname = path.dirname(configFile);
const resolvedExtendsFile = path.resolve(configFileDirname, referenceId);
fs.access(resolvedExtendsFile, (err) => {
if (err) {
// Not a file, try require.resolve
try {
return callback(null, dynamicRequire.resolve(
referenceId,
{ "paths": [ configFileDirname ] }
));
} catch {
// Unable to resolve, use resolvedExtendsFile
}
}
return callback(null, resolvedExtendsFile);
});
}
/**
* Resolve referenced "extends" path in a configuration file
* using path.resolve() with require.resolve() as a fallback.
@ -935,7 +965,7 @@ function parseConfiguration(name, content, parsers) {
* @param {Object} fs File system implementation.
* @returns {string} Resolved path to file.
*/
function resolveConfigExtends(configFile, referenceId, fs) {
function resolveConfigExtendsSync(configFile, referenceId, fs) {
const configFileDirname = path.dirname(configFile);
const resolvedExtendsFile = path.resolve(configFileDirname, referenceId);
try {
@ -994,16 +1024,25 @@ function readConfig(file, parsers, fs, callback) {
const configExtends = config.extends;
if (configExtends) {
delete config.extends;
const resolvedExtends = resolveConfigExtends(file, configExtends, fs);
return readConfig(resolvedExtends, parsers, fs, (errr, extendsConfig) => {
if (errr) {
return callback(errr);
}
return callback(null, {
...extendsConfig,
...config
});
});
return resolveConfigExtends(
file,
configExtends,
fs,
(_, resolvedExtends) => readConfig(
resolvedExtends,
parsers,
fs,
(errr, extendsConfig) => {
if (errr) {
return callback(errr);
}
return callback(null, {
...extendsConfig,
...config
});
}
)
);
}
return callback(null, config);
});
@ -1047,7 +1086,7 @@ function readConfigSync(file, parsers, fs) {
const configExtends = config.extends;
if (configExtends) {
delete config.extends;
const resolvedExtends = resolveConfigExtends(file, configExtends, fs);
const resolvedExtends = resolveConfigExtendsSync(file, configExtends, fs);
return {
...readConfigSync(resolvedExtends, parsers, fs),
...config
@ -1221,7 +1260,7 @@ module.exports = markdownlint;
*/
/**
* Called with the result of the lint operation.
* Called with the result of the lint function.
*
* @callback LintCallback
* @param {Error | null} err Error object or null.
@ -1251,10 +1290,19 @@ module.exports = markdownlint;
*/
/**
* Called with the result of the readConfig operation.
* Called with the result of the readConfig function.
*
* @callback ReadConfigCallback
* @param {Error | null} err Error object or null.
* @param {Configuration} [config] Configuration object.
* @returns {void}
*/
/**
* Called with the result of the resolveConfigExtends function.
*
* @callback ResolveConfigExtendsCallback
* @param {Error | null} err Error object or null.
* @param {string} [path] Resolved path to file.
* @returns {void}
*/