mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
42 lines
1.5 KiB
JavaScript
42 lines
1.5 KiB
JavaScript
// @ts-check
|
|
|
|
// eslint-disable-next-line n/no-unsupported-features/node-builtins
|
|
import { availableParallelism } from "node:os";
|
|
import { Worker } from "node:worker_threads";
|
|
import { __filename } from "./esm-helpers.mjs";
|
|
import markdownlint from "../lib/markdownlint.mjs";
|
|
const markdownlintSync = markdownlint.sync;
|
|
|
|
/**
|
|
* Lint specified Markdown files (using multiple threads).
|
|
*
|
|
* @param {import("../lib/markdownlint.mjs").Options} options Configuration options.
|
|
* @returns {Promise<import("../lib/markdownlint.mjs").LintResults>} Results object.
|
|
*/
|
|
export function markdownlintParallel(options) {
|
|
const workerCount = availableParallelism();
|
|
const files = options.files || [];
|
|
const chunkSize = Math.ceil(files.length / workerCount);
|
|
const promises = [];
|
|
for (let i = 0; i < workerCount; i++) {
|
|
promises.push(new Promise((resolve, reject) => {
|
|
const workerData = {
|
|
...options,
|
|
"files": files.slice(i * chunkSize, (i + 1) * chunkSize)
|
|
};
|
|
const worker = new Worker(__filename(import.meta).replace(/parallel\.mjs$/, "worker.mjs"), { workerData });
|
|
worker.on("message", resolve);
|
|
worker.on("error", reject);
|
|
}));
|
|
}
|
|
return Promise.all(promises).then((workerResults) => {
|
|
const combinedResults = markdownlintSync(null);
|
|
for (const workerResult of workerResults) {
|
|
// eslint-disable-next-line guard-for-in
|
|
for (const result in workerResult) {
|
|
combinedResults[result] = workerResult[result];
|
|
}
|
|
}
|
|
return combinedResults;
|
|
});
|
|
}
|