Skip to content

Commit

Permalink
fix(cache): fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Feverqwe committed Oct 19, 2023
1 parent c8991bf commit 2a7049a
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 134 deletions.
19 changes: 12 additions & 7 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import {Lang, Stage, IncludeMode, ResourceType} from './constants';
import {ChangelogItem} from '@diplodoc/transform/lib/plugins/changelog/types';
import PluginEnvApi from './utils/pluginEnvApi';

export type VarsPreset = 'internal'|'external';
export type VarsPreset = 'internal' | 'external';

export type YfmPreset = Record<string, string>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Metadata = Record<string, any>;

export type ExternalAuthorByPathFunction = (path: string) => Contributor | null;
export type ContributorsByPathFunction = (path: string) => Promise<FileContributors>;
export type NestedContributorsForPathFunction = (path: string, nestedContributors: Contributors) => void;
export type NestedContributorsForPathFunction = (
path: string,
nestedContributors: Contributors,
) => void;
export type UserByLoginFunction = (login: string) => Promise<Contributor | null>;
export type CollectionOfPluginsFunction = (output: string, options: PluginOptions) => string;

Expand Down Expand Up @@ -99,15 +102,17 @@ export type YfmTocIncluder = {

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

export type YfmTocIncluderName = typeof includersNames[number];
export type YfmTocIncluderName = (typeof includersNames)[number];

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

export type IncluderFunction<PassedParams> = (args: IncluderFunctionParams<PassedParams>) => Promise<void>;
export type IncluderFunction<PassedParams> = (
args: IncluderFunctionParams<PassedParams>,
) => Promise<void>;

export type IncluderFunctionParams<PassedParams> = {
// item that contains include that uses includer
Expand Down Expand Up @@ -151,7 +156,7 @@ export interface LeadingPageLinks extends Filter {
}

export interface Filter {
when?: boolean|string;
when?: boolean | string;
[key: string]: unknown;
}

Expand Down Expand Up @@ -243,8 +248,8 @@ export type Resources = {
};

export type YandexCloudTranslateGlossaryPair = {
sourceText: string;
translatedText: string;
sourceText: string;
translatedText: string;
};

export type CommitInfo = {
Expand Down
1 change: 0 additions & 1 deletion src/resolvers/lintPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ function MdFileLinter(content: string, lintOptions: FileTransformOptions): void
const pluginOptions: PluginOptions = {
...options,
vars,
varsHashList,
root,
path: localPath,
lintMarkdown, // Should pass the function for linting included files
Expand Down
5 changes: 4 additions & 1 deletion src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ export function liquidMd2Html(input: string, vars: Record<string, unknown>, path
});
}

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

Expand Down
49 changes: 28 additions & 21 deletions src/resolvers/md2md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void>

const rawContent = fs.readFileSync(resolvedInputPath, 'utf8');

const cacheKey = cacheServiceBuildMd.getHashKey({filename: inputPath, content: rawContent, varsHashList});
const cacheKey = cacheServiceBuildMd.getHashKey({
filename: inputPath,
content: rawContent,
varsHashList,
});

let result: string;
let changelogs: ChangelogItem[];
Expand All @@ -31,15 +35,15 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void>
if (cachedFile) {
logger.info(inputPath, CACHE_HIT);
await cachedFile.extractCacheAsync();
const results = cachedFile.getResult<{result: string; changelogs: ChangelogItem[]; logs: Record<LogLevels, string[]>}>();
const results = cachedFile.getResult<{
result: string;
changelogs: ChangelogItem[];
logs: Record<LogLevels, string[]>;
}>();
result = results.result;
changelogs = results.changelogs;
} else {
const content = await getContentWithUpdatedMetadata(
rawContent,
metadata,
vars.__system,
);
const content = await getContentWithUpdatedMetadata(rawContent, metadata, vars.__system);

const cacheFile = cacheServiceBuildMd.createFile(cacheKey);
const envApi = PluginEnvApi.create({
Expand Down Expand Up @@ -90,7 +94,10 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void>
changesName = Math.trunc(new Date(changesDate).getTime() / 1000);
}
if (!changesName) {
changesName = `name-${mdFilename}-${String(changelogs.length - index).padStart(3, '0')}`;
changesName = `name-${mdFilename}-${String(changelogs.length - index).padStart(
3,
'0',
)}`;
}

const changesPath = join(outputDir, `changes-${changesName}.json`);
Expand All @@ -99,10 +106,13 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void>
throw new Error(`Changelog ${changesPath} already exists!`);
}

fs.writeFileSync(changesPath, JSON.stringify({
...changes,
source: mdFilename,
}));
fs.writeFileSync(
changesPath,
JSON.stringify({
...changes,
source: mdFilename,
}),
);
});
}

