Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Dec 12, 2024
1 parent 9e31d82 commit d04c1ba
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions markdownlint-cli2.mjs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
// @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";
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";
Expand All @@ -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}`;

Expand All @@ -67,7 +57,7 @@ const readConfigFile = (fs, dir, name, otherwise) => () => {
then(
() => readConfig(
file,
getParsers(),
parsers,
fs
),
otherwise
Expand Down Expand Up @@ -151,7 +141,7 @@ const getExtendedConfig = (config, configPath, fs) => {
return extendConfig(
config,
configPath,
getParsers(),
parsers,
fs
);
}
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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) => {
Expand Down Expand Up @@ -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),
Expand All @@ -343,7 +334,7 @@ const getAndProcessDirInfo = (
then(
() => fs.promises.
readFile(file, utf8).
then(getJsoncParse()).
then(jsoncParse).
then((obj) => obj[packageName]),
noop
)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d04c1ba

Please sign in to comment.