-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
326 additions
and
422 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,41 @@ | ||
import log from '@diplodoc/transform/lib/log'; | ||
import {spawn, Worker, Thread} from 'threads'; | ||
import {PluginService, TocService} from '../services'; | ||
import {getChunkSize} from '../utils/workers'; | ||
import {runPool} from './processPool'; | ||
import {chunk} from 'lodash'; | ||
import {lintPage} from '../resolvers'; | ||
import {extname} from 'path'; | ||
|
||
import {ArgvService, TocService, PresetService, PluginService} from '../services'; | ||
import {ProcessLinterWorker} from '../workers/linter'; | ||
import {logger} from '../utils'; | ||
import {LINTING_FINISHED, WORKERS_COUNT, MIN_CHUNK_SIZE} from '../constants'; | ||
import {lintPage} from '../resolvers'; | ||
import {splitOnChunks} from '../utils/worker'; | ||
import {getChunkSize} from '../utils/workers'; | ||
|
||
let processLinterWorkers: (ProcessLinterWorker & Thread)[]; | ||
let navigationPathsChunks: string[][]; | ||
import {LINTING_FINISHED} from '../constants'; | ||
|
||
export async function processLinter(): Promise<void> { | ||
const argvConfig = ArgvService.getConfig(); | ||
|
||
const navigationPaths = TocService.getNavigationPaths(); | ||
|
||
if (!processLinterWorkers) { | ||
lintPagesFallback(navigationPaths); | ||
|
||
if (process.env.DISABLE_PARALLEL_BUILD) { | ||
await lintPagesFallback(navigationPaths); | ||
return; | ||
} | ||
|
||
const presetStorageDump = PresetService.dump(); | ||
|
||
/* Subscribe on the linted page event */ | ||
processLinterWorkers.forEach((worker) => { | ||
worker.getProcessedPages().subscribe((pathToFile) => { | ||
logger.info(pathToFile as string, LINTING_FINISHED); | ||
}); | ||
}); | ||
|
||
/* Run processing the linter */ | ||
await Promise.all( | ||
processLinterWorkers.map((worker, i) => { | ||
const navigationPathsChunk = navigationPathsChunks[i]; | ||
|
||
return worker.run({ | ||
argvConfig, | ||
navigationPaths: navigationPathsChunk, | ||
presetStorageDump, | ||
}); | ||
}), | ||
); | ||
|
||
/* Unsubscribe from workers */ | ||
await Promise.all( | ||
processLinterWorkers.map((worker) => { | ||
return worker.finish().then((logs) => { | ||
log.add(logs); | ||
}); | ||
}), | ||
); | ||
const navigationPathsChunks = chunk(navigationPaths, getChunkSize(navigationPaths)); | ||
|
||
/* Terminate workers */ | ||
await Promise.all( | ||
processLinterWorkers.map((worker) => { | ||
return Thread.terminate(worker); | ||
navigationPathsChunks.map(async (navigationPathsChunk) => { | ||
await runPool('lint', navigationPathsChunk); | ||
}), | ||
); | ||
} | ||
|
||
export async function initLinterWorkers() { | ||
const navigationPaths = TocService.getNavigationPaths() | ||
.filter((filename) => extname(filename) === '.md'); | ||
const chunkSize = getChunkSize(navigationPaths); | ||
|
||
if (process.env.DISABLE_PARALLEL_BUILD || chunkSize < MIN_CHUNK_SIZE || WORKERS_COUNT <= 0) { | ||
return; | ||
} | ||
|
||
navigationPathsChunks = splitOnChunks(navigationPaths, chunkSize).filter((arr) => arr.length); | ||
|
||
const workersCount = navigationPathsChunks.length; | ||
async function lintPagesFallback(navigationPaths: string[]) { | ||
PluginService.setPlugins(); | ||
|
||
processLinterWorkers = await Promise.all( | ||
new Array(workersCount).fill(null).map(() => { | ||
// TODO: get linter path from env | ||
return spawn<ProcessLinterWorker>(new Worker('./linter'), {timeout: 60000}); | ||
await Promise.all( | ||
navigationPaths.map(async (pathToFile) => { | ||
await lintPage({ | ||
inputPath: pathToFile, | ||
fileExtension: extname(pathToFile), | ||
onFinish: () => { | ||
logger.info(pathToFile, LINTING_FINISHED); | ||
}, | ||
}); | ||
}), | ||
); | ||
} | ||
|
||
function lintPagesFallback(navigationPaths: string[]) { | ||
PluginService.setPlugins(); | ||
|
||
navigationPaths.forEach((pathToFile) => { | ||
lintPage({ | ||
inputPath: pathToFile, | ||
fileExtension: extname(pathToFile), | ||
onFinish: () => { | ||
logger.info(pathToFile, LINTING_FINISHED); | ||
}, | ||
}); | ||
}); | ||
} |
Oops, something went wrong.