-
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
35 changed files
with
919 additions
and
975 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type {Run} from './run'; | ||
|
||
import {ArgvService, PluginService, PresetService} from '~/services'; | ||
Check failure on line 3 in src/commands/lint/handler.ts
|
||
import {processLogs} from '~/steps'; | ||
import {bounded, isExternalHref, logger, own} from '~/utils'; | ||
Check failure on line 5 in src/commands/lint/handler.ts
|
||
import {LINTING_FINISHED, MIN_CHUNK_SIZE, WORKERS_COUNT} from '~/constants'; | ||
import log from '@diplodoc/transform/lib/log'; | ||
import {Pool, Thread, Worker, spawn} from 'threads'; | ||
import {ProcessLinterWorker} from '~/workers/linter'; | ||
import {lintPage} from '~/resolvers'; | ||
import {extname} from 'path'; | ||
|
||
function connect(run: Run, linter: Worker) { | ||
return linter; | ||
} | ||
|
||
export async function handler(run: Run) { | ||
try { | ||
if (!run.config.lint.enabled) { | ||
return; | ||
} | ||
|
||
const workers = new Pool( | ||
async () => { | ||
return connect(run, await spawn(new Worker('./linter'))); | ||
}, | ||
{ | ||
size: WORKERS_COUNT, | ||
}, | ||
); | ||
|
||
run.toc.hooks.ItemResolved.tap((item, toc, path) => { | ||
if (own<string, 'href'>(item, 'href') && !isExternalHref(item.href)) { | ||
workers.queue(async (linter) => { | ||
const result = await linter.process([item.href, path]); | ||
|
||
run.logger.info(path, LINTING_FINISHED); | ||
|
||
return result; | ||
}); | ||
} | ||
}); | ||
|
||
await run.toc.init(); | ||
await workers.completed(); | ||
await workers.terminate(); | ||
|
||
/* Subscribe on the linted page event */ | ||
// await workers.map(([worker]) => { | ||
// worker.getProcessedPages().subscribe((pathToFile) => { | ||
// logger.info(pathToFile as string, LINTING_FINISHED); | ||
// }); | ||
// }); | ||
|
||
/* Run processing the linter */ | ||
// await workers.map(([worker, chunk]) => { | ||
// await worker.init(config, chunk); | ||
// return run.start(); | ||
// }); | ||
// | ||
// /* Unsubscribe from workers */ | ||
// await workers.map(([worker]) => { | ||
// return worker.finish().then((logs) => { | ||
// log.add(logs); | ||
// }); | ||
// }); | ||
|
||
// await workers.terminate(); | ||
} catch (error) { | ||
run.logger.error(error); | ||
} finally { | ||
processLogs(run.input); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {intercept} from '~/utils'; | ||
import {AsyncParallelHook, AsyncSeriesHook} from 'tapable'; | ||
|
||
const name = 'Lint'; | ||
|
||
export function hooks() { | ||
return intercept(name, { | ||
/** | ||
* Async series hook which runs before start of any Run type.<br/><br/> | ||
* Args: | ||
* - run - [Build.Run](./Run.ts) constructed context.<br/> | ||
* Best place to subscribe on Run hooks. | ||
*/ | ||
BeforeAnyRun: new AsyncSeriesHook<Run>(['run'], `${name}.BeforeAnyRun`), | ||
/** | ||
* Async parallel hook which runs on start of any Run type.<br/><br/> | ||
* Args: | ||
* - run - [Build.Run](./Run.ts) constructed context.<br/> | ||
* Best place to do something in parallel with main build process. | ||
*/ | ||
Run: new AsyncParallelHook<Run>(['run'], `${name}.Run`), | ||
AfterAnyRun: new AsyncSeriesHook<Run>(['run'], `${name}.AfterAnyRun`), | ||
}); | ||
} | ||
|
||
export const Hooks = Symbol(`${name}Hooks`); | ||
|
||
export function getHooks(program: {[Hooks]?: ReturnType<typeof hooks>}) { | ||
return program[Hooks] || hooks(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import type {IProgram, BaseArgs as ProgramArgs, BaseConfig as ProgramConfig} from '~/core/program'; | ||
|
||
import {pick} from 'lodash'; | ||
|
||
import {BaseProgram} from '~/core/program'; | ||
import {GenericIncluderExtension, OpenapiIncluderExtension} from '~/core/toc'; | ||
import {Stage, YFM_CONFIG_FILENAME} from '~/constants'; | ||
import {Command, Config} from '~/config'; | ||
|
||
import {options} from '../build/config'; | ||
import {Run} from '../build/run'; | ||
import {Templating, TemplatingArgs, TemplatingConfig} from '../build/features/templating'; | ||
import {LintArgs, LintConfig} from '../build/features/linter'; | ||
|
||
import {handler} from './handler'; | ||
import {Hooks, hooks} from './hooks'; | ||
|
||
type BaseArgs = {output: AbsolutePath}; | ||
|
||
type BaseConfig = { | ||
varsPreset: string; | ||
vars: Hash; | ||
allowHtml: boolean; | ||
sanitizeHtml: boolean; | ||
ignoreStage: string[]; | ||
ignore: string[]; | ||
}; | ||
|
||
export type {Run}; | ||
|
||
const command = 'Build'; | ||
|
||
export type CommandArgs = ProgramArgs & BaseArgs & Partial<TemplatingArgs & LintArgs>; | ||
|
||
export type CommandConfig = Config< | ||
BaseArgs & ProgramConfig & BaseConfig & TemplatingConfig & LintConfig | ||
>; | ||
|
||
export class Lint | ||
// eslint-disable-next-line new-cap | ||
extends BaseProgram<CommandConfig, CommandArgs>(command, { | ||
config: { | ||
scope: 'link', | ||
defaults: () => | ||
({ | ||
varsPreset: 'default', | ||
vars: {}, | ||
ignore: [], | ||
allowHtml: true, | ||
sanitizeHtml: true, | ||
ignoreStage: [Stage.SKIP], | ||
lint: {enabled: true, config: {'log-levels': {}}}, | ||
}) as Partial<CommandConfig>, | ||
}, | ||
}) | ||
implements IProgram<CommandArgs> | ||
{ | ||
readonly [Hooks] = hooks(); | ||
|
||
readonly templating = new Templating(); | ||
|
||
// readonly linter = new Lint(); | ||
|
||
readonly command = new Command('build').description('Build documentation in target directory'); | ||
|
||
readonly options = [ | ||
options.input('./'), | ||
options.output({required: true}), | ||
options.varsPreset, | ||
options.vars, | ||
options.allowHtml, | ||
options.sanitizeHtml, | ||
options.ignore, | ||
options.ignoreStage, | ||
options.config(YFM_CONFIG_FILENAME), | ||
]; | ||
|
||
apply(program?: IProgram) { | ||
this[Hooks].Config.tap('Lint', (config, args) => { | ||
config.ignoreStage = ([] as string[]).concat(config.ignoreStage); | ||
|
||
return config; | ||
}); | ||
|
||
this.templating.apply(this); | ||
this.linter.apply(this); | ||
|
||
new GenericIncluderExtension().apply(this); | ||
new OpenapiIncluderExtension().apply(this); | ||
|
||
super.apply(program); | ||
} | ||
|
||
async action(config?: CommandConfig, chunk?: NormalizedPath[]) { | ||
const run = new Run(config || this.config); | ||
|
||
run.logger.pipe(this.logger); | ||
|
||
await this[Hooks].BeforeAnyRun.promise(run); | ||
|
||
await run.toc.init(chunk); | ||
|
||
await Promise.all([handler(run), this[Hooks].Run.promise(run)]); | ||
|
||
await this[Hooks].AfterAnyRun.promise(run); | ||
} | ||
} |
Empty file.
Oops, something went wrong.