Expand All @@ -116,7 +126,10 @@ function copyFile(targetPath: string, targetDestPath: string, options?: PluginOp
const {envApi} = options;
let sourceIncludeContent: string;
if (envApi) {
sourceIncludeContent = envApi.readFile(relative(envApi.root, targetPath), 'utf-8') as string;
sourceIncludeContent = envApi.readFile(
relative(envApi.root, targetPath),
'utf-8',
) as string;
} else {
sourceIncludeContent = fs.readFileSync(targetPath, 'utf8');
}
Expand All @@ -136,11 +149,7 @@ function copyFile(targetPath: string, targetDestPath: string, options?: PluginOp
}

export function liquidMd2Md(input: string, vars: Record<string, unknown>, path: string) {
const {
applyPresets,
resolveConditions,
conditionsInCode,
} = ArgvService.getConfig();
const {applyPresets, resolveConditions, conditionsInCode} = ArgvService.getConfig();

return liquid(input, vars, path, {
conditions: resolveConditions,
Expand All @@ -152,9 +161,7 @@ export function liquidMd2Md(input: string, vars: Record<string, unknown>, path:
}

function transformMd2Md(input: string, options: PluginOptions) {
const {
disableLiquid,
} = ArgvService.getConfig();
const {disableLiquid} = ArgvService.getConfig();
const {
vars = {},
path,
Expand Down
45 changes: 36 additions & 9 deletions src/services/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,36 @@ export class CacheService {
if (!argsHash) {
const args = ArgvService.getConfig();
const staticArgs = pick(args, [
'varsPreset', 'ignore', 'outputFormat', 'allowHTML', 'vars', 'applyPresets',
'resolveConditions', 'conditionsInCode', 'disableLiquid', 'strict', 'ignoreStage', 'singlePage',
'removeHiddenTocItems', 'connector', 'lang', 'lintConfig', 'resources', 'addSystemMeta',
'contributors', 'ignoreAuthorPatterns', 'allowCustomResources',
'varsPreset',
'ignore',
'outputFormat',
'allowHTML',
'vars',
'applyPresets',
'resolveConditions',
'conditionsInCode',
'disableLiquid',
'strict',
'ignoreStage',
'singlePage',
'removeHiddenTocItems',
'connector',
'lang',
'lintConfig',
'resources',
'addSystemMeta',
'contributors',
'ignoreAuthorPatterns',
'allowCustomResources',
]);
argsHash = CacheService.getHash(JSON.stringify(staticArgs));
}
const contentHash = CacheService.getHash(content);
return {
key: this.getHash(JSON.stringify({filename, contentHash, varsHashList, argsHash})),
filename, contentHash, varsHashList,
filename,
contentHash,
varsHashList,
};
}

Expand All @@ -79,7 +98,9 @@ export class CacheService {
}

checkFile({key}: HashKey) {
if (this.disabled) { return; }
if (this.disabled) {
return;
}

const filepath = this.getCacheFilepath(key);
if (!fs.existsSync(filepath)) {
Expand All @@ -97,7 +118,9 @@ export class CacheService {
}

async checkFileAsync({key}: HashKey) {
if (this.disabled) { return; }
if (this.disabled) {
return;
}

const filepath = this.getCacheFilepath(key);
const exists = await fileExists(filepath);
Expand All @@ -121,7 +144,9 @@ export class CacheService {
}

addFile(file: CacheFile) {
if (this.disabled) { return; }
if (this.disabled) {
return;
}

const filepath = this.getCacheFilepath(file.getKey());
const place = path.dirname(filepath);
Expand All @@ -134,7 +159,9 @@ export class CacheService {
}

async addFileAsync(file: CacheFile) {
if (this.disabled) { return; }
if (this.disabled) {
return;
}

const filepath = this.getCacheFilepath(file.getKey());
const place = path.dirname(filepath);
Expand Down
Loading

0 comments on commit 2a7049a

Please sign in to comment.