Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Dec 18, 2024
1 parent 0dc3fec commit 6e460ca
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 74 deletions.
85 changes: 44 additions & 41 deletions markdownlint-cli2.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-ignore

// Requires
// Imports
import fsNode from "node:fs";
import { createRequire } from "node:module";
const dynamicRequire = createRequire(import.meta.url);
Expand Down Expand Up @@ -66,19 +66,21 @@ const readConfigFile = (fs, dir, name, otherwise) => () => {
};

// Import a module ID with a custom directory in the path
const importModule = async (dirOrDirs, id, noRequire) => {
const importModule = async (dirOrDirs, id, noImport) => {
if (typeof id !== "string") {
return id;
} else if (noRequire) {
} else if (noImport) {
return null;
}
const expandId = expandTildePath(id, os);
const dirs = Array.isArray(dirOrDirs) ? dirOrDirs : [ dirOrDirs ];
const expandId = expandTildePath(id, os);
const errors = [];
let moduleName = null;
try {
let moduleName = null;
try {
moduleName = pathToFileURL(resolveModule(dynamicRequire, expandId, dirs));
} catch {
} catch (error) {
errors.push(error);
moduleName =
// eslint-disable-next-line n/no-unsupported-features/node-builtins
(!pathDefault.isAbsolute(expandId) && URL.canParse(expandId))
Expand All @@ -89,38 +91,39 @@ const importModule = async (dirOrDirs, id, noRequire) => {
const module = await import(/* webpackIgnore: true */ moduleName);
return module.default;
} catch (error) {
throw new Error(
`Unable to require or import module '${id}'.`,
{ "cause": error }
errors.push(error);
throw new AggregateError(
errors,
`Unable to import module '${id}'.`
);
}
};

// Import an array of modules by ID
const importModuleIds = (dirs, ids, noRequire) => (
const importModuleIds = (dirs, ids, noImport) => (
Promise.all(
ids.map(
(id) => importModule(dirs, id, noRequire)
(id) => importModule(dirs, id, noImport)
)
).then((results) => results.filter(Boolean))
);

// Import an array of modules by ID (preserving parameters)
const importModuleIdsAndParams = (dirs, idsAndParams, noRequire) => (
const importModuleIdsAndParams = (dirs, idsAndParams, noImport) => (
Promise.all(
idsAndParams.map(
(idAndParams) => importModule(dirs, idAndParams[0], noRequire).
(idAndParams) => importModule(dirs, idAndParams[0], noImport).
then((module) => module && [ module, ...idAndParams.slice(1) ])
)
).then((results) => results.filter(Boolean))
);

// Import a JavaScript file and return the exported object
const importConfig = (fs, dir, name, noRequire, otherwise) => () => {
const importConfig = (fs, dir, name, noImport, otherwise) => () => {
const file = pathPosix.join(dir, name);
return fs.promises.access(file).
then(
() => importModule(dir, name, noRequire),
() => importModule(dir, name, noImport),
otherwise
);
};
Expand All @@ -140,7 +143,7 @@ const getExtendedConfig = (config, configPath, fs) => {
};

// Read an options or config file in any format and return the object
const readOptionsOrConfig = async (configPath, fs, noRequire) => {
const readOptionsOrConfig = async (configPath, fs, noImport) => {
const basename = pathPosix.basename(configPath);
const dirname = pathPosix.dirname(configPath);
let options = null;
Expand All @@ -154,7 +157,7 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => {
basename.endsWith(".markdownlint-cli2.cjs") ||
basename.endsWith(".markdownlint-cli2.mjs")
) {
options = await importModule(dirname, basename, noRequire);
options = await importModule(dirname, basename, noImport);
} else if (
basename.endsWith(".markdownlint.jsonc") ||
basename.endsWith(".markdownlint.json") ||
Expand All @@ -166,7 +169,7 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => {
basename.endsWith(".markdownlint.cjs") ||
basename.endsWith(".markdownlint.mjs")
) {
config = await importModule(dirname, basename, noRequire);
config = await importModule(dirname, basename, noImport);
} else {
throw new Error(
"File name should be (or end with) one of the supported types " +
Expand Down Expand Up @@ -277,7 +280,7 @@ const getAndProcessDirInfo = (
dirToDirInfo,
dir,
relativeDir,
noRequire,
noImport,
allowPackageJson
) => {
// Create dirInfo
Expand Down Expand Up @@ -311,10 +314,10 @@ const getAndProcessDirInfo = (
() => fs.promises.readFile(file, utf8).then(yamlParse),
() => fs.promises.access(captureFile(markdownlintCli2Cjs)).
then(
() => importModule(dir, file, noRequire),
() => importModule(dir, file, noImport),
() => fs.promises.access(captureFile(markdownlintCli2Mjs)).
then(
() => importModule(dir, file, noRequire),
() => importModule(dir, file, noImport),
() => (allowPackageJson
? fs.promises.access(captureFile(packageJson))
// eslint-disable-next-line prefer-promise-reject-errors
Expand Down Expand Up @@ -372,12 +375,12 @@ const getAndProcessDirInfo = (
fs,
dir,
".markdownlint.cjs",
noRequire,
noImport,
importConfig(
fs,
dir,
".markdownlint.mjs",
noRequire,
noImport,
noop
)
)
Expand Down Expand Up @@ -406,7 +409,7 @@ const getBaseOptions = async (
options,
fixDefault,
noGlobs,
noRequire
noImport
) => {
const tasks = [];
const dirToDirInfo = {};
Expand All @@ -416,7 +419,7 @@ const getBaseOptions = async (
dirToDirInfo,
baseDir,
relativeDir,
noRequire,
noImport,
true
);
await Promise.all(tasks);
Expand Down Expand Up @@ -457,7 +460,7 @@ const enumerateFiles = async (
dirToDirInfo,
gitignore,
ignoreFiles,
noRequire
noImport
) => {
const tasks = [];
/** @type {import("globby").Options} */
Expand Down Expand Up @@ -525,7 +528,7 @@ const enumerateFiles = async (
dirToDirInfo,
dir,
null,
noRequire,
noImport,
false
);
dirInfo.files.push(file);
Expand All @@ -538,7 +541,7 @@ const enumerateParents = async (
fs,
baseDir,
dirToDirInfo,
noRequire
noImport
) => {
const tasks = [];

Expand Down Expand Up @@ -567,7 +570,7 @@ const enumerateParents = async (
dirToDirInfo,
dir,
null,
noRequire,
noImport,
false
);
lastDirInfo.parent = dirInfo;
Expand All @@ -592,7 +595,7 @@ const createDirInfos = async (
optionsOverride,
gitignore,
ignoreFiles,
noRequire
noImport
) => {
await enumerateFiles(
fs,
Expand All @@ -602,13 +605,13 @@ const createDirInfos = async (
dirToDirInfo,
gitignore,
ignoreFiles,
noRequire
noImport
);
await enumerateParents(
fs,
baseDir,
dirToDirInfo,
noRequire
noImport
);

// Merge file lists with identical configuration
Expand Down Expand Up @@ -642,7 +645,7 @@ const createDirInfos = async (
importModuleIds(
[ effectiveDir, ...effectiveModulePaths ],
markdownlintOptions.customRules,
noRequire
noImport
).then((customRules) => {
// Expand nested arrays (for packages that export multiple rules)
markdownlintOptions.customRules = customRules.flat();
Expand All @@ -654,7 +657,7 @@ const createDirInfos = async (
importModuleIdsAndParams(
[ effectiveDir, ...effectiveModulePaths ],
markdownlintOptions.markdownItPlugins,
noRequire
noImport
).then((markdownItPlugins) => {
markdownlintOptions.markdownItPlugins = markdownItPlugins;
})
Expand Down Expand Up @@ -847,7 +850,7 @@ const outputSummary = async (
modulePaths,
logMessage,
logError,
noRequire
noImport
) => {
const errorsPresent = (summary.length > 0);
if (errorsPresent || outputFormatters) {
Expand All @@ -860,7 +863,7 @@ const outputSummary = async (
const dir = relativeDir || baseDir;
const dirs = [ dir, ...modulePaths ];
const formattersAndParams = outputFormatters
? await importModuleIdsAndParams(dirs, outputFormatters, noRequire)
? await importModuleIdsAndParams(dirs, outputFormatters, noImport)
// eslint-disable-next-line no-inline-comments, unicorn/no-await-expression-member
: [ [ (await import(/* webpackMode: "eager" */ "markdownlint-cli2-formatter-default")).default ] ];
await Promise.all(formattersAndParams.map((formatterAndParams) => {
Expand All @@ -880,7 +883,7 @@ export const main = async (params) => {
optionsDefault,
optionsOverride,
fileContents,
noRequire,
noImport,
allowStdin
} = params;
let {
Expand Down Expand Up @@ -937,7 +940,7 @@ export const main = async (params) => {
const resolvedConfigPath =
posixPath(pathDefault.resolve(baseDirSystem, configPath));
optionsArgv =
await readOptionsOrConfig(resolvedConfigPath, fs, noRequire);
await readOptionsOrConfig(resolvedConfigPath, fs, noImport);
relativeDir = pathPosix.dirname(resolvedConfigPath);
}
// Process arguments and get base options
Expand All @@ -950,7 +953,7 @@ export const main = async (params) => {
optionsArgv || optionsDefault,
fixDefault,
noGlobs,
noRequire
noImport
);
} finally {
if (!baseOptions?.baseMarkdownlintOptions.noBanner) {
Expand Down Expand Up @@ -1009,7 +1012,7 @@ export const main = async (params) => {
optionsOverride,
gitignore,
ignoreFiles,
noRequire
noImport
);
// Output linting status
if (showProgress) {
Expand Down Expand Up @@ -1047,7 +1050,7 @@ export const main = async (params) => {
modulePaths,
logMessage,
logError,
noRequire
noImport
);
// Return result
return errorsPresent ? 1 : 0;
Expand Down
Loading

0 comments on commit 6e460ca

Please sign in to comment.