2024-06-07 22:24:28 -07:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
// eslint-disable-next-line n/no-unsupported-features/node-builtins
|
2024-11-28 20:36:44 -08:00
|
|
|
import { availableParallelism } from "node:os";
|
|
|
|
import { Worker } from "node:worker_threads";
|
2024-12-03 19:58:28 -08:00
|
|
|
import { lint } from "markdownlint/sync";
|
2024-11-28 20:36:44 -08:00
|
|
|
import { __filename } from "./esm-helpers.mjs";
|
2024-06-07 22:24:28 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Lint specified Markdown files (using multiple threads).
|
|
|
|
*
|
2024-12-03 19:58:28 -08:00
|
|
|
* @param {import("markdownlint").Options} options Configuration options.
|
|
|
|
* @returns {Promise<import("markdownlint").LintResults>} Results object.
|
2024-06-07 22:24:28 -07:00
|
|
|
*/
|
2024-11-28 20:36:44 -08:00
|
|
|
export function markdownlintParallel(options) {
|
2024-06-07 22:24:28 -07:00
|
|
|
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)
|
2024-09-01 16:16:05 -07:00
|
|
|
};
|
2024-11-28 20:36:44 -08:00
|
|
|
const worker = new Worker(__filename(import.meta).replace(/parallel\.mjs$/, "worker.mjs"), { workerData });
|
2024-06-07 22:24:28 -07:00
|
|
|
worker.on("message", resolve);
|
|
|
|
worker.on("error", reject);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return Promise.all(promises).then((workerResults) => {
|
2024-12-03 19:58:28 -08:00
|
|
|
const combinedResults = lint(null);
|
2024-06-07 22:24:28 -07:00
|
|
|
for (const workerResult of workerResults) {
|
|
|
|
// eslint-disable-next-line guard-for-in
|
|
|
|
for (const result in workerResult) {
|
|
|
|
combinedResults[result] = workerResult[result];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return combinedResults;
|
|
|
|
});
|
|
|
|
}
|