Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cache context #816

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion src/cmd/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import {prepareMapFile} from '../../steps/processMapFile';
import {copyFiles, logger} from '../../utils';
import {upload as publishFilesToS3} from '../../commands/publish/upload';
import {CacheContextCli} from '~/context/cache';

export const build = {
command: ['build', '$0'],
Expand Down Expand Up @@ -174,13 +175,13 @@
);
}

async function handler(args: Arguments<any>) {

Check warning on line 178 in src/cmd/build/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
const userOutputFolder = resolve(args.output);
const tmpInputFolder = resolve(args.output, TMP_INPUT_FOLDER);
const tmpOutputFolder = resolve(args.output, TMP_OUTPUT_FOLDER);

if (typeof VERSION !== 'undefined') {
console.log(`Using v${VERSION} version`);

Check warning on line 184 in src/cmd/build/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
}

try {
Expand All @@ -190,7 +191,7 @@
input: tmpInputFolder,
output: tmpOutputFolder,
});
Includers.init([OpenapiIncluder as any]);

Check warning on line 194 in src/cmd/build/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type

const {
output: outputFolderPath,
Expand All @@ -212,14 +213,16 @@

const outputBundlePath = join(outputFolderPath, BUNDLE_FOLDER);

const cache = new CacheContextCli();

if (!lintDisabled) {
/* Initialize workers in advance to avoid a timeout failure due to not receiving a message from them */
await initLinterWorkers();
}

const processes = [
!lintDisabled && processLinter(),
!buildDisabled && processPages(outputBundlePath),
!buildDisabled && processPages(outputBundlePath, cache),
].filter(Boolean) as Promise<void>[];

await Promise.all(processes);
Expand Down
15 changes: 15 additions & 0 deletions src/context/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {CacheContext} from '@diplodoc/transform/lib/typings';

export class CacheContextCli implements CacheContext {
cache: {
[key: string]: string | null | undefined;
} = {};

get(key: string) {
return this.cache[key];
}

set(key: string, value: string) {
this.cache[key] = value;
}
}
4 changes: 4 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Logger} from '@diplodoc/transform/lib/log';
import {ChangelogItem} from '@diplodoc/transform/lib/plugins/changelog/types';
import {LintConfig} from '@diplodoc/transform/lib/yfmlint';
import type {DocAnalytics} from '@diplodoc/client';
import {CacheContext} from '@diplodoc/transform/lib/typings';

import {IncludeMode, Lang, ResourceType, Stage} from './constants';
import {FileContributors, VCSConnector, VCSConnectorConfig} from './vcs-connector/connector-models';
Expand Down Expand Up @@ -248,6 +249,7 @@ export interface PluginOptions {
collectOfPlugins?: (input: string, options: PluginOptions) => string;
changelogs?: ChangelogItem[];
extractChangelogs?: boolean;
cache?: CacheContext;
}

