mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Implement (undocumented, promise-only) extendConfig API for readConfig-like behavior when the Configuration object is already available (i.e., to implement "extends" consistently).
This commit is contained in:
parent
953165a219
commit
2ab546bec0
4 changed files with 188 additions and 50 deletions
|
|
@ -1079,6 +1079,63 @@ function resolveConfigExtendsSync(configFile, referenceId, fs) {
|
|||
return resolvedExtendsFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend specified configuration object.
|
||||
*
|
||||
* @param {Configuration} config Configuration object.
|
||||
* @param {string} file Configuration file name.
|
||||
* @param {ConfigurationParser[]} parsers Parsing
|
||||
* function(s).
|
||||
* @param {Object} fs File system implementation.
|
||||
* @param {ReadConfigCallback} callback Callback (err, result) function.
|
||||
* @returns {void}
|
||||
*/
|
||||
function extendConfig(config, file, parsers, fs, callback) {
|
||||
const configExtends = config.extends;
|
||||
if (configExtends) {
|
||||
return resolveConfigExtends(
|
||||
file,
|
||||
helpers.expandTildePath(configExtends, require("node:os")),
|
||||
fs,
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
(_, resolvedExtends) => readConfig(
|
||||
// @ts-ignore
|
||||
resolvedExtends,
|
||||
parsers,
|
||||
fs,
|
||||
(err, extendsConfig) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
const result = {
|
||||
...extendsConfig,
|
||||
...config
|
||||
};
|
||||
delete result.extends;
|
||||
return callback(null, result);
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
return callback(null, config);
|
||||
}
|
||||
|
||||
const extendConfigPromisify = promisify && promisify(extendConfig);
|
||||
|
||||
/**
|
||||
* Extend specified configuration object.
|
||||
*
|
||||
* @param {Configuration} config Configuration object.
|
||||
* @param {string} file Configuration file name.
|
||||
* @param {ConfigurationParser[]} [parsers] Parsing function(s).
|
||||
* @param {Object} [fs] File system implementation.
|
||||
* @returns {Promise<Configuration>} Configuration object.
|
||||
*/
|
||||
function extendConfigPromise(config, file, parsers, fs) {
|
||||
// @ts-ignore
|
||||
return extendConfigPromisify(config, file, parsers, fs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read specified configuration file.
|
||||
*
|
||||
|
|
@ -1105,8 +1162,7 @@ function readConfig(file, parsers, fs, callback) {
|
|||
fs = require("node:fs");
|
||||
}
|
||||
// Read file
|
||||
const os = require("node:os");
|
||||
file = helpers.expandTildePath(file, os);
|
||||
file = helpers.expandTildePath(file, require("node:os"));
|
||||
fs.readFile(file, "utf8", (err, content) => {
|
||||
if (err) {
|
||||
// @ts-ignore
|
||||
|
|
@ -1120,34 +1176,8 @@ function readConfig(file, parsers, fs, callback) {
|
|||
return callback(new Error(message));
|
||||
}
|
||||
// Extend configuration
|
||||
const configExtends = config.extends;
|
||||
if (configExtends) {
|
||||
delete config.extends;
|
||||
return resolveConfigExtends(
|
||||
file,
|
||||
helpers.expandTildePath(configExtends, os),
|
||||
fs,
|
||||
(_, resolvedExtends) => readConfig(
|
||||
// @ts-ignore
|
||||
resolvedExtends,
|
||||
parsers,
|
||||
fs,
|
||||
(errr, extendsConfig) => {
|
||||
if (errr) {
|
||||
// @ts-ignore
|
||||
return callback(errr);
|
||||
}
|
||||
// @ts-ignore
|
||||
return callback(null, {
|
||||
...extendsConfig,
|
||||
...config
|
||||
});
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
// @ts-ignore
|
||||
return callback(null, config);
|
||||
return extendConfig(config, file, parsers, fs, callback);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -1221,6 +1251,7 @@ markdownlint.readConfigSync = readConfigSync;
|
|||
markdownlint.getVersion = getVersion;
|
||||
markdownlint.promises = {
|
||||
"markdownlint": markdownlintPromise,
|
||||
"extendConfig": extendConfigPromise,
|
||||
"readConfig": readConfigPromise
|
||||
};
|
||||
module.exports = markdownlint;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue