diff --git a/markdownlint-cli2.js b/markdownlint-cli2.js index 3c0125d6..ef8477a3 100755 --- a/markdownlint-cli2.js +++ b/markdownlint-cli2.js @@ -92,6 +92,31 @@ const requireConfig = (fs, dir, name, noRequire) => ( ) ); +// Read an options or config file in any format and return the object +const readOptionsOrConfig = async (pathSystem, dirSystem, fs, noRequire) => { + const name = path.basename(pathSystem); + let options = null; + let config = null; + if (name.endsWith(".markdownlint-cli2.jsonc")) { + options = jsoncParse(await fs.promises.readFile(pathSystem, utf8)); + } else if (name.endsWith(".markdownlint-cli2.yaml")) { + options = yamlParse(await fs.promises.readFile(pathSystem, utf8)); + } else if (name.endsWith(".markdownlint-cli2.js")) { + options = requireConfig(fs, dirSystem, ".markdownlint-cli2.js", noRequire); + } else if ( + name.endsWith(".markdownlint.jsonc") || + name.endsWith(".markdownlint.json") || + name.endsWith(".markdownlint.yaml") || + name.endsWith(".markdownlint.yml") + ) { + config = + await markdownlintReadConfig(pathSystem, [ jsoncParse, yamlParse ], fs); + } else if (name.endsWith(".markdownlint.js")) { + config = requireConfig(fs, dirSystem, ".markdownlint.js", noRequire); + } + return options || { config }; +}; + // Filter a list of files to ignore by glob const removeIgnoredFiles = (dir, files, ignores) => { const micromatch = require("micromatch"); @@ -694,17 +719,21 @@ const main = async (params) => { // eslint-disable-next-line max-len `${name || packageName} v${packageVersion} (${libraryName} v${libraryVersion})` ); - - // TBD -config - + // Read argv configuration file (if relevant and present) + let optionsArgv = null; + const [ configPath ] = (argv || []); + if ((name === "markdownlint-cli2-config") && configPath) { + optionsArgv = + await readOptionsOrConfig(configPath, baseDirSystem, fs, noRequire); + } // Process arguments and get base options - const globPatterns = processArgv(argv); + const globPatterns = processArgv(optionsArgv ? argv.slice(1) : argv); const { baseMarkdownlintOptions, dirToDirInfo } = await getBaseOptions( fs, baseDir, globPatterns, - optionsDefault, + optionsArgv || optionsDefault, fixDefault, noGlobs, noRequire