Skip to content

Commit

Permalink
Merge pull request #342 from yandex-cloud/moki-codes/drop-includer-fi…
Browse files Browse the repository at this point in the history
…eld-support

includers: drop includer field support
  • Loading branch information
v8tenko authored May 12, 2023
2 parents 080b824 + f1fa3ad commit 58b0e8c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 80 deletions.
28 changes: 11 additions & 17 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,30 @@ export interface YfmTocInclude {
repo: string;
path: string;
mode?: IncludeMode;
includer?: YfmTocIncluder;
includers?: YfmTocIncluders;
}

export type YfmTocIncludersNormalized = YfmTocIncluderObject[];

export type YfmTocIncluders = YfmTocIncluder[];

export type YfmTocIncluder = YfmTocIncluderName | YfmTocIncluderObject;

export const includersNames = ['sourcedocs', 'openapi', 'generic', 'unarchive'] as const;

export type YfmTocIncluderName = typeof includersNames[number];

export type YfmTocIncluderObject = {
export type YfmTocIncluder = {
name: YfmTocIncluderName;
// arbitrary includer parameters
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} & Record<string, unknown>;

export type Includer = {
export const includersNames = ['sourcedocs', 'openapi', 'generic', 'unarchive'] as const;

export type YfmTocIncluderName = typeof includersNames[number];

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Includer<FnParams = any> = {
name: YfmTocIncluderName;
includerFunction: IncluderFunction;
includerFunction: IncluderFunction<FnParams>;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type IncluderFunction = (args: IncluderFunctionParams) => Promise<any>;
export type IncluderFunction<PassedParams> = (args: IncluderFunctionParams<PassedParams>) => Promise<void>;

export type IncluderFunctionParams<Params extends Record<string, unknown> = Record<string, unknown>> = {
export type IncluderFunctionParams<PassedParams> = {
// item that contains include that uses includer
item: YfmToc;
// base read directory path
Expand All @@ -125,8 +120,7 @@ export type IncluderFunctionParams<Params extends Record<string, unknown> = Reco
tocPath: string;
vars: YfmPreset;
// arbitrary includer parameters
// eslint-disable-next-line @typescript-eslint/no-explicit-any
passedParams: Params;
passedParams: PassedParams;
index: number;
};

Expand Down
2 changes: 0 additions & 2 deletions src/services/includers/batteries/unarchive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ async function includerFunction(params: IncluderFunctionParams<Params>) {
} catch (err) {
throw new UnarchiveIncluderError(err.toString(), tocPath);
}

return {input: output};
}

export {name, includerFunction};
Expand Down
71 changes: 10 additions & 61 deletions src/services/includers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {isObject} from 'lodash';

import {ArgvService} from '../index';
import {IncludeMode} from '../../constants';
import {logger} from '../../utils/logger';
import {generic, sourcedocs, openapi, unarchive} from './batteries';

import type {
Expand All @@ -14,17 +13,8 @@ import type {
YfmTocInclude,
YfmTocIncluder,
YfmTocIncluders,
YfmTocIncluderObject,
YfmTocIncludersNormalized,
} from '../../models';

const includerUsage = `include:
path: <path-where-to-include>
includer:
name: <includer-name>
<includer-parameter>: <value-for-includer-parameter>
`;

const includersUsage = `include:
path: <path-where-to-include>
includers:
Expand Down Expand Up @@ -60,7 +50,7 @@ function init(custom: Includer[] = []) {
}

async function applyIncluders(path: string, item: YfmToc, vars: YfmPreset) {
if (!(item && item.include) || !includeHasIncluders(item.include)) {
if (!item.include?.includers) {
return;
}

Expand All @@ -71,19 +61,15 @@ async function applyIncluders(path: string, item: YfmToc, vars: YfmPreset) {
// normalize include mode (includers support link mode only)
item.include.mode = IncludeMode.LINK;

const includers = normalizeIncludeIncluders(path, item.include) as YfmTocIncludersNormalized;

item.include.includers = includers;
const {status, message} = includersValid(item.include.includers);
if (!status) {
throw new IncludersError(message ?? '', path);
}

let index = 0;

for (const {name, ...rest} of includers) {
for (const {name, ...rest} of item.include.includers) {
const includer = getIncluder(name);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const passedParams: Record<string, any> = {
...rest,
};
const passedParams = {...rest};

await applyIncluder({path, item, includer, passedParams, index, vars});
}
Expand All @@ -94,39 +80,10 @@ async function applyIncluders(path: string, item: YfmToc, vars: YfmPreset) {
index++;
}

function includeHasIncluders(include: YfmTocInclude) {
return include.includer || include.includers;
}

function includeValid(include: YfmTocInclude) {
return (include.mode === IncludeMode.LINK || !include.mode) && include.path?.length;
}

function normalizeIncludeIncluders(path: string, include: YfmTocInclude) {
if (include.includers) {
const {status, message} = includersValid(include.includers);

if (status) {
return include.includers;
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
throw new IncludersError(message!, path);
}

logger.warn(path, 'includer field is getting depricated, use includers field\n' + includersUsage);

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const {status, message} = includerValid(include.includer!);

if (status) {
return [include.includer];
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
throw new IncludersError(message!, path);
}

function includersValid(includers: YfmTocIncluders) {
for (const includer of includers) {
const {status, message} = includerValid(includer);
Expand All @@ -140,19 +97,11 @@ function includersValid(includers: YfmTocIncluders) {
}

function includerValid(includer: YfmTocIncluder) {
if (Array.isArray(includer)) {
return {status: false, message: `use includers field to provide multiple includers:\n${includersUsage}`};
}

if (typeof includer === 'string') {
return {status: false, message: `use updated includer format:\n${includerUsage}`};
}

if (isObject(includer)) {
if (typeof includer.name !== 'string') {
return {
status: false,
message: 'use string to specify includers name',
message: 'use string in the includer.name to specify includers name',
};
}

Expand All @@ -165,15 +114,15 @@ function includerValid(includer: YfmTocIncluder) {

return {
status: false,
message: `use appropriate includer/includers format:\n${includerUsage}${includersUsage}`,
message: `use appropriate includers format:\n${includersUsage}`,
};
}

function getIncluder(includerName: string) {
return includersMap[includerName];
}

function includerExists(includer: YfmTocIncluderObject) {
function includerExists(includer: YfmTocIncluder) {
return includersMap[includer.name as keyof typeof includersMap];
}

Expand Down

0 comments on commit 58b0e8c

Please sign in to comment.