Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions __tests__/lint-worker.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import lintWorker from '../src/utils/lint-worker';

describe('lintWorker', () => {
test('returns lint results for the provided content list', async () => {
const [result] = lintWorker({
contentList: ['中English\n'],
isFixMode: false,
rules: {},
});

expect(result.lintResult.length).toBeGreaterThan(0);
});
});
76 changes: 35 additions & 41 deletions src/utils/batch-lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { readFile } from 'fs/promises';
import type { LintMdRulesConfig, lintMarkdown } from '@lint-md/core';
import { Piscina } from 'piscina';
import type { LintWorkerOptions } from '../types';
import { averagedGroup } from './averaged-group';

export async function runTasksWithLimit<T>(
tasks: (() => Promise<T>)[],
Expand Down Expand Up @@ -38,50 +37,45 @@ export const batchLint = async (
maxThreads: concurrency,
});

const fileContentList = await runTasksWithLimit(
mdFilePaths.map((filePath) => {
return async () => {
const res = await readFile(filePath);
return {
path: filePath,
content: res.toString(),
};
};
}),
concurrency
);

// 将 md 文件内容进行分组,供各个线程分配执行
const markdownContentGroup = averagedGroup(fileContentList, concurrency, (item) => {
return item.content.length;
});

const res = await Promise.all(
markdownContentGroup.map((groupItem) => {
const asyncCall = async () => {
const batchLintResult: ReturnType<typeof lintMarkdown>[]
= await lintWorkerPool.run({
contentList: groupItem.items.map((value) => {
return value.content;
}),
isFixMode,
rules,
isDev,
} as LintWorkerOptions);
const problemResults: {
path: string
lintResult: ReturnType<typeof lintMarkdown>['lintResult']
fixedResult?: ReturnType<typeof lintMarkdown>['fixedResult']
}[] = [];

return batchLintResult.map((lintResult, index) => {
for (let start = 0; start < mdFilePaths.length; start += concurrency) {
const currentBatchPaths = mdFilePaths.slice(start, start + concurrency);
const fileContentList = await runTasksWithLimit(
currentBatchPaths.map((filePath) => {
return async () => {
const content = await readFile(filePath, 'utf8');
return {
...lintResult,
path: groupItem.items[index].path,
path: filePath,
content,
};
});
};
}),
concurrency
);

const batchLintResult = await lintWorkerPool.run({
contentList: fileContentList.map(item => item.content),
isFixMode,
rules,
isDev,
} as LintWorkerOptions) as ReturnType<typeof lintMarkdown>[];

const batchResults = batchLintResult.map((lintResult, index) => {
return {
...lintResult,
path: fileContentList[index].path,
};
});

return asyncCall();
})
);
problemResults.push(...batchResults.filter((item) => {
return item.lintResult.length > 0;
}));
}

return res.flat().filter((item) => {
return item.lintResult.length > 0;
});
return problemResults;
};