diff --git a/markdownlint-cli2.mjs b/markdownlint-cli2.mjs index c406f7e..9b20f3d 100755 --- a/markdownlint-cli2.mjs +++ b/markdownlint-cli2.mjs @@ -1,7 +1,7 @@ // @ts-ignore // Requires -import fsx from "node:fs"; +import fsNode from "node:fs"; import { createRequire } from "node:module"; const dynamicRequire = createRequire(import.meta.url); import os from "node:os"; @@ -9,13 +9,15 @@ import pathDefault from "node:path"; const pathPosix = pathDefault.posix; import { pathToFileURL } from "node:url"; import { globby } from "globby"; -import micromatch from "micromatch"; import { applyFixes, getVersion } from "markdownlint"; import { lint, extendConfig, readConfig } from "markdownlint/promise"; import { expandTildePath } from "markdownlint/helpers"; import appendToArray from "./append-to-array.mjs"; import mergeOptions from "./merge-options.mjs"; import resolveAndRequire from "./resolve-and-require.mjs"; +import parsers from "./parsers/parsers.mjs"; +import jsoncParse from "./parsers/jsonc-parse.mjs"; +import yamlParse from "./parsers/yaml-parse.mjs"; // Variables const packageName = "markdownlint-cli2"; @@ -29,18 +31,6 @@ const utf8 = "utf8"; // No-op function const noop = () => null; -// Gets a JSONC parser -import jsoncParse from "./parsers/jsonc-parse.mjs"; -const getJsoncParse = () => jsoncParse; - -// Gets a YAML parser -import yamlParse from "./parsers/yaml-parse.mjs"; -const getYamlParse = () => yamlParse; - -// Gets an ordered array of parsers -import parsers from "./parsers/parsers.mjs"; -const getParsers = () => parsers; - // Negates a glob const negateGlob = (glob) => `!${glob}`; @@ -67,7 +57,7 @@ const readConfigFile = (fs, dir, name, otherwise) => () => { then( () => readConfig( file, - getParsers(), + parsers, fs ), otherwise @@ -151,7 +141,7 @@ const getExtendedConfig = (config, configPath, fs) => { return extendConfig( config, configPath, - getParsers(), + parsers, fs ); } @@ -167,9 +157,9 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => { let config = null; try { if (basename.endsWith(".markdownlint-cli2.jsonc")) { - options = getJsoncParse()(await fs.promises.readFile(configPath, utf8)); + options = jsoncParse(await fs.promises.readFile(configPath, utf8)); } else if (basename.endsWith(".markdownlint-cli2.yaml")) { - options = getYamlParse()(await fs.promises.readFile(configPath, utf8)); + options = yamlParse(await fs.promises.readFile(configPath, utf8)); } else if ( basename.endsWith(".markdownlint-cli2.cjs") || basename.endsWith(".markdownlint-cli2.mjs") @@ -181,7 +171,7 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => { basename.endsWith(".markdownlint.yaml") || basename.endsWith(".markdownlint.yml") ) { - config = await readConfig(configPath, getParsers(), fs); + config = await readConfig(configPath, parsers, fs); } else if ( basename.endsWith(".markdownlint.cjs") || basename.endsWith(".markdownlint.mjs") @@ -207,12 +197,13 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => { }; // Filter a list of files to ignore by glob -const removeIgnoredFiles = (dir, files, ignores) => ( - micromatch( +const removeIgnoredFiles = (dir, files, ignores) => { + const micromatch = dynamicRequire("micromatch"); + return micromatch( files.map((file) => pathPosix.relative(dir, file)), ignores - ).map((file) => pathPosix.join(dir, file)) -); + ).map((file) => pathPosix.join(dir, file)); +}; // Process/normalize command-line arguments and return glob patterns const processArgv = (argv) => { @@ -325,10 +316,10 @@ const getAndProcessDirInfo = ( tasks.push( fs.promises.access(captureFile(markdownlintCli2Jsonc)). then( - () => fs.promises.readFile(file, utf8).then(getJsoncParse()), + () => fs.promises.readFile(file, utf8).then(jsoncParse), () => fs.promises.access(captureFile(markdownlintCli2Yaml)). then( - () => fs.promises.readFile(file, utf8).then(getYamlParse()), + () => fs.promises.readFile(file, utf8).then(yamlParse), () => fs.promises.access(captureFile(markdownlintCli2Cjs)). then( () => importOrRequireResolve(dir, file, noRequire), @@ -343,7 +334,7 @@ const getAndProcessDirInfo = ( then( () => fs.promises. readFile(file, utf8). - then(getJsoncParse()). + then(jsoncParse). then((obj) => obj[packageName]), noop ) @@ -780,7 +771,7 @@ const lintFiles = (fs, dirInfos, fileContents) => { "files": filteredFiles, "strings": filteredStrings, "config": markdownlintConfig || markdownlintOptions.config, - "configParsers": getParsers(), + "configParsers": parsers, "customRules": markdownlintOptions.customRules, "frontMatter": markdownlintOptions.frontMatter ? new RegExp(markdownlintOptions.frontMatter, "u") @@ -909,7 +900,7 @@ export const main = async (params) => { } = params; const logMessage = params.logMessage || noop; const logError = params.logError || noop; - const fs = params.fs || fsx; + const fs = params.fs || fsNode; const baseDirSystem = (directory && pathDefault.resolve(directory)) || process.cwd();