export interface Plugin {
Expand All @@ -258,6 +260,7 @@ export interface ResolveMd2MdOptions {
inputPath: string;
outputPath: string;
metadata: MetaDataOptions;
cache?: CacheContext;
}

export interface ResolverOptions {
Expand All @@ -269,6 +272,7 @@ export interface ResolverOptions {
outputPath: string;
outputBundlePath: string;
metadata?: MetaDataOptions;
cache?: CacheContext;
}

export interface PathData {
Expand Down
9 changes: 6 additions & 3 deletions src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import log from '@diplodoc/transform/lib/log';
import {MarkdownItPluginCb} from '@diplodoc/transform/lib/plugins/typings';
import {getPublicPath, isFileExists} from '@diplodoc/transform/lib/utilsFS';
import yaml from 'js-yaml';
import {CacheContext} from '@diplodoc/transform/lib/typings';

import {Lang, PROCESSING_FINISHED} from '../constants';
import {LeadingPage, ResolverOptions, YfmToc} from '../models';
Expand All @@ -28,6 +29,7 @@ import {
export interface FileTransformOptions {
path: string;
root?: string;
cache?: CacheContext;
}

const FileTransformer: Record<string, Function> = {
Expand All @@ -39,14 +41,14 @@ const fixRelativePath = (relativeTo: string) => (path: string) => {
return join(getAssetsPublicPath(relativeTo), path);
};

const getFileMeta = async ({fileExtension, metadata, inputPath}: ResolverOptions) => {
const getFileMeta = async ({fileExtension, metadata, inputPath, cache}: ResolverOptions) => {
const {input, allowCustomResources} = ArgvService.getConfig();

const resolvedPath: string = resolve(input, inputPath);
const content: string = readFileSync(resolvedPath, 'utf8');

const transformFn: Function = FileTransformer[fileExtension];
const {result} = transformFn(content, {path: inputPath});
const {result} = transformFn(content, {path: inputPath, cache});

const vars = getVarsPerFile(inputPath);
const updatedMetadata = metadata?.isContributorsEnabled
Expand Down Expand Up @@ -213,7 +215,7 @@ export function liquidMd2Html(input: string, vars: Record<string, unknown>, path

function MdFileTransformer(content: string, transformOptions: FileTransformOptions): Output {
const {input, ...options} = ArgvService.getConfig();
const {path: filePath} = transformOptions;
const {path: filePath, cache} = transformOptions;

const plugins = PluginService.getPlugins();
const vars = getVarsPerFile(filePath);
Expand All @@ -231,5 +233,6 @@ function MdFileTransformer(content: string, transformOptions: FileTransformOptio
getVarsPerFile: getVarsPerRelativeFile,
getPublicPath,
extractTitle: true,
cache,
});
}
5 changes: 4 additions & 1 deletion src/resolvers/md2md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {ChangelogItem} from '@diplodoc/transform/lib/plugins/changelog/types';
import {enrichWithFrontMatter} from '../services/metadata';

export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void> {
const {inputPath, outputPath, metadata: metadataOptions} = options;
const {inputPath, outputPath, metadata: metadataOptions, cache} = options;
const {input, output, changelogs: changelogsSetting} = ArgvService.getConfig();
const resolvedInputPath = resolve(input, inputPath);

Expand All @@ -36,6 +36,7 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void>
vars: vars,
log,
copyFile,
cache,
});

writeFileSync(outputPath, result);
Expand Down Expand Up @@ -113,6 +114,7 @@ function transformMd2Md(input: string, options: PluginOptions) {
collectOfPlugins,
log: pluginLog,
copyFile: pluginCopyFile,
cache,
} = options;

let output = input;
Expand All @@ -136,6 +138,7 @@ function transformMd2Md(input: string, options: PluginOptions) {
collectOfPlugins,
changelogs,
extractChangelogs: Boolean(changelogsSetting),
cache,
});
}

Expand Down
18 changes: 14 additions & 4 deletions src/steps/processPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ import {
import {getVCSConnector} from '../vcs-connector';
import {VCSConnector} from '../vcs-connector/connector-models';
import {generateStaticRedirect} from '../utils/redirect';
import {CacheContext} from '@diplodoc/transform/lib/typings';

const singlePageResults: Record<string, SinglePageResult[]> = {};
const singlePagePaths: Record<string, Set<string>> = {};

// Processes files of documentation (like index.yaml, *.md)
export async function processPages(outputBundlePath: string): Promise<void> {
export async function processPages(outputBundlePath: string, cache: CacheContext): Promise<void> {
const {
input: inputFolderPath,
output: outputFolderPath,
Expand Down Expand Up @@ -74,6 +75,7 @@ export async function processPages(outputBundlePath: string): Promise<void> {
metaDataOptions,
resolveConditions,
singlePage,
cache,
);
}),
);
Expand Down Expand Up @@ -253,6 +255,7 @@ async function preparingPagesByOutputFormat(
metaDataOptions: MetaDataOptions,
resolveConditions: boolean,
singlePage: boolean,
cache: CacheContext,
): Promise<void> {
const {
filename,
Expand Down Expand Up @@ -289,10 +292,10 @@ async function preparingPagesByOutputFormat(

switch (outputFormat) {
case 'md':
await processingFileToMd(path, metaDataOptions);
await processingFileToMd(path, metaDataOptions, cache);
return;
case 'html': {
const resolvedFileProps = await processingFileToHtml(path, metaDataOptions);
const resolvedFileProps = await processingFileToHtml(path, metaDataOptions, cache);

if (singlePage) {
savePageResultForSinglePage(resolvedFileProps, path);
Expand Down Expand Up @@ -333,19 +336,25 @@ function copyFileWithoutChanges(
shell.cp(from, to);
}

async function processingFileToMd(path: PathData, metaDataOptions: MetaDataOptions): Promise<void> {
async function processingFileToMd(
path: PathData,
metaDataOptions: MetaDataOptions,
cache: CacheContext,
): Promise<void> {
const {outputPath, pathToFile} = path;

await resolveMd2Md({
inputPath: pathToFile,
outputPath,
metadata: metaDataOptions,
cache,
});
}

async function processingFileToHtml(
path: PathData,
metaDataOptions: MetaDataOptions,
cache: CacheContext,
): Promise<DocInnerProps> {
const {outputBundlePath, filename, fileExtension, outputPath, pathToFile} = path;
const {deepBase, deep} = TocService.getDeepForPath(pathToFile);
Expand All @@ -359,5 +368,6 @@ async function processingFileToHtml(
metadata: metaDataOptions,
deep,
deepBase,
cache,
});
}
Loading